Python Strings: Concatenation, f-strings, and Indexing

Python 3.14 · ✓ verified by execution on 2026-07-19

Text values are strings (str), and you’ll shape them constantly — greetings, labels, messages. Start by joining strings together with +, which for text means “stick these end to end”:

python
greeting = "Hello" + " " + "World"
print(greeting)
Output
Hello World

That works with variables too:

python
first = "Ada"
last = "Lovelace"
print(first + " " + last)
Output
Ada Lovelace

The easy way: f-strings

Joining with + gets clumsy fast — and it breaks the moment you try to mix in a number (you saw that TypeError earlier). The modern, readable way is an f-string: put f before the quotes, then drop any variable inside { }:

python
name = "Ada"
age = 36
print(f"{name} is {age} years old")
Output
Ada is 36 years old

Notice age is a number, and the f-string inserted it with no fuss — no converting required. That’s why f-strings are the tool you’ll reach for again and again.

Measuring length with len()

len() tells you how many characters a string has:

python
print(len("hello"))
Output
5

And you can combine tools — an f-string with len() inside:

python
word = "Python"
print(f"{word} has {len(word)} letters")
Output
Python has 6 letters

Reaching a single character: indexing

Every character has a position, called its index. The catch that trips up every beginner: indexing starts at 0, not 1. So the first character is at index 0:

python
word = "Python"
print(word[0])
Output
P

A negative index counts back from the end, so -1 is the last character:

python
word = "Python"
print(word[-1])
Output
n

The mixing error, and the fix

Trying to join text and a number with + fails — Python won’t guess what you mean:

python
print("Score: " + 100)
This example raises an error (on purpose)
TypeError

The clean fix is an f-string, which handles the number for you:

python
score = 100
print(f"Score: {score}")
Output
Score: 100

Edge cases worth remembering

Check yourself

What happens when you run print("Score: " + 100)?

Reveal answer

Python raises a TypeError — you can't add text and a number — "Score: " is text and 100 is a number, so + raises TypeError. Use an f-string, f"Score: {100}", or convert with str(100).

In the string "Python", what is at index 0?

Reveal answer

'P' — indexing starts at 0 — Python indexes from 0, so "Python"[0] is 'P', [1] is 'y', and so on.

Which line correctly prints: Ada is 36 years old (with name = "Ada", age = 36)?

Reveal answer

print(f"{name} is {age} years old") — The f-string inserts both values automatically. The second line fails (can't + a number to text); the third has no f, so the braces stay as literal text.

What is len("a b") (note the space)?

Reveal answer

3 — the space is a character too — len() counts every character, and a space is a real character. "a b" is 'a', ' ', 'b' — length 3.

Challenges

Challenge 1 +10 XP

Create a variable city set to "Paris", then use an f-string to print exactly: I love Paris

python
  • Test 1 — expects "I love Paris"
Need a hint? (−25% XP)

Start the string with f and put {city} where the name should go.

Show solution (0 XP)
city = "Paris"
print(f"I love {city}")

Challenge 2 +15 XP

Given word = "Python", print its length on the first line and its last character on the second line.

python
  • Test 1 — expects "6\nn"
Need a hint? (−25% XP)

len(word) gives the length; word[-1] gives the last character.

Show solution (0 XP)
word = "Python"
print(len(word))
print(word[-1])