Strings and Text

A string is a value that stores text.

Output:

Ada

Strings can use double quotes or single quotes:

Use one style consistently unless the text itself contains quotes.

Joining Text

You can join strings with +:

Output:

large model

The " " string supplies the space. Python does not insert it automatically.

f-Strings

An f-string lets you place values inside text:

Output:

score: 8

This is one of the most useful beginner tools. It lets you label values while debugging.

Labeled output

f-strings make printed values easier to read.

Runs locally with Python in your browser.

Ready to run.

Strings Are Not Numbers

This value is a number:

8

This value is text:

"8"

They look similar when printed, but Python treats them differently.

Output:

9
81

The first line adds numbers. The second joins strings.

Do Not Add Text to Numbers

This fails:

print("score: " + 8)

Python does not automatically join a string and an integer with +. Use an f-string:

print(f"score: {8}")
Exercise: String or number

What does this print?

print("4" + "5")

Answer it first, then check.

HintNotice the quotation marks

Both values are strings. For strings, + joins the text in order.

SolutionThe output is 45

The quotation marks make both values strings, so Python joins "4" and "5". It prints 45; it does not perform numeric addition.

Keep Text and Numbers Separate

Strings are how programs hold and print text. In Language Modeling, text becomes data. Here, the first step is simply to see text as a value Python can store.