Level 1
0 / 100 XP

Strings and text

Every program works with two kinds of data: numbers and text. You've covered numbers. Now meet the other one.

In Python, text is called a string.

What is a string?

A string is any sequence of characters wrapped in quotes:

Python
name = "Alice" city = "New York" message = "Hello, world!" print(name) print(city) print(message)

Letters, spaces, punctuation, numbers — if it's wrapped in quotes, it's a string.

Single or double quotes

Python accepts both. Pick one and be consistent:

Python
a = "Hello" b = 'Hello' print(a) # Hello print(b) # Hello

They're identical. Double quotes are more common, but single quotes are perfectly valid.

Strings can contain numbers

This is a common source of confusion early on:

Python
age_number = 25 age_string = "25" print(age_number) # 25 print(age_string) # 25

They print the same — but Python treats them completely differently. One is a number you can do math with. The other is just text that happens to look like a number.

Measuring a string with len()

len() tells you how many characters are in a string:

Python
name = "Alice" city = "New York" print(len(name)) # 5 print(len(city)) # 8

Spaces count as characters. Punctuation counts too.

Python
message = "Hello, world!" print(len(message)) # 13

Key takeaways

  • A string is any text wrapped in quotes
  • Single and double quotes both work — pick one and stick with it
  • A number inside quotes is a string, not a number — Python treats them differently
  • len() counts every character in a string, including spaces and punctuation