Working With Strings
Joining text and dropping values inside it
A string is just text. You've used them since lesson one. Now let's do more with them.
Joining strings
You can stick strings together with +. But be careful — there's no automatic space, so you add your own:
first = "Ada"
last = "Lovelace"
print(first + " " + last)The " " in the middle is a string containing a single space. Without it you'd get AdaLovelace squashed together.
The easy way: f-strings
Joining with + gets fiddly fast. Python has a lovely shortcut called an f-string. You put the letter f right before the opening quote, then drop variables straight into the text inside curly braces { }:
name = "Ada"
age = 36
print(f"{name} is {age} years old.")That shows: Ada is 36 years old. Python swaps each {...} for the value inside it. F-strings are the friendly, modern way to build messages, and you'll use them constantly.
A couple of useful tricks
Strings can do things to themselves. We ask using a dot:
shout = "hello"
print(shout.upper()) # HELLO
print(len("hello")) # 5 (how many characters).upper() makes a SHOUTING copy. len(...) counts the characters.
Tip: Forgetting the f is a classic beginner slip. Without it, "{name}" shows the literal text {name} instead of the value. If a variable isn't getting swapped in, check for the missing f.
Your turn
Change the name and age in the f-string and run it.
Press “Run code” to see the result.
Quick check
Q1 What does f"{name} is here" do when name holds "Ada"?
Q2 Why does "Ada" + "Lovelace" give AdaLovelace with no space?
Want to save your progress? It's free.
Create a free account