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.
This method will not modify the given string.
s = 'AHello' res = s[1:] print(res)
If you want to remove the first
n characters, then use this syntax:-
s = s[n:]
Using split() function
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)
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)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.