9. Python Data Types - Strings Quiz

1. Which of the following is not a valid way to create a string in Python?
Answer: char('A')
2. What will be the output of the following Python code?

text = "Hello World!"
result = text.upper()
print(result)
Answer: HELLO WORLD!
3. What will be the output of the following Python code?

text = "this is a test string."
output = text.capitalize()
print(output)
Answer: This is a test string.
4. Which of the following will produce the output "Hello World"?
Answer: All of the above.
5. What is the correct output of the following Python code?

print("Hello\nWorld")
Answer: Hello
World
6. What is the primary purpose of the strip() method in Python?
Answer: To remove leading and trailing whitespace or specified characters from a string.
7. What will be the output of the following Python code?

s = " Hello World "
print(s.strip())
Answer: "Hello World" (with leading and trailing spaces removed)
8. What will be the output of the following Python code?

s = "---Python---"
print(s.strip('-'))
Answer: "Python"
9. If s = "abcba", what will be the output of s.strip('ab')?
Answer: "c"
10. Which of the following methods removes only leading whitespace characters from a string?
Answer: lstrip()
Score: 0 / 0 answered