ShareYourGroupShareYourGroupPython

Lesson 5 of 21 · about 5 minutes with the exercises

Strings

A string is a piece of text: a name, a sentence, a phone number, a whole page. Strings sit inside quotation marks. This lesson shows how to join them, measure them and take pieces out of them.

Joining and repeating

The plus sign joins two strings. The star repeats a string.

first = "Asha"
last = "Verma"
print(first + " " + last)
print("ha" * 3)
print("-" * 20)

Output

Asha Verma
hahaha
--------------------

Joining does not add a space for you. That is why the example puts " " in the middle. The line of dashes is a neat trick for drawing a divider in your output.

How long is it?

len counts the characters in a string. Spaces and punctuation count too.

word = "Chandigarh"
print(len(word))
print(len("good morning"))

Output

10
12

Picking one character

Each character has a position number called an index. Counting starts at 0, not 1. Put the index in square brackets after the string.

word = "Python"
print(word[0])
print(word[1])
print(word[5])

Output

P
y
n

Negative numbers count from the end. -1 is the last character, which saves you from working out the length.

word = "Python"
print(word[-1])
print(word[-2])

Output

n
o
LetterPython
Index012345
Negative index-6-5-4-3-2-1

Asking for a position that does not exist gives an IndexError.

word = "Python"
print(word[6])

What Python says

Traceback (most recent call last):
  File "example.py", line 2, in <module>
    print(word[6])
          ~~~~^^^
IndexError: string index out of range

Slicing: taking a piece

A slice takes a run of characters. Write [start:stop]. The slice begins at start and ends just before stop.

word = "Python"
print(word[0:2])
print(word[2:6])
print(word[:2])
print(word[2:])

Output

Py
thon
Py
thon

Leave out the start and the slice begins at the beginning. Leave out the stop and it runs to the end. A handy way to read [2:6] is "from position 2, up to but not including position 6". The length of the piece is stop minus start.

A third number is the step. A step of 2 takes every second character. A step of -1 walks backwards, which reverses the string.

word = "Python"
print(word[::2])
print(word[::-1])

Output

Pto
nohtyP

Strings cannot be changed in place

You can read a character by its index, but you cannot replace it. Strings are immutable, which means fixed once made.

word = "Python"
word[0] = "J"

What Python says

Traceback (most recent call last):
  File "example.py", line 2, in <module>
    word[0] = "J"
    ~~~~^^^
TypeError: 'str' object does not support item assignment

To get a changed string, build a new one from the pieces you want and store it.

word = "Python"
word = "J" + word[1:]
print(word)

Output

Jython

Is this text inside that text?

The word in checks whether one string appears inside another. It gives True or False.

email = "asha@example.com"
print("@" in email)
print("gmail" in email)

Output

True
False

Text and numbers do not mix by themselves

Joining a string and a number with plus is an error. Turn the number into a string first with str, or let print do the joining with commas.

age = 14
print("Age: " + str(age))
print("Age:", age)

Output

Age: 14
Age: 14

Try it yourself

Exercise 1. Store your full name in a variable. Print its first letter, its last letter and its length.

Show one answer
name = "Meena Gill"
print(name[0])
print(name[-1])
print(len(name))

Output

M
l
10

Exercise 2. From the string "2026-11-12", print the year, the month and the day on separate lines using slices.

Show one answer
date = "2026-11-12"
print(date[:4])
print(date[5:7])
print(date[8:])

Output

2026
11
12

Exercise 3. Check whether the word "level" reads the same backwards. Print True or False.

Show one answer
word = "level"
print(word == word[::-1])

Output

True

Examples on this page were run with Python 3.14 on 12 November 2026. The output shown is the real output.

This is an independent learning site. It is not connected with or approved by the Python Software Foundation. "Python" is a trademark of the Python Software Foundation and is used here only to name the language.