. Advertisement .
..3..
. Advertisement .
..4..
Need to remove first character from Python string? This guide will show you three different approaches to executing this task. Let’s check it out for further details!
Keep in mind that a Python string is immutable, meaning that changes cannot be made. As a result, to delete a character from a string, a new one will be created without the initial character that was removed.
Python strings can include delimiters, alphabets, integers, etc. You may remove the initial character from the strings with the built-in Python routines.
Method 1: Remove First Character From Python String Utilizing String Slicing Approach
In Python, strings are indexed much like other items. You can choose strings by utilizing the positions of their index, as the index starts at zero and rises from there. With the string slicing approach, you can specify precisely the amount of characters you might wish to remove from the string.
Suppose you want to delete the first character or a specific character from a string; you can use the slicing operator str[1:]
. This method will return the entire string minus the first character.
In the following example, we will delete the first character of the hhello string.
string = "hhello"
new_string = string[1:]
print(new_string)
Output:
hello
Notice that you won’t have to explicitly give the end index when slicing up to the end of the Python string.
Method 2: Remove First Character From Python String Utilizing str.lstrip()
The method str.lstrip()
removes one or more characters from the beginning of the string by taking these characters as input and then gives back the result with the characters removed. However, if the characters appear once or more at the beginning of a string, the method str.lstrip()
will eliminate them.
Observe the following sample code to see how the string’s beginning characters are trimmed utilizing the str.lstrip()
.
string = "Hhello world"
new_string = string.lstrip("H")
print(new_string)
string = "HHHHhello world"
new_string = string.lstrip("H")
print(new_string)
Output:
hello world
hello world
Method 3: Remove First Character From Python String Utilizing regex
The initial character in a string can easily be eliminated utilizing the method re.sub()
of the library re
. This re.sub()
approach will replace every character that matches the regular expression pattern parameter with the second one.
Here is an example to use as a reference.
import re
string = "Hhello world"
new_string = re.sub(r'.', '', string, count = 1)
print(new_string)
As you can see in this code, count = 1
indicates that the procedure re.sub only, and maximally, once, substitutes the specified pattern.
Output:
hello world
The Bottom Line
Above are the three ways to remove first character from Python string. These methods are quite simple to perform, so keep practicing, and you will soon master these techniques. Suppose you don’t need to remove the first but the special characters in a string; this detailed guide will show you how. In case you don’t want to remove but replace a character in a string, check out this tutorial.
Leave a comment