Tutorials › Python Tutorial › Python Strings

Python Strings

11 min read · Lesson 7 of 24

A string is a piece of text: a name, a message, a phone number, a whole paragraph. In Python, strings have the type str. They are one of the types you will use most often.

Creating strings

Wrap text in either single quotes or double quotes. Both work the same way.

Example PYTHON
city = "Kisumu"
lake = 'Victoria'
print(city)
print(lake)
Result
Kisumu
Victoria
Try it Yourself »

Pick one style and use it consistently. Using double quotes makes it easy to include an apostrophe inside the text, and the other way round:

Example PYTHON
print("It's a sunny day in Malindi.")
print('She said "Asante sana" and left.')
Result
It's a sunny day in Malindi.
She said "Asante sana" and left.
Try it Yourself »

Multi-line strings

Triple quotes (""" or ''') let a string span several lines. The line breaks are kept.

Example PYTHON
address = """Skope Digital Academy
P.O. Box 1234
Kisumu, Kenya"""
print(address)
Result
Skope Digital Academy
P.O. Box 1234
Kisumu, Kenya
Try it Yourself »

Escape characters

A backslash \ inside a string starts an escape sequence: a way to write characters that are hard to type directly.

CodeMeaning
\nnew line
\ttab
\\a single backslash
\"double quote
\'single quote
Example PYTHON
print("Name:\tAmina\nTown:\tGarissa")
print("Path: C:\\Users\\Amina")
Result
Name:	Amina
Town:	Garissa
Path: C:\Users\Amina
Try it Yourself »

Strings are sequences

A string is an ordered sequence of characters, and each character has a position called an index. Counting starts at 0, not 1.

Example PYTHON
word = "Nairobi"
print(word[0])
print(word[3])
print(word[-1])
print(word[-2])
Result
N
r
i
b
Try it Yourself »

Negative indexes count from the end: -1 is the last character, -2 the one before it.

Length

The built-in len() function returns the number of characters.

Example PYTHON
word = "Nairobi"
print(len(word))
print(len(""))
Result
7
0
Try it Yourself »

Slicing

You can take a part of a string with a slice: text[start:end]. The slice includes start but stops before end.

Example PYTHON
phone = "0712345678"
print(phone[0:4])
print(phone[4:])
print(phone[:2])
print(phone[-3:])
Result
0712
345678
07
678
Try it Yourself »

Leaving out start means "from the beginning". Leaving out end means "to the end". Slicing works the same way on lists; see Python List Methods and Slicing.

Joining and repeating

Use + to join (concatenate) strings and * to repeat them.

Example PYTHON
first = "Brian"
last = "Odhiambo"
full = first + " " + last
print(full)
print("=" * 20)
print("ha" * 3)
Result
Brian Odhiambo
====================
hahaha
Try it Yourself »

Checking for text with in

The in keyword checks whether one string appears inside another. The answer is True or False.

Example PYTHON
message = "Your M-Pesa payment of KES 500 was received."
print("M-Pesa" in message)
print("Airtel" in message)
print("Airtel" not in message)
Result
True
False
True
Try it Yourself »

Strings cannot be changed

Strings are immutable: once created, their characters cannot be changed in place. Trying word[0] = "n" raises a TypeError. Instead, you build a new string and store it, usually with a method like replace().

Example PYTHON
town = "kericho"
town = town.capitalize()
print(town)
Result
Kericho
Try it Yourself »
Note: String methods such as capitalize() never modify the original string. They return a new string, which you must store or print. The next lesson, Python String Methods, covers many of them.

Looping over a string

Because a string is a sequence, a for loop can visit each character in turn:

Example PYTHON
for letter in "Lamu":
    print(letter)
Result
L
a
m
u
Try it Yourself »

Exercises

Exercise

Print the first character of the string.

county = "Bungoma"
print(county[])
Exercise

Print the number of characters in the string.

print(("Kakamega"))

Next: Python String Methods.

Sign in or create a free account to track progress, earn XP, badges and a certificate.