Removing first x characters from string?

PythonString

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

>>> 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.

Solution 2 - Python

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

Solution 3 - Python

>>> x = 'lipsum'
>>> x.replace(x[:3], '')
'sum'

Solution 4 - Python

Use del.

Example:

>>> text = 'lipsum'
>>> l = list(text)
>>> del l[3:]
>>> ''.join(l)
'sum'

Solution 5 - Python

Example to show last 3 digits of account number.

x = '1234567890'   
x.replace(x[:7], '')

o/p: '890'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontkbxView Question on Stackoverflow
Solution 1 - PythonjamylakView Answer on Stackoverflow
Solution 2 - PythonKen AView Answer on Stackoverflow
Solution 3 - PythontkbxView Answer on Stackoverflow
Solution 4 - PythonU12-ForwardView Answer on Stackoverflow
Solution 5 - PythonPratik JaswantView Answer on Stackoverflow