String Exercises

Test your understanding of strings in Python with these exercises:

Exercise 1: Reverse a String

Write a Python program to reverse the following string: "Python".

Python
# Your code here
text = "Python"
reversed_text = text[::-1]
print(reversed_text)  # Output should be "nohtyP"

Exercise 2: Count Vowels in a String

Write a Python program to count the number of vowels (a, e, i, o, u) in the string "Hello, World!".

Python
# Your code here
text = "Hello, World!"
vowels = "aeiouAEIOU"
count = 0

for char in text:
    if char in vowels:
        count += 1

print(count)  # Output should be 3

Exercise 3: Replace a Word in a String

Write a Python program to replace the word "World" with "Python" in the string "Hello, World!".

Python
# Your code here
text = "Hello, World!"
new_text = text.replace("World", "Python")
print(new_text)  # Output should be "Hello, Python!"