Python · String in Depth
Length and index
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 1 — String Length with len()
In Python, we can find out how many characters are in a string using the
len()
function. (short for length)
codemon_name = "Aquachu"
name_length = len(codemon_name)
print("The name", codemon_name, "has", name_length, "characters.")
phrase = "I love my Codémon!"
print("The phrase has", len(phrase), "characters.")
What len() counts
The len() function counts EVERYTHING in a string:
- Letters (a, b, c...)
- Numbers (1, 2, 3...)
- Spaces ( )
- Special characters (!, @, #...)
- Punctuation (., ,, ;...)
Exercise 2 — Counting Characters
What is the length of the string "Pyrochu rocks!"?
Hint : Remember to count all characters: letters, the space, and the exclamation mark.
Exercise 3 — String Indexing
In Python, you can access individual characters in a string using indexing. Each character has a position (index) in the string, starting from 0 for the first character.
To access a character at a specific position, use square brackets [ ] with the index number:
codemon = "Florachu"
# First character (index 0)
first_char = codemon[0]
print("First character: ", first_char)
# Fourth character (index 3)
fourth_char = codemon[3]
print("Fourth character: ", fourth_char)
Index starts at 0!
Remember that in Python (and most programming languages), the first character in a string is at index 0, not 1. So the characters in "Florachu" are at positions:
F = 0, l = 1, o = 2, r = 3, a = 4, c = 5, h = 6, u = 7
Exercise 4 — Negative Indexing
Python also allows you to use negative indices to access characters from the end of the string:
- -1 refers to the last character
- -2 refers to the second-to-last character
- And so on...
codemon = "Florachu"
# Last character
last_char = codemon[-1]
print("Last character: ", last_char)
# Second-to-last character
second_last = codemon[-2]
print("Second-to-last character: ", second_last)
Negative index starts at -1!
In python, index in normal order starts at 0, while negative index starts at -1. So the characters in "Florachu" are at positions:
F = 0 or -8, l = 1 or -7, o = 2 or -6, r = 3 or -5, a = 4 or -4, c = 5 or -3, h = 6 or -2, u = 7 or -1
Exercise 5 — Accessing Characters
Fill in the blanks to extract the correct characters from the Codémon name.
codemon = "Pyrochu"
# Get the first character (P)
first = codemon[____]
# Get the last character (u)
last = codemon[____]
# Get the third character (r)
third = codemon[____]
print(first, third, last)
Options: 0 -1 2 1 3 -3
Hint : Remember: indices start at 0, and negative indices start at -1.
Exercise 6 — Get the Last Character
To get the last character of a string, we can use the length minus one. (or negative indexing but we already covered that)
name = "Florachu"
length = len(name)
last_char = name[length - 1]
print("Last character:", last_char)
len(name)
gives the number of characters. Since counting starts at 0, the last position is len - 1.
Why len - 1?
If a string has 8 characters. The length is 8. The indices go from 0 to 7. So last character is at position len - 1 => 8 - 1 = 7.
Exercise 7 — Matching Indices
Match each index with the character it would return from the string "Aquachu".
Items to match
• Aquachu[0]
• Aquachu[3]
• Aquachu[-1]
• Aquachu[-3]
Exercise 8 — Index Errors
It's important to be careful when using indices. If you try to access an index that doesn't exist in the string, Python will raise an IndexError : index out of range.
codemon = "Aquachu" # 7 characters
# Let's access a non-existing index
print(codemon[10]) # This will raise an IndexError
Avoid Index Errors
If a string has length n, the valid indices are from 0 to n-1 (or -1 to -n for negative indices). Always make sure your index is within this range!
Exercise 9 — String Operations and Results
Match each operation with its correct result for the string
name = "Pyrochu"
.
Items to match
• len(name)
• name[0]
• name[-1]
• name[len(name)-2]
Exercise 10 — Codémon Name Analyzer
Create a Codémon name analyzer! Choose a Codémon name and write a program that:
- Displays the length of the name using
len()
- Extracts and displays the first character (using index 0)
- Extracts and displays the last character (using negative index or len()-1)
- Creates a Codémon code by concatenating the first and last letters
Starter code
# Analyze your Codémon name here
Back to the course