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.

INPUTprint("Python") print("123") print("hello there!")
OUTPUTPython 123 hello there!
METAPHORA string is like one row of cells in a spreadsheet. Each cell holds exactly one character: a letter, a space, a digit or a symbol. The cells are in a fixed order, and each one has a position. Where it stops working: in a spreadsheet you can retype a single cell. In Python you can't edit a string in place. You make a new string instead. You will see this in Step 08.

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.

INPUTprint("It's a nice day") print('She said "hi"')
OUTPUTIt's a nice day She said "hi"
TIPIf your text contains an apostrophe (like It's), wrap it in double quotes. If it contains double quotes, wrap it in single quotes.

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.

INPUTfirst = "Ada" last = "Lovelace" print(first + " " + last)
OUTPUTAda Lovelace
TIPRemove the " " and you get AdaLovelace. No space appears unless you put one there.

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.

INPUTprint("Hello", "World") print("Age:", 20)
OUTPUTHello World Age: 20
TIPFor now, use the comma only inside print( ). Why a comma behaves differently in other places is a story for a later lesson.

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.

INPUTprint("ha" * 3) print("-" * 20)
OUTPUThahaha --------------------

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.

INPUTname = "Python" print(len(name)) print(len("Hi there"))
OUTPUT6 8
TIPSpaces count as characters. "Hi there" has 8: two for Hi, one space, five for there.
METAPHORlen( ) is counting the filled cells in the row.

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.

INPUTname = "Python" print(name[0]) print(name[5]) print(name[-1])
OUTPUTP n n
TIPname[6] is an error, because there is no position 6: a 6-letter word has positions 0 to 5. Beginners hit this constantly, and it is a good first error to see on purpose.
METAPHORGo back to the row of spreadsheet cells. Where it stops working: a spreadsheet labels its first cell 1 (or A). Python labels it 0, because the position means 'how many steps from the start'. The first character is 0 steps away.

EVERY CHARACTER HAS A POSITION

P0-6
y1-5
t2-4
h3-3
o4-2
n5-1

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.

INPUTname = "Python" print(name[-1]) print(name[-2]) print(name[-6])
OUTPUTn o P
TIPYou never need the length to get the last character. name[-1] works on every string, forever.
METAPHORA queue of people: you can number them from the front (0, 1, 2) or from the back (-1 is the last person in the queue).

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.

INPUTname = "ada lovelace" print(name.upper( )) print(name.title( )) print(name)
OUTPUTADA LOVELACE Ada Lovelace ada lovelace
TIPMethods don't change the original string. They give back a new one. To keep the new one, put it back in a box with name = name.upper( ). This is the spreadsheet row from Step 01: you can't edit a string in place, you make a new one.
name(empty box)
INPUT(nothing run yet)

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.

CRASHESprint("Age: " + 20) # TypeError
WORKSprint("Age:", 20) # Age: 20

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.

Report a mistake or suggest a lesson