Python · Level 2 Text

Slicing

Cut out part of a string or list with [start:stop], and reverse it with [::-1].

  • [start:stop] stops before stop
  • Leaving out start or stop
  • [::-1] reverses

text[start:stop] takes the part from start up to, but not including, stop. Leave one out to go to the edge.

word = "Python"
print(word[0:2])    # Py
print(word[:3])     # Pyt
print(word[-2:])    # on
print(word[::-1])   # nohtyP

Write preview(text, n) that returns the first n characters followed by … when the text is longer than n, or the text unchanged otherwise.

solution.py
Test Suites Pending
Stdout