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?

  1. trim()
  2. rstrip()
  3. split()
  4. 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?

  1. s.sort() arranges the characters of s in place
  2. Methods such as upper() return a new string and leave the original unchanged
  3. s[1] = 'x' replaces the second character of s
  4. 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])
  1. edcb
  2. fedc
  3. ed
  4. 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

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

Other subjects

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