Skip to content

Python: Remove the First N Characters from a String

Python Remove First n Characters from a String Cover Image

In this tutorial, you’ll learn how to remove the first n characters from a string in Python. You’ll learn how to do this using string slicing, as well as the popular regular expression library re. You’ll also learn how to build your own function to remove the first n characters from a Python string.

A characteristic of strings in Python is that they are immutable, meaning that they cannot be changed directly. Because of this, the code you’ll see throughout this tutorial will re-assign the string to its sliced or re-selected string.

Use Python to Remove the First N Characters from a String Using Slicing

Strings in Python are indexed like many other objects. The index begins at zero and increases from there, allowing us to make selections of strings using their index positions.

Using a string slicing method allows us to specify exactly how many characters we may want to drop from a string.

Say we wanted to drop the first character only from our string, we could select the entire string beginning from the second character (index 1):

>>> a_string = 'The quick brown fox jumps over the lazy dog'
>>> reduced_string = a_string[1:]

>>> print(reduced_string)

he quick brown fox jumps over the lazy dog

You may notice that following the colon we don’t include a number. This allows us to simply select to the end of the string, without knowing exactly how long the string is.

Now let’s see how we can use string slicing to remove the first 3 characters from a string in Python:

>>> a_string = 'The quick brown fox jumps over the lazy dog'
>>> reduced_string = a_string[3:]

>>> print(reduced_string)

 quick brown fox jumps over the lazy dog

Tip! If you wanted to learn to make sure your strings don’t start with certain characters, such as strings, check out my post on the Python strip() method in my post here.

Now let’s see how we we can use regular expressions to remove the first n characters from a string in Python.

Use Python to Remove the First N Characters from a String Using Regular Expressions

You can use Python’s regular expressions to remove the first n characters from a string, using re’s .sub() method. This is accomplished by passing in a wildcard character and limiting the substitution to a single substitution.

Let’s see how we can accomplish removing the first character from a string:

>>> import re

>>> a_string = 'The quick brown fox jumps over the lazy dog'
>>> reduced_string = re.sub(r'.', '', a_string, count = 1)

>>> print(reduced_string)

he quick brown fox jumps over the lazy dog

Let’s see what we’ve done here:

  1. We used the regular expression sub() method to pass in the string we want to replace. The . string replaces any character in regular expressions.
  2. We say to replace this character with nothing.
  3. Finally we pass in the count= parameter to limit our substitution to a single time (meaning we only remove the beginning of the string).

Let’s see how we can remove the first five characters from a Python string using regular expressions:

>>> import re

>>> a_string = 'The quick brown fox jumps over the lazy dog'
>>> reduced_string = re.sub(r'.', '', a_string, count = 5)

>>> print(reduced_string)

uick brown fox jumps over the lazy dog

Finally, let’s explore how we can create a function to remove the first characters from a string.

Create a Function To Remove the First Characters from a String

In this section, you’ll learn how to create a function to remove the first n characters from a string. While this may seem trivial given the ease with which this can be done, creating a function can make your code more readable for future readers to understand exactly what you are doing.

Let’s see how we can create a simple function. To learn more about Python functions for data science, check out my tutorial here.

def remove_first_n_characters(a_string, number_to_remove):
    return a_string[number_to_remove:]

In this section, you learned how to create a readable function to remove the first n characters from a string.

Conclusion

In this post, you learned how to remove the first n characters from a string in Python, using both string slicing as well as the regular expression re library’s .sub() function. Finally, you learned how to create a simple function to accomplish this.

To learn more about the regular expression library, check out the official documentation here.

Nik Piepenbreier

Nik is the author of datagy.io and has over a decade of experience working with data analytics, data science, and Python. He specializes in teaching developers how to use Python for data science using hands-on tutorials.View Author posts

Tags:

4 thoughts on “Python: Remove the First N Characters from a String”

  1. def remove_first_n_characters(a_string, number_to_remove):
    return a_string[:number_to_remove]

    is wrong, it should be return a_string[number_to_remove:]

  2. hello,
    I think the last function is incorrect.
    Should be :
    def remove_first_n_characters(a_string, number_to_remove):
    return a_string[number_to_remove:]

    Cheers

Leave a Reply

Your email address will not be published. Required fields are marked *