There are 2 ways to remove the first character from a Python string.
Example string removes the first character in Python
Simple example code with different ways to remove the first char from the string.
Using Slicing
This method will not modify the given string.
s = 'AHello'
res = s[1:]
print(res)
Output:
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)
Comment if you have any doubts or suggestions on this Python string topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.