We saw how to access individual characters of a string with
. It’s also possible to select a specific slice of a string - take several consecutive characters. This can be accomplished with the same
brackets but by providing two numbers - the start and the end of the selection:
s = 'This is a long long string' print(s[0: 1]) # T print(s[0: 2]) # Th print(s[0: 18]) # This is a long lon print(s[1: 3]) # hi print(s[1: -1]) # his is a long long strin print(s[3: -2]) # s is a long long stri print(s[-10: -2]) # ong stri
Note 1: The start is always inclusive and the end is always exclusive ⇒
Note 2: Both start and end can be negative - implying negative indexing.
We should like to know what’s the central segment of a string.
Given a string of length
n, we would like to see the letters that span the positions from
The input contains a single line of text, which is guaranteed to have a length divisible by 4.
The program should print the central segment.