Python Strings · Question 10 · Medium
split() is called once with a comma separator and once with no argument. What are the two lengths?
a = "a,b,,c".split(",")
b = "a b c".split()
print(len(a), len(b))
- 3 3
- 4 4
- 4 3
- 3 4
Answer and explanation
Answer: C. 4 3
Explanation: With an explicit separator, two commas in a row produce an empty string, so the first list is ['a', 'b', '', 'c'] with 4 items. With no argument, split() treats any run of whitespace as one separator, giving ['a', 'b', 'c'].
Previous: replace() is called on a string and both the old and new names are printed… · Next: A list of integers is passed to join(). What happens when this runs?
Practise python strings
Try a fresh set of python strings questions with an explanation after every answer, free and without signing up: Python Strings practice. Or sit a full placement mock test.
More python strings questions
- In the slice s[a:b] of a string s, with a less than b, which characters are…
- A slice with a step of 3 is taken from a city name. What does it print?
- replace() is called on a string and both the old and new names are printed…
- A list of integers is passed to join(). What happens when this runs?
- Format specifiers are used inside two f-strings. What does this print?
- A string literal contains a tab escape and an escaped backslash. How many…
- Which statement about Python strings is true?
- A slice runs past the end of a six-letter string. What does this print?
- A slice with negative start and negative step is taken from a string. What is…
- split() and rsplit() are each limited to one split. What does this print?
More python practice
Lists & Tuples · Dictionaries & Sets · Conditions & Loops · Functions & Scope · Comprehensions & Generators · Classes & Objects · Exceptions & Files · Built-ins & Standard Library
LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge