String Data Types in Python - 25 Multiple Choice Questions
1. How do you create a string in Python?
A) `string s = "Hello"`
B) `s = 'Hello'`
C) `s = Hello`
D) Both A and B
Answer: B
A) `string s = "Hello"`
B) `s = 'Hello'`
C) `s = Hello`
D) Both A and B
Answer: B
2. What is the output of `print("Python"[0])`?
A) `P`
B) `y`
C) `Error`
D) `None`
Answer: A
A) `P`
B) `y`
C) `Error`
D) `None`
Answer: A
3. Which method is used to convert a string to uppercase?
A) `upper()`
B) `toUpper()`
C) `capitalize()`
D) `uppercase()`
Answer: A
A) `upper()`
B) `toUpper()`
C) `capitalize()`
D) `uppercase()`
Answer: A
4. What is the output of `"Hello".replace("H", "J")`?
A) `Hello`
B) `Jello`
C) `Error`
D) `Jello!`
Answer: B
A) `Hello`
B) `Jello`
C) `Error`
D) `Jello!`
Answer: B
5. How do you concatenate two strings `s1` and `s2`?
A) `s1 + s2`
B) `s1 & s2`
C) `s1 . s2`
D) `s1 << s2`
Answer: A
A) `s1 + s2`
B) `s1 & s2`
C) `s1 . s2`
D) `s1 << s2`
Answer: A
6. Which method is used to find the length of a string?
A) `length()`
B) `size()`
C) `len()`
D) `count()`
Answer: C
A) `length()`
B) `size()`
C) `len()`
D) `count()`
Answer: C
7. What will `len("abc")` return?
A) `2`
B) `3`
C) `4`
D) `None`
Answer: B
A) `2`
B) `3`
C) `4`
D) `None`
Answer: B
8. How do you access the last character of a string `s`?
A) `s[-1]`
B) `s[len(s)]`
C) `s[len(s)-1]`
D) Both A and C
Answer: D
A) `s[-1]`
B) `s[len(s)]`
C) `s[len(s)-1]`
D) Both A and C
Answer: D
9. Which method returns the substring of a string?
A) `slice()`
B) `substring()`
C) `str[]`
D) `None of the above`
Answer: C
A) `slice()`
B) `substring()`
C) `str[]`
D) `None of the above`
Answer: C
10. What is the result of `"Hello " + "World"`?
A) `HelloWorld`
B) `Hello World`
C) `Hello!World`
D) `Error`
Answer: B
A) `HelloWorld`
B) `Hello World`
C) `Hello!World`
D) `Error`
Answer: B
11. What does `"abc".find("a")` return?
A) `0`
B) `1`
C) `2`
D) `-1`
Answer: A
A) `0`
B) `1`
C) `2`
D) `-1`
Answer: A
12. How do you check if a string contains a substring?
A) `if substring in string`
B) `if string.contains(substring)`
C) `if string.has(substring)`
D) `None of the above`
Answer: A
A) `if substring in string`
B) `if string.contains(substring)`
C) `if string.has(substring)`
D) `None of the above`
Answer: A
13. Which method is used to split a string into a list?
A) `split()`
B) `divide()`
C) `cut()`
D) `break()`
Answer: A
A) `split()`
B) `divide()`
C) `cut()`
D) `break()`
Answer: A
14. What is the output of `"Hello".strip("H")`?
A) `ello`
B) `Hello`
C) `Hello!`
D) `Error`
Answer: A
A) `ello`
B) `Hello`
C) `Hello!`
D) `Error`
Answer: A
15. Which of the following creates a multi-line string?
A) `s = "Hello\nWorld"`
B) `s = '''Hello World'''`
C) `s = """Hello World"""`
D) All of the above
Answer: D
A) `s = "Hello\nWorld"`
B) `s = '''Hello World'''`
C) `s = """Hello World"""`
D) All of the above
Answer: D
16. What will `"Python"[1:4]` return?
A) `Pyt`
B) `yth`
C) `ytho`
D) `ytho`
Answer: B
A) `Pyt`
B) `yth`
C) `ytho`
D) `ytho`
Answer: B
17. Which of the following is not a string method?
A) `capitalize()`
B) `lower()`
C) `size()`
D) `replace()`
Answer: C
A) `capitalize()`
B) `lower()`
C) `size()`
D) `replace()`
Answer: C
18. What does the expression `"abc" * 3` evaluate to?
A) `abcabcabc`
B) `abcabc`
C) `Error`
D) `abcabcabcabc`
Answer: A
A) `abcabcabc`
B) `abcabc`
C) `Error`
D) `abcabcabcabc`
Answer: A
19. How can you convert a number to a string?
A) `str(number)`
B) `string(number)`
C) `convert(number)`
D) `None of the above`
Answer: A
A) `str(number)`
B) `string(number)`
C) `convert(number)`
D) `None of the above`
Answer: A
20. What is the output of `print("abc".count("a"))`?
A) `1`
B) `2`
C) `0`
D) `Error`
Answer: A
A) `1`
B) `2`
C) `0`
D) `Error`
Answer: A
21. Which method checks if a string starts with a specific substring?
A) `startswith()`
B) `beginswith()`
C) `starts()`
D) `first()`
Answer: A
A) `startswith()`
B) `beginswith()`
C) `starts()`
D) `first()`
Answer: A
22. How do you escape a quote inside a string?
A) `\'`
B) `""`
C) `\`
D) `""`
Answer: A
A) `\'`
B) `""`
C) `\`
D) `""`
Answer: A
23. Which of the following will return a boolean?
A) `"abc" == "abc"`
B) `"abc" != "xyz"`
C) Both A and B
D) `None of the above`
Answer: C
A) `"abc" == "abc"`
B) `"abc" != "xyz"`
C) Both A and B
D) `None of the above`
Answer: C
24. How do you format a string in Python?
A) `format()`
B) `f"{variable}"`
C) `string.format(variable)`
D) All of the above
Answer: D
A) `format()`
B) `f"{variable}"`
C) `string.format(variable)`
D) All of the above
Answer: D
25. What does `str.isdigit()` do?
A) Checks if the string is numeric
B) Converts string to integer
C) Returns the length of the string
D) None of the above
Answer: A
A) Checks if the string is numeric
B) Converts string to integer
C) Returns the length of the string
D) None of the above
Answer: A