Python Strings questions and answers
Indexing, slicing, methods, formatting and immutability. Practise python strings questions from the python section of placement tests, with a worked explanation after every answer. Practice is free and needs no sign-up.
Sample python strings questions with worked answers
Question 1
Which string method removes whitespace from both ends of a string?
- trim()
- rstrip()
- split()
- strip()
Answer: D. strip()
Explanation: strip() returns a copy without leading and trailing whitespace. rstrip() removes only from the right end, split() breaks the string into a list, and trim() is a Java method that does not exist in Python.
Question 2
Which statement about Python strings is true?
- s.sort() arranges the characters of s in place
- Methods such as upper() return a new string and leave the original unchanged
- s[1] = 'x' replaces the second character of s
- A string in single quotes has a different type from one in double quotes
Answer: B. Methods such as upper() return a new string and leave the original unchanged
Explanation: Strings are immutable, so no method or index assignment can change one in place; methods return new strings. Strings have no sort() method, and both quote styles create the same str type.
Question 3
A slice with negative start and negative step is taken from a string. What is printed?
s = "abcdef"
print(s[-2:1:-1])
- edcb
- fedc
- ed
- edc
Answer: D. edc
Explanation: Index -2 is 'e' (index 4). With step -1 the slice moves left and stops before index 1, so it takes indexes 4, 3 and 2: 'e', 'd', 'c'.
All 20 python strings questions with answers
- A city name is indexed from the front and from the back. What does this print?
- The length of a string is used to reach its last character. What is printed?
- Two case-changing methods are called on string literals. What is the output?
- Which string method removes whitespace from both ends of a string?
- The in operator tests two substrings of a city name. What does this print?
- An f-string is used to report Asha's marks. What exactly is printed?
- 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…
- split() is called once with a comma separator and once with no argument. What…
- 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?
- strip() is given a string of characters as its argument. What does this print?
- Three pairs of strings are compared with the less-than operator. What is the…
- Two step slices of a word are joined, the second one reversed. What does this…
Python Strings: common questions
What are python strings questions?
Indexing, slicing, methods, formatting and immutability. They appear in the python section of many placement tests.
Where can I practise python strings questions with answers for free?
Right here on LogicWiz Exams. Every python strings question has a worked explanation after you answer. Practice is free and needs no sign-up.
How should I practise python strings?
Start with easy questions, then move to medium and hard. Read the explanation after each answer, and come back to the ones you missed.
Start practising
On this page you can start a fresh set of python strings questions, choose easy, medium or hard, and time yourself if you want to. Or practise the whole subject: Python practice.
More python topics
- Variables, Types & Operators: Dynamic typing, numbers, operators, precedence and truthiness.
- Lists & Tuples: Slicing, methods, copies, sorting and unpacking.
- Dictionaries & Sets: Lookups, iteration, hashable keys and set operations.
- Conditions & Loops: if/elif, for, while, range, break, continue and loop else.
- Functions & Scope: Arguments, defaults, *args, closures, lambda and recursion.
- Comprehensions & Generators: List, dict and set comprehensions, yield and lazy evaluation.
- Classes & Objects: Attributes, inheritance, super(), dunder methods and properties.
- Exceptions & Files: try/except/else/finally, raising errors and the with statement.
- Built-ins & Standard Library: sorted, zip, any/all, collections, itertools and imports.
Other subjects
- Quantitative Aptitude: Arithmetic, percentages, time and work, speed, probability and data interpretation: the numerical section of every fresher test.
- Logical Reasoning: Series, coding-decoding, arrangements, syllogisms and puzzles: the section that separates prepared candidates from the rest.
- Verbal Ability: Vocabulary, grammar, reading comprehension and sentence order: tested in nearly every IT services hiring exam.
- Technical & Programming: CS fundamentals, output prediction and pseudo-code: the technical MCQ round for IT roles.
- SQL: SQL quizzes by topic, from SELECT and joins to window functions: query output and concepts for analyst and developer roles.
LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge · Test from a job description · LogicWiz Open: free team quiz · Free interview prep