LESSON 04
Strings
Text is more than words in quotes. Learn to join it, measure it, and reshape it.
USES: print( ), variables, quotation marks, the * operator from Lesson 3.
01
Strings are text
Anything inside quotation marks is a string. A string is Python's word for text: names, sentences, even a single letter. You have used strings in every lesson so far. Now you learn what you can do with them. Look at the second line below: "123" has quotes, so it is text that happens to look like a number, not a number you can do maths with.
02
Single or double quotes
Python accepts both "double" and 'single' quotes, and they do the same job. Pick one style and stay consistent. The only rule: the quote that opens the string must be the same kind that closes it. Having both lets you put one kind of quote inside the other.
BLUEPRINT · TAP OR HOVER EACH PIECE
Blueprint 1 of 2: joining. Tap each piece.
03
Joining strings with +
You already know + adds numbers. Put it between two strings and it joins them into one new string. You don't need to remember the technical word for this. Just remember the idea: glue.
04
The comma in print( )
There is a second way to put text side by side, and you may have seen it: a comma between things inside print( ). It looks like + but it does a different job. + makes one new string and adds no space. The comma does not join anything. It hands print( ) several separate things, and print( ) puts a space between them when it shows them. That is why the comma can show a number next to text, which + cannot do.
05
Repeating with *
In Lesson 3, * multiplied numbers. Between a string and a whole number, it repeats the string. The same symbol does a different job depending on what is next to it. That is common in Python, and you will meet it again.
BLUEPRINT · TAP OR HOVER EACH PIECE
Blueprint 2 of 2: measuring. Tap each piece.
06
How long is it? len( )
len( ) is a function, like print( ). You put a string between its brackets and it gives you back the length: how many characters (cells) it has.
07
Positions start at 0
Every character has a position, and Python starts counting at 0, not 1. The first character is at position 0. To grab one character, write the string, then the position in square brackets. A negative number counts from the end, so name[-1] is always the last character, without needing to know the length.
EVERY CHARACTER HAS A POSITION
08
Counting from the end
Positive positions count from the left, starting at 0. Negative positions count from the right, and the last character is -1 (not -0, because -0 is just 0, and that spot is already taken). So name[-1] is always the last character, name[-2] the one before it, and in a 6-letter word name[-6] is the first. The grey numbers in the letter row above are exactly this idea, drawn.
09
Methods: upper( ), lower( ), title( )
A method is a function that belongs to a string. You write the string, then a dot, then the method name. .upper( ) always makes every letter uppercase. .lower( ) always makes every letter lowercase. .title( ) makes the first letter of each word uppercase and the rest lowercase. Look at the last line of the output: name is still the original.
10
Text and numbers don't mix with +
Here is the thing every beginner meets sooner or later. Try to glue text and a number together with +, and Python stops with an error: it cannot glue text to a number. Some older tutorials work around this with extra functions or %s. You won't need them. Right now you have a working fix from Step 04, the comma. The nicer fix arrives in the next lesson.
11
Quick quiz: what does this print?
What does print("5" + "3") show? Decide first, then check.
12
Quick quiz: comma or plus?
What does print("5", "3") show? Decide first, then check.