Python String Methods
The last page joined text and left the pieces as they were. This page asks a string to make a new string. The dot does that. guest.upper() builds LIDA. It does not change what guest holds. len sits outside the string, with no dot, and counts characters.
The name stays
guest = "lida" stores the lowercase text. print(guest.upper()) writes LIDA. The next print(guest) writes lida again. "Tea".lower() writes tea. Upper and lower each return a new string. The stored text changes only if you store that new string yourself.
guest = "lida"
print(guest.upper())
print(guest)
print("Tea".lower())
The three lines are LIDA, then lida, then tea. If you want the name itself to hold the capitals, write guest = guest.upper() and then print(guest). Until that line, guest is still lida.
Spaces at the ends, not in the middle
strip removes spaces at the two ends and returns a new string. " tea ".strip() writes tea. "lida tea".strip() writes lida tea, because the space between the words is not at an end. The stored text, if you had a name, stays as it was until you store the result.
print(" tea ".strip())
print("lida tea".strip())
len counts every character
len("lida") is 4. len("lida tea") is 8: four for lida, one for the space, three for tea. The space counts. len does not use a dot. You pass the string in parentheses.
print(len("lida"))
print(len("lida tea"))
The two lines are 4, then 8. len does not join, does not change case, and does not remove spaces. It only reports how many characters are there.
Left for the next page
This page does not place a name inside a sentence, does not cut the text into positions, and does not add numbers. The next page puts a stored name into the text you write. Case, ends, and length stay what you just saw.
Check yourself
- print(guest.upper()) writes LIDA, and print(guest) still writes lida.
- "Tea".lower() writes tea.
- " tea ".strip() writes tea.
- "lida tea".strip() writes lida tea.
- len("lida tea") is 8.
- guest = guest.upper() is what stores LIDA under the name.
Misses
- Expecting guest itself to become LIDA after guest.upper()
- Expecting strip to remove the space in "lida tea"
- Forgetting the space when you count "lida tea"
- Writing len.("lida") or guest.len()
- Thinking upper changes the original and also returns nothing
Keep
- guest.upper() writes LIDA. guest stays lida until you store the result.
- "Tea".lower() writes tea.
- strip removes spaces at the ends. The space in "lida tea" stays.
- len("lida") is 4. len("lida tea") is 8. The space counts.
- The next page puts a name into the text you write.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top