How to Remove First Character from String in Python

Python strings are immutable, and to change a string, you need to create a new string object with the necessary modifications.

Remove first character from String in Python

To remove the first character from a string in Python, use the slicingThe str[1:] returns the whole string except the first character.

data = "MillyAlcock"
slicing = data[1:]
print(slicing)

Output

illyAlcock

You can see from the output that the first character is removed, and we get the remaining string in Python.

Removing the first character using the split() and join() methods

The split() is a built-in string function in Python that splits a string into a list. The join() is a built-in string method to join sequence elements separated by a string separator.

To remove the first character from a string using the split() and join() method, use the split() method to remove the first character and the use the join() method to join the elements into a string.

data = "Mabel"
char = "M"

str = "".join(data.split(char, 1))
print(str)

Output

abel

You can see that we removed the first character, “M”, is removed from a string.

Remove the first character using the lstrip() method

The lstrip() is a built-in Python method that removes any leading characters. The lstrip() function returns a copy of the string with leading characters removed.

Pass the first character as an argument to the lstrip() function, and it will return a string except for that first character because it is removed from a string.

data = "Mabel"
char = "M"

str = data.lstrip(char)
print(str)

Output

abel

You can see that the lstrip() function takes the first character as an argument and the output string is without that first character.

Removing the first character from a string using regex in Python

To use regular expression in Python, use the re module. The re module has one method called re.sub(), which can help us remove the first character from a string.

The sub() method searches for the pattern in the string, and we will replace the pattern with an empty character, resulting in the string with the first character being removed.

import re

data = "Mabel"
str = re.sub(r'.', '', data, count=1)
print(str)

Output

abel

You can see that the re.sub() method removes the first character from the string by searching the pattern and replacing it with an empty character.

Remove the specific character from the string in Python

To remove the specific character from the string in Python, use the string.replace() method. Replace the specific character with an empty character using replace() method and return the output string.

data = "Houseofthedragon"
str = data.replace("d", "")
print(str)

Output

Houseoftheragon

The replace() method accepts the character to be replaced; in our case, it is the character “d”. We are replacing the character “d” with an empty character, and in the output, you can see that the “d” is removed from the main string.

Removing the first n character from the string in Python

To remove the first n characters from a string, use Python slicing.

data = "Houseofthedragon"
n = 5
print(data[n:])

Output

ofthedragon

We wanted to remove the first five characters from the string in this example. So, we took n = 5, and then using slicing, we removed the first five characters from the string.

Conclusion

Use slicing to remove the first character or first n characters. Use the replace() method to remove the particular character from the string. Removing the first character, n characters, or specific characters, Python provides lots of functions or a combination of functions that can help us achieve the necessary result.

That’s it for this article.

Related posts

Remove the last character from a string

Removing character from string

Remove whitespace from a string

Python trim string

Leave a Comment

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