Remove the first two characters from a String in Python

avatar

Borislav Hadzhiev

Last updated: Aug 14, 2022

banner

Photo from Unsplash

Remove the first two characters from a String in Python #

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

main.py
my_str = 'avocado' result_1 = my_str[2:] print(result_1) # 👉️ 'ocado' result_2 = my_str.lstrip(my_str[0:2]) print(result_2) # 👉️ 'ocado' result_3 = my_str.lstrip('av') print(result_3) # 👉️ 'ocado' # ✅ remove the first two words from a string my_str_2 = 'one two three four five six' result_4 = my_str_2.split(' ', 2)[2] print(result_4) # 👉️ 'three four five six'

The first example uses string slicing to remove the first two characters 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[2:] starts at the character at index 2 and goes to the end of the string.

main.py
print('hello'[2:]) # 👉️ 'llo' print('world'[2:]) # 👉️ 'rld' print('test'[2:]) # 👉️ 'st'

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

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

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 two characters from a string, e.g. result = my_str.lstrip(my_str[0:2]). The str.lstrip() method will return a new string with the specified leading characters removed.

main.py
my_str = 'avocado' result_1 = my_str.lstrip(my_str[0:2]) print(result_1) # 👉️ 'ocado' result_2 = my_str.lstrip('av') print(result_2) # 👉️ 'ocado'

The first example uses a slice of 0:2 to get the first 2 characters 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 the str.lstrip() method doesn't necessarily remove only the first two characters from the string.

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

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

main.py
my_str = 'one two three four five six' result = my_str.split(' ', 2)[2] print(result) # 👉️ 'three four five six'

We split the string on a space 2 times and returned the third list item.

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

main.py
my_str = 'avocado' result = my_str.removeprefix(my_str[0:2]) print(result) # 👉️ 'ocado'

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 2 characters 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.