Strings Questions with Answers

41 practice questions on Strings (Python) with answers and step-by-step solutions. Try 10 samples below, then practise all 41 free in the AptiNest app.

Sample questions

Question 1Easy

What is the output of: s = "AptiNest"; print(s[-4:])

  1. Nest
  2. Apti
  3. test
  4. Error

Answer: A. Nest

Explanation

s[-4:] slices the last four characters, which is Nest.

Step-by-step solution

  1. The string "AptiNest" has 8 characters.
  2. The slice s[-4:] starts 4 characters from the end and runs to the end.
  3. The last four characters are N, e, s, t, which give "Nest".
  4. So the output is Nest, which is option A.

Why the other options are wrong

  • B. "Apti" is the first four characters, not the last four.
  • C. "test" is not part of the string; the last four characters are "Nest" with a capital N.
  • D. Negative slice indexes are valid in Python, so no error happens.

Tip: s[-n:] gives the last n characters of a string.

Question 2Easy

What is the output?

print("Hello"[-1], "Hello"[-5], "Hello"[1:-1])
  1. o H ello
  2. l H ell
  3. o H ell
  4. o e ell

Answer: C. o H ell

Explanation

Negative indices count from the end (-1 is the last character, -5 the first); [1:-1] drops the first and last characters.

Step-by-step solution

  1. Index -1 counts from the end, so "Hello"[-1] is o.
  2. Index -5 is five places from the end, which is the first character, H.
  3. The slice 1:-1 starts at index 1 and stops before the last character, giving ell.
  4. The output is o H ell.

Why the other options are wrong

  • A. The slice 1:-1 stops before the last character, so the result is ell, not ello.
  • B. Index -1 is the last character, o, not l.
  • D. Index -5 is the first character, H, not e.

Tip: Negative indices count from the end, starting at -1; a slice end of -1 leaves out the last character.

Question 3Easy

What is the output?

print("a-b-c".partition("-"), "abc".partition("x"))
  1. ('a', '-', 'b-c') ('abc', '', '')
  2. ('a', '-', 'b', '-', 'c') ('abc', '', '')
  3. ('a', '-', 'b-c') ('', '', 'abc')
  4. ('a', 'b-c') ('abc',)

Answer: A. ('a', '-', 'b-c') ('abc', '', '')

Explanation

partition splits at the first separator into a 3-tuple; if the separator is missing it returns (s, "", "").

Step-by-step solution

  1. partition splits at the first separator into 3 parts.
  2. 'a-b-c' -> ('a','-','b-c').
  3. If missing -> ('abc','','').

Why the other options are wrong

  • B. partition does only one split.
  • C. When missing, the whole string comes first.
  • D. It always returns a 3-tuple.

Tip: 3 parts always.

Question 4Easy

What is the output?

print("abc"[::-1])
  1. cba
  2. a
  3. abc
  4. c

Answer: A. cba

Explanation

A step of -1 walks the string backwards, which reverses it.

Step-by-step solution

  1. [::-1] uses a step of -1, which walks the string backwards.
  2. Abc read backwards is cba.
  3. The output is cba, the correct answer.

Why the other options are wrong

  • B. A is wrong; a step of -1 keeps every character.
  • C. Abc is wrong; the string is reversed.
  • D. C is wrong; the whole string is reversed, not just one character.

Tip: s[::-1] is a common way to reverse a string or a list.

Question 5Medium

What is the output?

print("A1b2".translate(str.maketrans("ab", "xy", "12")))
  1. Axby
  2. A1y2
  3. xy
  4. Ay

Answer: D. Ay

Explanation

maketrans maps a->x and b->y and deletes 1 and 2; uppercase A is not in the table so it stays.

Step-by-step solution

  1. Maketrans maps a to x and b to y, and deletes the digits 1 and 2.
  2. The input A1b2 has A, 1, b, 2 in order.
  3. A is not in the table so it stays, the 1 and 2 are deleted, and b becomes y.
  4. The output is Ay.

Why the other options are wrong

  • A. The digits 1 and 2 are deleted by the third argument, so they do not appear.
  • B. The digit 1 is deleted and b becomes y, so the result has no 1 or 2.
  • C. A is not in the table, so it stays and the result is not just xy.

Tip: str.maketrans with three arguments maps the first set to the second and deletes the characters in the third.

Question 6Medium

What is the output?

print("abc" in "xabcx", "" in "abc", "ac" in "abc")
  1. True True False
  2. True True True
  3. False True False
  4. True False False

Answer: A. True True False

Explanation

in on strings tests for a contiguous substring; the empty string is in every string.

Step-by-step solution

  1. Abc appears inside xabcx as a contiguous block, so the first test is True.
  2. The empty string is part of every string, so the second test is True.
  3. Ac is not a contiguous block in abc, because b sits between a and c, so the third test is False.
  4. The output is True True False.

Why the other options are wrong

  • B. Ac is not a contiguous part of abc, so the last test is False.
  • C. Abc does appear inside xabcx, so the first test is True.
  • D. The empty string is in every string, so the second test is True.

Tip: The in operator on strings checks for a contiguous substring; the empty string always matches.

Question 7Medium

You need to build one string from 100,000 pieces in a loop. Which approach is recommended?

  1. Collect the pieces in a list and call "".join(list) once
  2. Use s.extend(piece)
  3. Use s = s + piece in the loop
  4. Use s.append(piece)

Answer: A. Collect the pieces in a list and call "".join(list) once

Explanation

Repeated concatenation may copy the growing string many times; join computes the size once and builds the result in one pass.

Step-by-step solution

  1. Adding to a string in a loop copies the growing string each time, which is slow.
  2. Collecting the pieces in a list and calling join once builds the result in one pass.
  3. This gives the expected string with a single allocation of the final size.
  4. The recommended approach is option A.

Why the other options are wrong

  • B. Str has no extend method.
  • C. Concatenating in a loop copies the string again on every step, which is slow for large inputs.
  • D. Str has no append method.

Tip: For many pieces, collect them in a list and use join once.

Question 8Medium

What is the output?

print("www.example.com".lstrip("w.moc"), "www.example.com".rstrip("w.moc"))
  1. .example.com www.example
  2. example.com www.example
  3. ample.com www.exa
  4. example.com www.example.

Answer: B. example.com www.example

Explanation

The argument is a set of characters, not a prefix: lstrip removes w, w, w, . and stops at e; rstrip removes m, o, c, . and stops at e.

Step-by-step solution

  1. lstrip('w.moc') removes leading characters from the set w, ., m, o and c: www. goes, then e stops it, which gives example.com.
  2. rstrip('w.moc') removes trailing characters from the set: m, o, c and. go, then e stops it, which gives www.example.
  3. So the output is example.com www.example, the correct answer.

Why the other options are wrong

  • A. .example.com www.example is wrong; the dot after www is removed too, so the first result has no leading dot.
  • C. ample.com www.exa is wrong; e is not in the set, so it stops the strip and stays.
  • D. example.com www.example. is wrong; the trailing dot is in the set, so rstrip removes it too.

Tip: Strip, lstrip and rstrip take a set of characters, not a prefix or suffix.

Question 9Hard

What is the output?

s = "banana"
print(s.count("an"), s.find("na", 3), s.rfind("a"))
  1. 3 4 5
  2. 2 2 5
  3. 2 4 1
  4. 2 4 5

Answer: D. 2 4 5

Explanation

'an' appears twice (indices 1 and 3). find('na', 3) starts searching at index 3 and finds 'na' at 4. rfind('a') returns the last index of 'a', which is 5.

Step-by-step solution

  1. count('an') finds 2 matches, at positions 1 and 3.
  2. find('na', 3) starts at index 3, and the first na from there is at index 4.
  3. rfind('a') gives the last index of the letter a, which is 5.
  4. The output is 2 4 5, the correct answer.

Why the other options are wrong

  • A. 3 4 5 is wrong; 'an' appears only twice.
  • B. 2 2 5 is wrong; the search starts at index 3, so the first na found is at 4.
  • C. 2 4 1 is wrong; the last a is at index 5, not 1.

Tip: Find, count and rfind take start or end positions, so check whether the start index is included.

Question 10Hard

Which call returns ["a", "b", "c"] for s = "a1b22c"?

  1. re.split(r"\d+", s)
  2. re.split(r"\d", s)
  3. s.partition("1")
  4. s.split("\d+")

Answer: A. re.split(r"\d+", s)

Explanation

The regex \d+ splits on runs of digits; splitting on single \d would give an empty string between "2" and "2", and str.split treats its argument literally.

Step-by-step solution

  1. re.split with the pattern one or more digits splits a1b22c at the 1 and the 22.
  2. That leaves a, b and c, with no empty items between the digit runs.
  3. Splitting on a single digit would give an empty item between 2 and 2.
  4. The answer is option A.

Why the other options are wrong

  • B. A single-digit pattern gives an empty string between the two 2s, so the result has an extra empty item.
  • C. Partition returns a three-item tuple, not a list of all the parts.
  • D. str.split takes the text literally, and that text is not in a1b22c, so no split happens.

Tip: Use re.split for patterns; use the plus sign to match runs of one or more digits.

Practise all 41 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Python topics · All topics