Basic

Text is a value you can build

Text is a value like any other, and it can be stored, joined, and repeated. The moment you can treat text as a value, formatting stops feeling like a special feature and becomes ordinary building.

What you will see

Score: 42

About 2 min of reading

Try It

Build a message from a value that changes.

1

Store a name as text.

first = "Ada"

Before you run it, say out loud what it will print. Then open the answer.

What it prints
>>>
2

Join a greeting to it. The space lives inside the quotes.

print("Hello, " + first)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Hello, Ada
3

Repeat a dash twenty times to draw a rule.

print("-" * 20)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
--------------------
4

Put a number inside text with the f form. The braces hold the name, not the text.

score = 42 print(f"Score: {score}")

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Score: 42
Without the f, the braces are printed as written and the name is not swapped in. That is the most common formatting slip, and it is caught by reading the line.

Data Flow

You write text pieces and values, the computer joins or repeats them, and the result is one text value.
1 first holds Ada
↓
2 the plus joins the pieces
↓
3 one text value is made
↓
4 print shows the message
Why this works
Quotes are the boundary

Quotes mark where text starts and stops. Inside them, a character is just a character, so a digit is text and not a number. Forgetting the quotes turns text into a name, and the computer then complains that the name was never filled.

When you have a choice

Building versus writing

You could type the whole message at once, or build it from pieces. Building from pieces is the habit that pays off, because the pieces are values that change at run time while the frame around them stays fixed.

Examples

Join two pieces

first = "Ada"
print("Hello, " + first + "!")

The plus joins text end to end. The spaces are inside the quotes on purpose, because a space outside the quotes is not a value at all. Joining is how a message is assembled.

Repeat a piece

print("-" * 20)

Star repeats text a set number of times, which is how a rule or a bar is drawn. Repeating only works on text, so "5" 3 gives 555 while 5 3 gives 15. The kind decides again.

Place values inside text

score = 42
print(f"Score: {score}")

The f before the quotes lets a name sit inside braces, and the computer swaps in its value. This is the readable way to mix text and values, and it is one value at the end.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← An expression makes one value
Previous Lesson
An expression makes one value
Next Lesson
A value can come from outside
A value can come from outside →