Search notes:

Python: taking substrings from a string

A substring can be extracted from a string with brackets. The characters are zero-indexed, so text[n] gets the character at position n+1.
The optional second number (after the colon) is exclusively, so text[n:m] gets the characters at position n+1 through m.
If the numbers are negative, they count from the end of the string towards the beginning.
#!/usr/bin/python3

txt = '0123456789'

print(txt[ 3  ]) # 4th character                - 3
print(txt[ 3:7]) # From 4th to 7th character    - 3456
print(txt[ 7: ]) # From 8th character to end    - 789
print(txt[-2  ]) # 2nd last character           - 8
print(txt[-4: ]) # 4th last character to end    - 6789
Github repository about-python, path: /built-in/types/str/substring.py

See also

str.rstrip() and remove last n characters from a string
strings

Index