A string is text: any sequence of characters — letters, digits, spaces, punctuation — wrapped in quotes. "Hello", 'Sam', "I am 9 years old" are all strings. The quotes are what tell Python “this is text, not a name and not a number.”
message = "Hello, world!"
print(message)
Output:
Hello, world!
A string can hold digits too: "42" is a string, not a number. That is why Lesson 5’s "5" + "3" gave "53": Python treated both values as text. If you need arithmetic, the value must be a number without quotes.
Both work, and they mean exactly the same thing. The trick is picking the kind that does not clash with the text itself:
print('Sam said "Hi!"')
print("It's a sunny day.")
Output:
Sam said "Hi!"
It's a sunny day.
The first line uses single quotes on the outside so the double quotes inside are safe. The second uses double quotes outside so the apostrophe in It’s is safe. Choose whichever keeps your text intact; there is no other difference.
+ joins strings end to end. This is called concatenation:
first = "Hello"
second = "Sam"
print(first + " " + second)
Output:
Hello Sam
Note the " " in the middle: without it, you would get HelloSam, because Python joins exactly what you give it and adds nothing on its own. Spaces, commas, and punctuation are your job.
* repeats a string:
print("Ha! " * 3)
Output:
Ha! Ha! Ha!
That is the same * as multiplication — with text, it means “repeat.”
A string and a number do not join. Try it and Python stops with an error:
print("You are " + 9)
Output:
TypeError: can only concatenate str (not "int") to str
Python will not guess what you mean. Turn the number into text first with str(): print("You are " + str(9)). Or use a comma, the way Lesson 4 showed: print("You are", 9) prints You are 9.
len() counts how many characters a string has, including spaces:
word = "hello world"
print(len(word))
Output:
11
Count it yourself: h-e-l-l-o (5), one space (6), w-o-r-l-d (11). Spaces count, which is the detail beginners most often miss.
You can also read a single character by its position. Counting starts at 0, not 1 — the first character is position 0:
word = "Sam"
print(word[0])
print(word[1])
print(word[2])
Output:
S
a
m
word[0] is S, word[1] is a, word[2] is m. Asking for a position past the end, like word[3], gives an error — IndexError: string index out of range — because there is no fourth character. Zero-based counting feels strange for a day and then becomes second nature — most popular languages count this way.
The last character is at position len(word) - 1 — one less than the length, because counting starts at 0:
word = "Sam"
print(word[len(word) - 1])
Output:
m
Strings, variables, and joining combine into the kind of line you see in real programs:
name = "Mia"
print("Hello, " + name + "! You are learning Python.")
Output:
Hello, Mia! You are learning Python.
Joining with + is the foundation, and it always works.
Press play on any slide and the lesson plays on by itself from there. You can still click Next or click a slide to move on.
Download the Lesson 6 slides (PowerPoint, with narration)
Download the Lesson 6 slides (PDF)
+, like the example above.len(). Count by hand and check they match.len(name) - 1)."Boo! " * 5 in the shell and explain the result in one sentence.A string is text in quotes. len() counts its characters and + joins strings together. Change the word and run again.
Try this: Change the word to a longer word and press Run again — the length number changes too.
Press Run to see the output here.
Save this as lesson06-practice.py and run it with F5 in IDLE.
# Lesson 6: Strings — Text, Quotes, Length
# WHAT TO DO: A string is text inside quotes. This program stores a
# name and should build a greeting by joining strings with +.
# Fill in line 11: replace the placeholder word with the variable name
# (no quotes around it), so the greeting uses the stored name.
# HINT: Joining "Hello, " and the name gives "Hello, Sam". A variable
# name on its own means "use the value stored under that name".
# Run with F5 in IDLE. You are done when you change `name` to your own name, run again, and the greeting changes too.
name = "Sam"
greeting = "Hello, " + "TODO" # <-- CHANGE THIS LINE: use the name variable
print(greeting)
print(len(name))
"42" is text, not a number.+ joins strings; * repeats them; Python adds no spaces for you.len() counts characters, including spaces.word[0] is the first character.Next lesson: Talking to Your Program: input().
"Hello" + " " + "Sam" give you?len("hello world")? Does the space count?"Sam", what is position 0, and what happens if you ask for position 3?Answer key: 1. Text wrapped in quotes. 2. To let the other kind of quote appear safely inside the text (for example, an apostrophe inside double quotes). 3. "Hello Sam" — without the space string you would get "HelloSam". 4. 11, and yes, the space counts as a character. 5. Position 0 is S; position 3 is an error, because there is no fourth character.