Python

Remove First Character from String Python

This python topic is all about removing the first character from a string in Python. We explore different tactics to remove the first character Python string. As we already know, the python string is unassailable, which means we cannot make variations in a Python string. Therefore, to delete the first character from the Python string, we will make a new string that will not contain the first character we wanted to remove. Python string contains integers, alphabets, delimiters, etc. To delete the first char from the Python string, we use Python built-in functions and illustrate them via Windows 10 Spyder Compiler.

Example 1

Python provides various inbuilt functions, Slicing() is one of them. If we wish to delete the first character or some other char from the python string, we can erase that character using the slicing method and then get the resultant string excluding the first character.

To understand the concept of the slicing() function, simply open the source code file and implement the program code. So, in our code illustration, we first create a naming string. We can then define a format that eliminates the first character. Afterward, we use the print statement to print the whole string, excluding the first character. The simplest code of this illustration is attached here.

first_string = "John"
sli_string = first_string.lstrip[1: ]
print(sli_string)

Let’s dry the code and check how slicing() slice the first character from the string.

Example 2

As we already know, Python provides various inbuilt functions, str.lstrip() is one of them. Except for slicing, lstrip also plays its role to remove a character from the string and trim the string. In this program, we explain our next unique lstrip() method. The str.lstrip() command gets character input, then deletes them from the beginning of the string, and as a result, it returns a new filtered string with erased characters. But keep in mind that the str.lstrip() method will discard the character(s) if they exist multiple times at the starting point of the string.

Our below program code demonstrates the whole process of lstrip(). Initially, we created a string of toys. We can then define the lstrip() format and pass our first character as an input to eliminate it from the given string. Afterward, we use the print statement to print the whole string, excluding the first character. Here we also demonstrate the second condition of lstrip(). We again create a new string and do the same steps just like we did earlier, but this time multiple characters occur at the start of the string. The simplest code of this illustration is attached here.

first_string = "John"
flt_string = first_string.lstrip("J")
print(flt_string)

second_string = "JJJJJJohn Alex"
new_str = second_string.lstrip("J")
print(new_str)

Let’s run the code and check how lstrip() removes the first character or its repetition from the string.

Example 3

After the slicing() and str.lstrip() method, now it’s time to explain the regex() method. The re.sub() is present in the ‘re’ class of the regex module. It can also be utilized to erase the first character. However, the approach exchanges all the characters that match the specified regular expression format with the second parameter. Just like other methods, regex also does the same thing but in a diverse way.

So, we are going to first import the re library so that the function works well as it resides in the re module. After that, we create a string just like we did in our above two program codes. Then we call the re.sub() command and pass string and count variable as a parameter. Here we use ‘count=1’, which specifies that character removes or string trim only one time. Same as we declare a print statement that prints out the trimmed string. The simplest code of this illustration is attached here.

import re

first_string = "Python world"
second_string = re.sub(r'.', '', first_string, count = 1)
print(second_string)

After successful execution of the program code, the trimmed string is displayed on the screen.

Conclusion

This article was all about removing characters from any python string. We have stated three distinctive methods. I hope you find all the aforementioned methods very useful and helpful. All methods are very simple and easy to understand and implement, so give a try to each of them.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content