Python String Exercises
Python String Questions
Below are some Python string-related questions to test your knowledge:
1. What is the output of the following code?
Code:
text = "Python"
print(text[1:4])
Answer: yth
2. How do you reverse a string in Python?
Code:
text = "hello"
print(text[::-1])
Answer: olleh
3. What does the .join() method do?
Code:
words = ["Hello", "World"]
result = " ".join(words)
print(result)
Answer: Hello World
4. How would you check if a string contains only digits?
Code:
text = "12345"
print(text.isdigit())
Answer: True
5. What does the .replace() method do?
Code:
text = "Hello world"
result = text.replace("world", "Python")
print(result)
Answer: Hello Python