Python String Methods Quiz
1. What is the output? s = "Hello World" print(s.lower())
A. Hello World
B. hello world
C. Hello world
D. HELLO WORLD
2. What is the output? s = "banana" print(s.find("na"), s.rfind("na"))
A. 2 4
B. 1 4
C. 2 3
D. 1 3
3. What is the output? s = "hello" print(s.count("l"))
A. 1
B. 2
C. 0
D. 3
4. What is the output? s = " Python " print(s.strip())
A. 'Python '
B. 'Python'
C. ' Python'
D. ' Python '
5. What is the output? s = "a,b,c" print(s.split(","))
A. a-b-c
B. ['a,b,c']
C. ['a', 'b', 'c']
D. ['a', 'bc']
6. What is the output? words = ["Python", "is", "fun"] print("-".join(words))
A. ['Python-is-fun']
B. Python is fun
C. Python-is-fun
D. Python--is--fun
7. What is the output? s = "abc123" print(s.isalnum(), s.isalpha(), s.isdigit())
A. False True False
B. True True True
C. True False False
D. False False True
8. What is the output? s = "Python" print(s.startswith("Py"), s.endswith("on"))
A. True True
B. True False
C. False False
D. False True
9. What is the output? s = "hello world" print(s.title(), s.capitalize())
A. HELLO WORLD Hello world
B. Hello world Hello World
C. hello world Hello World
D. Hello World Hello world
10. What is the output? s = "42" print(s.zfill(5))
A. 42
B. 000042
C. 42000
D. 00042
1 comment:
What is the output?
s = "Hello World" print(s.lower())
lower() converts the alphabetic characters to lowercase.
What is the output?
s = "banana" print(s.find("na"), s.rfind("na"))
find() returns the first starting index and rfind() returns the last starting index; for banana these are 2 and 4.
What is the output?
s = "hello" print(s.count("l"))
count() counts the occurrences of the specified substring, and hello contains two l characters.
What is the output?
s = " Python " print(s.strip())
strip() removes the leading and trailing spaces, leaving Python.
What is the output?
s = "a,b,c" print(s.split(","))
split(',') separates the string at each comma and returns the resulting parts as a list.
What is the output?
words = \["Python", "is", "fun"\] print("-".join(words))
join() places the specified hyphen between each element of the iterable.
What is the output?
s = "abc123" print(s.isalnum(), s.isalpha(), s.isdigit())
abc123 contains only letters and digits, so isalnum() is True; because it contains both types, isalpha() and isdigit() are False.
What is the output?
s = "Python" print(s.startswith("Py"), s.endswith("on"))
Python begins with Py and ends with on, so both checks succeed.
What is the output?
s = "hello world" print(s.title(), s.capitalize())
title() capitalizes the first letter of each word, while capitalize() changes only the first character of the whole string.
What is the output?
s = "42" print(s.zfill(5))
zfill(5) pads the left side with zeros until the string has five characters.
Post a Comment