Yesterday you computed (5 + 3) * 2 and got 16. What do you think "5" + "3" gives?
Eight? Or… maybe Python complains because they're strings, not numbers?
It runs cleanly and returns "53". With strings, + doesn't add — it concatenates. Glues the right side onto the end of the left:
greeting = "Hello" + " " + "world"
print(greeting) # Hello worldSo the same + symbol does two different things depending on what's on either side?
Yes. The operator's meaning depends on the type of its operands. With numbers it adds. With strings it concatenates. With a number on one side and a string on the other — it errors out, because Python won't guess what you meant. 1 + "two" is a TypeError.
And how do I find out how long a string is — say I want to know how many characters in "Hello"?
That's a function — len. You call it with the string in parentheses, and it returns the count:
print(len("Hello")) # 5Spaces count?
Everything between the quotes counts. "Hello world" has length 11 — five letters, one space, five letters.
What if I want a quote character inside my string?
Two ways. Use the other quote style: 'She said "hi"' works because the outer quotes are single. Or escape with a backslash: "She said \"hi\"". The escape tells Python "this quote is content, not a string boundary".
So strings are containers of characters, and Python gives me functions to glue them and measure them.
Two simple operations and you can already build a hundred small useful programs.
A string is a sequence of characters, written between matching quotes:
greeting = "Hello"
name = 'Ada'Single and double quotes are interchangeable. Pick one style for a file and stick to it.
+ — concatenationWith strings, + glues left + right into one new string:
first = "Hello"
second = "world"
print(first + " " + second) # Hello worldNote the explicit " " — Python doesn't insert spaces for you. If you want a space, you write a space.
len() — measure lengthlen is a built-in function that returns the number of characters in a string:
print(len("Hello")) # 5
print(len("Hello world")) # 11 (spaces count)
print(len("")) # 0 (empty string)The value len returns is an int — you can do arithmetic on it, store it in a variable, anything you'd do with a number.
Three options:
print('She said "hi".') # outer single, inner double
print("She said \"hi\".") # outer double, inner escaped
print("""She said "hi".""") # triple-quoted, no escape neededTriple-quoted strings ("""...""") also let you write multi-line text:
bio = """Ada Lovelace
Mathematician
Wrote the first algorithm."""
print(bio)print("Age: " + 36) # TypeError: can only concatenate str (not "int") to strYou'd convert the int to a string with str(36) — "Age: " + str(36) works and produces "Age: 36". (You'll meet a cleaner way to mix text and values in the f-strings lesson next.)
Yesterday you computed (5 + 3) * 2 and got 16. What do you think "5" + "3" gives?
Eight? Or… maybe Python complains because they're strings, not numbers?
It runs cleanly and returns "53". With strings, + doesn't add — it concatenates. Glues the right side onto the end of the left:
greeting = "Hello" + " " + "world"
print(greeting) # Hello worldSo the same + symbol does two different things depending on what's on either side?
Yes. The operator's meaning depends on the type of its operands. With numbers it adds. With strings it concatenates. With a number on one side and a string on the other — it errors out, because Python won't guess what you meant. 1 + "two" is a TypeError.
And how do I find out how long a string is — say I want to know how many characters in "Hello"?
That's a function — len. You call it with the string in parentheses, and it returns the count:
print(len("Hello")) # 5Spaces count?
Everything between the quotes counts. "Hello world" has length 11 — five letters, one space, five letters.
What if I want a quote character inside my string?
Two ways. Use the other quote style: 'She said "hi"' works because the outer quotes are single. Or escape with a backslash: "She said \"hi\"". The escape tells Python "this quote is content, not a string boundary".
So strings are containers of characters, and Python gives me functions to glue them and measure them.
Two simple operations and you can already build a hundred small useful programs.
A string is a sequence of characters, written between matching quotes:
greeting = "Hello"
name = 'Ada'Single and double quotes are interchangeable. Pick one style for a file and stick to it.
+ — concatenationWith strings, + glues left + right into one new string:
first = "Hello"
second = "world"
print(first + " " + second) # Hello worldNote the explicit " " — Python doesn't insert spaces for you. If you want a space, you write a space.
len() — measure lengthlen is a built-in function that returns the number of characters in a string:
print(len("Hello")) # 5
print(len("Hello world")) # 11 (spaces count)
print(len("")) # 0 (empty string)The value len returns is an int — you can do arithmetic on it, store it in a variable, anything you'd do with a number.
Three options:
print('She said "hi".') # outer single, inner double
print("She said \"hi\".") # outer double, inner escaped
print("""She said "hi".""") # triple-quoted, no escape neededTriple-quoted strings ("""...""") also let you write multi-line text:
bio = """Ada Lovelace
Mathematician
Wrote the first algorithm."""
print(bio)print("Age: " + 36) # TypeError: can only concatenate str (not "int") to strYou'd convert the int to a string with str(36) — "Age: " + str(36) works and produces "Age: 36". (You'll meet a cleaner way to mix text and values in the f-strings lesson next.)
Create a free account to get started. Paid plans unlock all tracks.