Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing first x characters from string?

Tags:

python

string

People also ask

How do I remove the first X from a string in Python?

You can use Python's regular expressions to remove the first n characters from a string, using re's . sub() method. This is accomplished by passing in a wildcard character and limiting the substitution to a single substitution.

How do I remove the first 5 characters of a string?

Strings are immutable in JavaScript. Alternatively, you can use the slice() method. Use the String. slice() method to remove the first N characters from a string, e.g. const result = str.

How do I remove the first two characters from a string?

To remove the first 2 characters from a string, use the slice method, passing it 2 as a parameter, e.g. str. slice(2) . The slice method returns a new string containing the specified portion of the original string.


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


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

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

Use del.

Example:

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

Example to show last 3 digits of account number.

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

o/p: '890'