Remove the first character from a String in Python

avatar

Borislav Hadzhiev

Last updated: Aug 13, 2022

banner

Photo from Unsplash

Remove the first character from a String in Python #

Use string slicing to remove the first character from a string, e.g. result = my_str[1:]. The new string will contain a slice of the original string with the first character removed.

main.py
my_str = 'melon' result_1 = my_str[1:] print(result_1) # 👉️ 'elon' result_2 = my_str.lstrip(my_str[0]) print(result_2) # 👉️ 'elon' result_3 = my_str.lstrip('m') print(result_3) # 👉️ 'elon' # ✅ if you need to remove the first word from a string my_str_2 = 'hello world' result_4 = my_str_2.split(' ', 1)[1] print(result_4) # 👉️ 'world'

The first example uses string slicing to remove the first character from a string.

The syntax for string slicing is my_str[start:stop:step].

The start index is inclusive, whereas the stop index is exclusive (up to, but not including).

Python indexes are zero-based, so the first character in a string has an index of 0, and the last character has an index of -1 or len(my_str) - 1.

The slice my_str[1:] starts at the character at index 1 and goes to the end of the string.

main.py
print('test'[1:]) # 👉️ est print('melon'[1:]) # 👉️ elon print('world'[1:]) # 👉️ orld

If you use this approach with an empty string, you will get an empty string back.

main.py
print(''[1:]) # 👉️ ''

No error is raised even though the string in the example is empty.

Alternatively, you can use the str.lstrip() method.

Use the str.lstrip() method to remove the first character from a string, e.g. result = my_str.lstrip(my_str[0]). The str.lstrip() method will return a new string with the specified leading character removed.

main.py
my_str = 'melon' result_1 = my_str.lstrip(my_str[0]) print(result_1) # 👉️ 'elon' result_2 = my_str.lstrip('m') print(result_2) # 👉️ 'elon'

The first example uses an index of 0 to get the first character of the string.

The str.lstrip method takes a string containing characters as an argument and returns a copy of the string with the specified leading characters removed.

The method does not change the original string, it returns a new string. Strings are immutable in Python.

Note that accessing the string at an index that is out of range would raise an error.

main.py
my_str = '' # ⛔️ IndexError: string index out of range result_1 = my_str.lstrip(my_str[0]) print(result_1) # 👉️ 'elon'

If you need to handle this scenario, use a try/except statement.

main.py
my_str = '' try: result_1 = my_str.lstrip(my_str[0]) print(result_1) # 👉️ 'elon' except IndexError: print('The specified string is empty')
Note that the str.lstrip() method doesn't necessarily remove a single character.

The method removes all occurrences and combinations of the specified character from the start of the string.

Use the str.split() method if you need to remove the first word from a string.

main.py
my_str = 'hello world' result = my_str.split(' ', 1)[1] print(result) # 👉️ 'world'

We split the string on a space a single time and returned the second list item.

You can also use the str.removeprefix() method to remove the first character from a string.

main.py
my_str = 'melon' result = my_str.removeprefix('m') print(result) # 👉️ 'elon'

The str.removeprefix method checks if the string starts with the specified prefix and if it does, the method returns a new string excluding the prefix, otherwise it returns a copy of the original string.

The removeprefix method can also be used to remove more than 1 character from the start of the string.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.