🎓 A1Learner
🔍
0 of 116 lessons completed

Python Strings

A string is a piece of text made of characters in order. Quotes mark the start and end of a string.

Two ways to quote text

Both quote styles work the same way.

print("Hello")
print('Hello')

Quotes inside quotes

Use one quote style outside. Then the other style is safe inside.

print('She said "hi"')
print("It's fine")

A string with many lines

Triple quotes let a string cover many lines.

poem = """Roses are red
Violets are blue"""
print(poem)

Count the characters

len() counts every character. Spaces count too.

word = "hello"
print(len(word))
print(len("hi there"))

Example

address = """Ada Lovelace
12 Code Street
London"""
print(address)
print(len(address))

Key points

  • String: a piece of text, made of characters in order
  • Character: one letter, digit, space, or mark
  • Use single quotes or double quotes to make a string
  • Use triple quotes to make a string with many lines
  • Use len() to count the characters in a string

In this example, address holds a string with three lines. Triple quotes let the string cover all three lines. print(address) shows the full address. Each line break also counts as one character. len(address) counts all the letters, spaces, and line breaks. That total is 34.

Note

Single and double quotes make the same kind of string.

Python does not treat them as different types.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Python Playground

Exercise

Which code makes a string?