Python string remove the first character | Example code

There are 2 ways to remove the first character from a Python string.

Example string remove the first character in Python

Simple example code with different ways to remove the first char from string.

Using Slicing

This method will not modify the given string.

s = 'AHello'

res = s[1:]
print(res)

Output:

Python string remove the first character

If you want to remove the first n characters, then use this syntax:-

s = s[n:]

Using split() function

use the split function with join function. In this method, you have to know the char to want to remove from the string.

s = 'AHello!'
ch = 'A'

s = ''.join(s.split(ch, 1))
print(s)

Output: Hello!

Using lstrip() function

Same as above code needed char to want to remove.

s = 'AHello!'
ch = 'A'

s = s.lstrip(ch)
print(s)  

Do comment if you have any doubts and suggestions on this Python string topic.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.