Python: lists and strings
Python lists and strings: indexes, slices, common methods, split and join. Twenty corrected exercises for working with text one step at a time.
Getting started
Home Assistant
Buying
Python lists and strings: indexes, slices, common methods, split and join. Twenty corrected exercises for working with text one step at a time.


AI-generated illustration
Some links on this page are affiliate links. The button opens Amazon so you can check today’s price.
Lists group several values in an order (often alike: numbers, names…). Strings (str, short for string) are also handled by indexes and slices, but they are immutable: a method such as lower() returns a new string rather than changing the variable “in place.”
Python track (7/10) — Track contents · Lesson 6 — functions · Lesson 8 — text files · Intermediate Python — venv and pip
Why this lesson matters: almost every small game (hangman, scores, nicknames) stores its data in lists or strings. Knowing how to split a sentence into words (split) or join items (join) saves you from needlessly complicated loops.
[1, 2, 3]. You can change it (append, pop)."Hello". You do not change the characters “in place”: you create a new string (strip, replace, lower).The index of the first item is 0. The index -1 means the last item.
scores = [12, 15, 9]
scores.append(18)
print(scores[0]) # 12
print(scores[-1]) # 18
print(scores[1:3]) # [15, 9] — slice: index 1 included, 3 excluded
A few useful methods: append(x), pop() (often removes the last item), insert(i, x), len(scores).
With a loop:
total = 0
for s in scores:
total += s
print(total) # or simply sum(scores)
s = " Robot "
print(s.strip().lower()) # "robot"
morceaux = "a,b,c".split(",") # ['a', 'b', 'c']
print("-".join(morceaux)) # a-b-c
Test for a substring: if "bot" in s:.
Two common approaches early in the track:
mot = "Python"
print(mot[::-1]) # slicing: step -1 → reverse order
Or a loop that builds the result one character at a time.
A compact way to build a list from another sequence:
nombres = [1, 2, 3, 4, 5]
carres = [n * n for n in nombres]
You do not have to use it right away; keep the idea for lightening some loops later.
Each exercise uses the lists and strings from the lesson (indexes, slices, append, split, join, and so on). The solutions are hidden by default: click Show solution to compare your code.
Exercise 1 — Create nombres = [3, 7, 1] and print its length with len. Easy
nombres = [3, 7, 1]
print(len(nombres))
Exercise 2 — With scores = [10, 14, 9], print the first and the last item (indexes 0 and -1). Easy
scores = [10, 14, 9]
print(scores[0])
print(scores[-1])
Exercise 3 — Start from xs = [1, 2], add 5 at the end with append, then print the list. Easy
xs = [1, 2]
xs.append(5)
print(xs)
Exercise 4 — Compute the sum of the items in [4, 1, 3] with a for loop and a variable total. Easy
total = 0
for x in [4, 1, 3]:
total += x
print(total)
Exercise 5 — From "a,b,c", get a list of three strings with split(",") and print it. Easy
parts = "a,b,c".split(",")
print(parts)
Exercise 6 — For s = " HELLO ", print the string in lowercase with no spaces at the start or the end (strip and lower). Easy
s = " HELLO "
print(s.strip().lower())
Exercise 7 — With mots = ["un", "deux", "trois"], print a single line un / deux / trois using join with " / ". Easy
mots = ["un", "deux", "trois"]
print(" / ".join(mots))
Exercise 8 — For lettres = ["a", "b", "c", "d", "e"], print the slice from index 1 to 3 exclusive (expected result: b and c). Intermediate
lettres = ["a", "b", "c", "d", "e"]
print(lettres[1:3])
Exercise 9 — Ask for a sentence and print how many times the letter a appears (lowercase only), with .count("a"). Intermediate
phrase = input("Sentence? ")
print(phrase.count("a"))
Exercise 10 — Ask for a string and print it reversed with the slice [::-1]. Intermediate
s = input("Text? ")
print(s[::-1])
Exercise 11 — Create nombres = [1, 2, 3, 4, 5], then a new list carres with a comprehension: the square of each n in nombres. Print carres. Intermediate
nombres = [1, 2, 3, 4, 5]
carres = [n * n for n in nombres]
print(carres)
Exercise 12 — Start from xs = [10, 20, 30]: remove the last item with pop(), print the removed item, then the updated list. Intermediate
xs = [10, 20, 30]
dernier = xs.pop()
print(dernier)
print(xs)
Exercise 13 — Insert "x" at index 0 in ["a", "b"] with insert, then print the list. Intermediate
xs = ["a", "b"]
xs.insert(0, "x")
print(xs)
Exercise 14 — Ask for a string s and a word m; print yes if m is contained in s (the in operator), otherwise no. Intermediate
s = input("Sentence? ")
m = input("Word? ")
if m in s:
print("yes")
else:
print("no")
Exercise 15 — From "pomme,poire,banane", get a list of three fruits, then print them again on one line, separated by |, with join. Hard
fruits = "pomme,poire,banane".split(",")
print(" | ".join(fruits))
Exercise 16 — List notes = [12, 15, 9, 14]: compute the average (the sum of the items divided by the number of items), as a float. Hard
notes = [12, 15, 9, 14]
moyenne = sum(notes) / len(notes)
print(moyenne)
Exercise 17 — Count how many vowels (a, e, i, o, u, lowercase) appear in an entered string (walk for c in texte and test c.lower()). Hard
texte = input("Text? ")
voy = "aeiou"
n = 0
for c in texte:
if c.lower() in voy:
n += 1
print(n)
Exercise 18 — Build a new list containing only the even numbers from [1, 2, 3, 4, 5, 6] with a comprehension (condition n % 2 == 0). Hard
pairs = [n for n in [1, 2, 3, 4, 5, 6] if n % 2 == 0]
print(pairs)
Exercise 19 — Ask for a sentence, split it into words with split() (spaces), and print the number of words. Hard
phrase = input("Sentence? ")
mots = phrase.split()
print(len(mots))
Exercise 20 — Ask for a string and say whether it is a palindrome (the same reversed), by comparing s and s[::-1] (you can lowercase s to ignore case). Hard
s = input("Word or sentence? ").strip().lower()
if s == s[::-1]:
print("palindrome")
else:
print("no")
What is the difference between liste[i:j] and liste[i:j:k]?
Both forms are slices. i:j runs from index i inclusive to j exclusive. The third number k is the step: [::-1] reverses the order (very handy for palindromes, exercise 20).
Why does split(",") fail on my CSV?
If a cell contains a comma inside quoted text, a plain split is not enough: you need the csv module (seen later) or stricter rules. For simple lists (a,b,c), split is fine.
A list or a string for storing words?
A list of strings (["chat", "chien"]) lets you change one word without touching the others. One long string is mainly for display or export (join).
The error IndexError: list index out of range?
The index you asked for does not exist (often len(liste) when the last valid index is len(liste) - 1). Check with print(len(scores)) before you read scores[i].
Reading and writing text files (lesson 8) lets you save lists (scores, nicknames) to disk. Next: errors and debugging to read the messages when an index or a type is wrong.
Was this page helpful?
Thank you!
What should we improve?
Torn between two models or stuck on a build? Drop us a line and we will get back to you by email.
Cookies and analytics
We use Google Analytics 4 to understand how the site is used (page views, navigation). It is only switched on if you accept. Declining does not stop you using the site. More: cookie settings and privacy policy (in French).