انتقل للمحتوى
Robo EgyptRobo Egypt
☰ Course contents
Robo Python: home
0. Lab tour1
1. The start5
2. Variables and input5
3. Conditions5
4. Loops5
5. Lists4
6. Functions4
7. Drawing with the turtle3
8. The project: the guess-the-number game3
9. Strings in depth1
10. Dictionaries (dict)3
11. Randomness and errors3
12. Classes and objects (class)2
13. Pro drawing and charts3
14. The final project: the quiz game2

String tools

Section 9: Strings in depthLesson 9.1 · ⏱ 9 min

A string in Python has ready tools. We write the variable's name, then a dot, then the tool's name.

  • upper() makes the letters capital, and lower() makes them small.
  • len(name) counts the letters (a space is a letter too).
  • name[0] is the first letter, because counting starts from zero.
  • name[0:4] is a piece from letter 0 up to just before letter 4.
  • replace swaps one word for another, and split() splits the sentence into a list of words.
name = "Robo Egypt"
print(name.upper())
print(name.lower())
print(len(name))
print(name[0])
print(name[0:4])
print(name.replace("Egypt", "Lab"))
words = "I love Python".split()
print(words)
print(len(words))
🖥️ The output in Robo Python:
ROBO EGYPT
robo egypt
10
R
Robo
Robo Lab
['I', 'love', 'Python']
3
🐍 Open the code in Robo Python ↗
The example in Robo Python: the code on the right and what it printed on the left
The example in Robo Python: the code on the right and what it printed on the left

📌 Another example: counting the vowels

We go through every letter, and count a, e, i, o and u.

🐍 Counting the vowels
word = "programming"
count = 0
for ch in word:
    if ch in "aeiou":
        count = count + 1
print(word, "has", count, "vowels")
🖥️ The output in Robo Python:
programming has 3 vowels
🐍 Open the code in Robo Python ↗

Test yourself

1. What does len("Robo Egypt") print?
2. What does "Python"[0] print?
  1. Print your name in capital letters, then its number of letters.
  2. Write a sentence of five words, split it with split, and print the last word.
✍️ Practise
Change the name in the example to your name, and predict the result before you click Run.
✍️ Practise here in the lessonor open the lab in a new page ↗

🎯 Challenge yourself

Easy

Print the word backwards ([::-1]).

👀 Show the solution
🐍 Solution
print("robot"[::-1])
🖥️ The output in Robo Python:
tobor
🐍 Open the code in Robo Python ↗

Result: tobor

Medium

Does the word read the same both ways (a palindrome)?

👀 Show the solution

When the program asks, we type: level.

🐍 Solution
word = input("Word: ")
if word == word[::-1]:
    print("palindrome")
else:
    print("not palindrome")
🖥️ The output in Robo Python:
Word: level
palindrome
🐍 Open the code in Robo Python ↗

Result: level: palindrome

Hard

Make the first letter of every word a capital (without title).

👀 Show the solution
🐍 Solution
text = "robo egypt lab"
words = text.split()
out = []
for w in words:
    out.append(w[0].upper() + w[1:])
print(" ".join(out))
🖥️ The output in Robo Python:
Robo Egypt Lab
🐍 Open the code in Robo Python ↗

Result: Robo Egypt Lab

Finished the lesson? Mark it so your progress counts.