Python String Format Quizzes
1. What is the output? print("python".upper())
A. PYTHON
B. python
C. Python
D. pYTHON
2. What is the output? print("PYTHON".lower())
A. pYthon
B. python
C. PYTHON
D. Python
3. What is the output? print("hello world".title())
A. Hello world
B. HELLO WORLD
C. hello world
D. Hello World
4. What is the output? print("hello".capitalize())
A. hello
B. HELLO
C. hELLO
D. Hello
5. What is the output? print("PyThOn".swapcase())
A. python
B. PyThOn
C. PYTHON
D. pYtHoN
6. What is the output? print("banana".find("na"))
A. 2
B. 4
C. 1
D. 3
7. What is the output? print("banana".count("a"))
A. 3
B. 4
C. 1
D. 2
8. What is the output? print("python".startswith("py"))
A. False
B. 0
C. None
D. True
9. What is the output? print("report.pdf".endswith(".pdf"))
A. None
B. False
C. True
D. pdf
10. What is the output? print("hello world".replace("world", "Python"))
A. helloPython
B. hello Python
C. hello world
D. Python world
11. What is the output? print("a,b,c".split(","))
A. ('a', 'b', 'c')
B. a b c
C. ['a', 'b', 'c']
D. ['a,b,c']
12. What is the output? print("-".join(["2026", "09", "12"]))
A. 2026/09/12
B. 20260912
C. 2026-09-12
D. ['2026-09-12']
13. What is the output? print("Python123".isalnum())
A. True
B. False
C. Python
D. 123
14. What is the output? print("12345".isdigit())
A. True
B. 5
C. 12345
D. False
15. What is the output? print("abc".center(7, "-"))
A. abc----
B. abc
C. ---abc-
D. --abc--
16. What is the output? print("42".zfill(5))
A. 00042
B. 42000
C. 0042
D. 42.000
17. What is the output? print("abc123".isalpha())
A. False
B. abc
C. 123
D. True
18. What is the output? print("Hello".find("x"))
A. -1
B. None
C. 0
D. 1
19. What is the output? print("hello\tworld".expandtabs(4))
A. hello\tworld
B. helloworld
C. hello world
D. hello world
1 comment:
What is the output?
print("python".upper())
The upper method converts all alphabetic characters to uppercase.
What is the output?
print("PYTHON".lower())
The lower method converts all alphabetic characters to lowercase.
What is the output?
print("hello world".title())
Title capitalizes the first character of each word.
What is the output?
print("hello".capitalize())
Capitalize makes the first character uppercase and the remaining characters lowercase.
What is the output?
print("PyThOn".swapcase())
Swapcase changes uppercase characters to lowercase and lowercase characters to uppercase.
What is the output?
print("banana".find("na"))
The first na starts at zero-based index 2.
What is the output?
print("banana".count("a"))
There are three a characters in banana.
What is the output?
print("python".startswith("py"))
The string begins with the characters py.
What is the output?
print("report.pdf".endswith(".pdf"))
The final four characters are .pdf.
What is the output?
print("hello world".replace("world", "Python"))
The substring world is replaced with Python.
What is the output?
print("a,b,c".split(","))
split with comma separates the string at each comma.
What is the output?
print("-".join(\["2026", "09", "12"\]))
join places the specified hyphen between each list element.
What is the output?
print("Python123".isalnum())
The string contains only letters and digits, so it is alphanumeric.
What is the output?
print("12345".isdigit())
Every character in the string is a digit.
What is the output?
print("abc".center(7, "-"))
A width of 7 requires four padding characters around abc; centering distributes them as two on each side.
What is the output?
print("42".zfill(5))
zfill pads the string on the left with zeros to reach width 5.
What is the output?
print("abc123".isalpha())
isalpha requires every character to be alphabetic; 123 prevents that.
What is the output?
print("Hello".find("x"))
find returns -1 when the requested substring is not found.
What is the output?
print("hello\\tworld".expandtabs(4))
With the tab after five characters and tab size 4, expansion reaches the next tab stop, requiring three spaces.
Post a Comment