Removing first x characters from string?


Question

How might one remove the first x characters from a string? For example, if one had a string lipsum, how would they remove the first 3 characters and get a result of sum?

1
98
8/4/2012 6:45:14 AM

Accepted Answer

>>> text = 'lipsum'
>>> text[3:]
'sum'

See the official documentation on strings for more information and this SO answer for a concise summary of the notation.

181
2/4/2018 10:50:39 AM

Another way (depending on your actual needs): If you want to pop the first n characters and save both the popped characters and the modified string:

s = 'lipsum'
n = 3
a, s = s[:n], s[n:]
print(a)
# lip
print(s)
# sum

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon