Python String Methods — Output Quiz
1. What is the output? s = "PyThOn" print(s.swapcase())
A. PYTHON
B. python
C. pYtHoN
D. PyThOn
2. What is the output? s = "banana" print(s.find("an"), s.rfind("an"))
A. 1 4
B. 1 3
C. 0 3
D. 2 4
3. What is the output? s = "Python Python" print(s.replace("Python", "Java", 1))
A. Python Python
B. Java Python
C. Python Java
D. Java Java
4. What is the output? s = "one two three" print(s.split(" ")[1])
A. one
B. three
C. two
D. two three
5. What is the output? print("abc".isalpha(), "123".isdigit(), "abc123".isalnum())
A. True True True
B. True False True
C. False True False
D. True True False
6. What is the output? print("Hello".islower(), "HELLO".isupper(), " ".isspace())
A. False False True
B. True True False
C. True False False
D. False True True
7. What is the output? print("hi".center(6, "-"))
A. -hi---
B. hi----
C. ---hi-
D. --hi--
8. What is the output? print("42".zfill(5), "7".rjust(3, "0"))
A. 42000 700
B. 00042 007
C. 00042 700
D. 42 7
9. What is the output? s = "a-b-c" print(s.partition("-")) print(s.rpartition("-"))
A. ('a', '-', 'b-c') and ('a-b', '-', 'c')
B. ['a', '-', 'b-c'] and ['a-b', '-', 'c']
C. ('a-b', '-', 'c') and ('a', '-', 'b-c')
D. ('a', 'b', 'c') and ('a', 'b', 'c')
1 comment:
What is the output?
s = "PyThOn" print(s.swapcase())
swapcase() changes each uppercase letter to lowercase and each lowercase letter to uppercase.
What is the output?
s = "banana" print(s.find("an"), s.rfind("an"))
The first an starts at index 1, while the last an starts at index 3.
What is the output?
s = "Python Python" print(s.replace("Python", "Java", 1))
replace() with a count of 1 changes only the first matching occurrence.
What is the output?
s = "one two three" print(s.split(" ")\[1\])
Splitting on spaces produces one, two, three; index 1 is two.
What is the output?
print("abc".isalpha(), "123".isdigit(), "abc123".isalnum())
abc contains only letters, 123 contains only digits, and abc123 contains only letters and digits.
What is the output?
print("Hello".islower(), "HELLO".isupper(), " ".isspace())
Hello is not entirely lowercase, HELLO is uppercase, and the third string contains only spaces.
What is the output?
print("hi".center(6, "-"))
center(6, '-') adds four padding characters around hi, giving a total width of six.
What is the output?
print("42".zfill(5), "7".rjust(3, "0"))
zfill(5) produces a five-character string with leading zeros, and rjust(3, '0') pads 7 to width three.
What is the output?
s = "a-b-c" print(s.partition("-")) print(s.rpartition("-"))
partition() splits at the first separator, while rpartition() splits at the last separator.
Post a Comment