Skip to content

Strings

3 min read

Text is a value too, just like a number, and it has its own operations. By the end of this lesson you'll join and repeat text, measure it, pick out single characters by their position, and know what Python means when it says str.

Text is not a number

Two lines. They look like they should do the same thing. Predict both.

What will this print?

Decide before you look. Guessing wrong is how this sticks.

print("5" + "3")
print(5 + 3)

Python prints

53
8

"5" and "3" are text, and + on text joins it: the characters 5 and 3 side by side. Without quotes they're numbers, and + adds them.

A piece of text is a , and Python calls its type str. "5" is a string that happens to contain a digit; 5 is a number. + means "join" for strings and "add" for numbers.

So what happens when you mix them?

* works on strings too. It repeats them, which is handy for lines on a receipt.

print("ha" * 3)
print("-" * 12)

In the editor, press Escape then Tab to move on.

Change the 12 so the dashes are exactly as long as the words Selam Coffee.

How long is a string?

len() counts the characters in a string, spaces included.

print(len("Selam"))
print(len("Selam Coffee"))
print(len("ሰላም"))

In the editor, press Escape then Tab to move on.

The last one is Selam written in Ge'ez letters, and Python counts each letter as one character: three. Python works with the letters of every language, not only English.

Each character has a position

Every character in a string has a numbered position, called its . Counting starts at 0, not 1. You can also count from the end with negative numbers: -1 is the last character.

Positions in the string "Selam"
CharacterSelam
From the start01234
From the end-5-4-3-2-1
The positions in "Selam", counted from the start and from the end.

To get one character, put its index in square brackets after the string, or after a name that refers to it.

Quick check

What is "Selam"[1]?

name = "Selam"
print(name[0])
print(name[1])
print(name[-1])
print(name[-2])

In the editor, press Escape then Tab to move on.

Change name to your own name and run it again.

A string of five characters has positions 0 to 4. Asking for position 5 goes past the end:

You can read characters this way, but you can't change them: strings can't be changed in place. You'll see what to do instead in Module 6.

Quotes inside strings

If your text contains an apostrophe, wrap it in double quotes. If it contains double quotes, wrap it in single quotes.

print("Abebe's shop")
print('She said "Selam" and left.')

In the editor, press Escape then Tab to move on.

For text over several lines, use three quotes at each end. The line breaks become part of the string.

address = """Selam Coffee
Bole Road
Open every day"""
print(address)

In the editor, press Escape then Tab to move on.

Exercise

First, last, and length

Give the name name to the string "Meron". Print its first letter, its last letter, and its length, like this. Then try it with your own name: your code should still work without other changes.

Output
First letter: M
Last letter: n
Length: 5

Your code

name = "Meron"

In the editor, press Escape then Tab to move on.

Show a solution

This is one way to solve it, not the only one. If yours prints the same thing, it works.

name = "Meron"
print("First letter:", name[0])
print("Last letter:", name[-1])
print("Length:", len(name))

Key takeaways

  • A string (str) is text in quotes. "5" is text; 5 is a number.
  • + joins strings and * repeats them. Mixing a string and a number with + is a TypeError.
  • len() counts characters, spaces included, in any language.
  • Indexes start at 0; -1 is the last character. Going past the end is an IndexError.
  • Use the other kind of quote when your text contains one, and triple quotes for several lines.