Remove the First N characters from String in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
5 min

banner

# Table of Contents

  1. Remove the first N characters from a String in Python
  2. Remove the first N characters if they are equal to a certain value
  3. Remove the first N characters from a String using str.replace()
  4. Remove the first N characters from a String using a for loop
  5. Remove the first N characters from a string using lstrip

# Remove the first N characters from a String in Python

Use string slicing to remove the first N characters from a string, e.g. new_string = string[N:].

The slice will remove the first N characters from the string by returning a copy of the string that starts at the character after the first N characters.

main.py
string = 'bobbyhadz.com' # โœ… Remove the first 2 characters from a string new_string = string[2:] print(new_string) # ๐Ÿ‘‰๏ธ bbyhadz.com # โœ… Remove the first 3 characters from a string new_string = string[3:] print(new_string) # ๐Ÿ‘‰๏ธ byhadz.com # โœ… Remove the first 4 characters from a string new_string = string[4:] print(new_string) # ๐Ÿ‘‰๏ธ yhadz.com

remove first n characters from string

The code for this article is available on GitHub

We used string slicing to remove the first N 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 string[2:] starts at index 2 and goes to the end of the string.

main.py
string = 'bobbyhadz.com' new_string = string[2:] print(new_string) # ๐Ÿ‘‰๏ธ bbyhadz.com

In other words, the slice removes the first 2 characters of the string (indices 0 and 1).

If you don't need to keep the original variable around, reassign it rather than declaring a new variable.

main.py
string = 'bobbyhadz.com' string = string[3:] print(string) # ๐Ÿ‘‰๏ธ byhadz.com

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

In other words, it removes the first 3 characters from the string (indices 0, 1 and 2).

# Remove the first N characters if they are equal to a certain value

If you only want to remove the first N characters from a string if they are equal to a certain value, use the str.startswith() method.

main.py
string = 'bobbyhadz.com' substring = 'bob' if string.startswith(substring): string = string[len(substring):] print(string) # ๐Ÿ‘‰๏ธ byhadz.com

remove first n characters if they equal certain value

The code for this article is available on GitHub

The example only removes the first 3 characters from the string if the string starts with the specified characters.

The str.startswith() method returns True if the string starts with the provided prefix, otherwise the method returns False.

You can also use string slicing if you need to get the two parts of the string.

main.py
string = 'bobbyhadz.com' n = 3 before, after = string[:n], string[n:] print(before) # ๐Ÿ‘‰๏ธ bob print(after) # ๐Ÿ‘‰๏ธ byhadz.com

The slice string[:3] starts at index 0 and goes up to, but not including index 3.

In other words, it returns the first 3 characters in the string (indices 0, 1 and 2).

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

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

# Remove the first N characters from a String using str.replace()

This is a three-step process:

  1. Use string slicing to get the first N characters of the string.
  2. Use the str.replace() method to remove the first N characters from the string.
  3. Remove the characters by replacing them with an empty string.
main.py
string = 'bobbyhadz.com' n = 2 new_string = string.replace(string[:n], '', 1) print(new_string) # ๐Ÿ‘‰๏ธ bbyhadz.com

remove first n characters from string using str replace

The code for this article is available on GitHub

We used the str.replace() method to remove the first N characters from a string by replacing them with an empty string.

By default, the str.replace() method replaces all occurrences of the provided substring in the string, so we specified a count of 1 to make sure we only replace the first occurrence.

The str.replace() method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.

The method takes the following parameters:

NameDescription
oldThe substring we want to replace in the string
newThe replacement for each occurrence of old
countOnly the first count occurrences are replaced (optional)

The method doesn't change the original string. Strings are immutable in Python.

Alternatively, you can use a for loop.

# Remove the first N characters from a String using a for loop

This is a three-step process:

  1. Use a for loop to iterate over the string with enumerate().
  2. Check if the index is greater than or equal to N.
  3. If the condition is met, append the current character to a new string.
main.py
string = 'bobbyhadz.com' new_string = '' n = 3 for index, char in enumerate(string): if index >= n: new_string += char print(new_string) # ๐Ÿ‘‰๏ธ 'byhadz.com'

remove first n characters from string using for loop

The code for this article is available on GitHub

We used the enumerate() function to get access to the index of the current iteration.

main.py
string = 'abc' for index, char in enumerate(string): print(char, index) # ๐Ÿ‘‰๏ธ a 0, b 1, c 2

The enumerate() function takes an iterable and returns an enumerate object containing tuples where the first element is the index and the second is the corresponding item.

On each iteration, we check if the current index is greater than or equal to N.

If the condition is met, we add the current character to a new string.

The += operator is a shorthand for new_string = new_string + char.

# Remove the first N characters from a string using lstrip

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

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'

remove first n characters using lstrip

The code for this article is available on GitHub

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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev