In this article we will discuss ways to remove first N characters from a string either by using slicing or regex or simple for loop.

Use slicing to remove first N character from string in python

In python, we can use slicing to select a specific range of characters in a string i.e.

str[start:end]

It returns the characters of the string from index position start to end -1, as a new string. Default values of start & end are 0 & Z respectively, where Z is the size of the string. It means if neither start nor end are provided, then it selects all characters in the string i.e. from 0 to size-1 and creates a new string from those characters.

We can use this slicing technique to slice out a piece of string, that includes all characters of string except the first N characters. Let’s see how to do that,

Remove first 3 character from string using Slicing

Suppose our string contains N characters. We will slice the string to select characters from index position 3 to N and then assign it back to the variable i.e.

org_string = "Sample String"

# Slice string to remove first 3 characters from string
mod_string = org_string[3:]

print(mod_string)

Output

ple String

By selecting characters in string from index position 3 to N, we selected all characters from string except the first 3 characters and then assigned this sliced string object back to the same variable again, to give an effect that first 3 characters of string are deleted.

Remove first character from string using slicing

To remove the first character from string, slice the string to select characters from index 1 till the end of string i.e.

org_string = "Sample String"

# Slice string to remove first 3 characters from string
mod_string = org_string[1:]

print(mod_string)

Output:

ample String

It selected all characters in the string except the first one.

Use for-loop to remove first N character from string in python

To delete first N character from a string, we can iterate over the characters of string one by one and select all characters from index position N till the end of the string. We have created a function for this,

def remove_first_n_char(org_str, n):
    """ Return a string by deleting first n
    characters from the string """
    mod_string = ""
    for i in range(n, len(org_str)):
        mod_string = mod_string + org_str[i]
    return mod_string

It accepts a string and a number N as arguments. Returns a new string that contains all the characters of the given string, except first N characters. Let’s use this function in some examples,

Remove first character from string using for loop

org_string = "Sample String"

# Remove first character from string
mod_string = remove_first_n_char(org_string, 1)

print(mod_string)

Output

ample String

Remove first 3 characters from string using for loop

org_string = "Sample String"

# Remove first character from string
mod_string = remove_first_n_char(org_string, 3)

print(mod_string)

Output

ple String

Remove First N character from string using regex

We can use regex in python, to match 2 groups in a string i.e.

  • Group 1: First N character of string
  • Group 2: Every character in string except the first N characters

Then modify the string by substituting both the groups by a single group i.e. group 2.

Remove first 3 characters from string using regex

import re

def remove_first_group(m):
    """ Return only group 1 from the match object
        Delete other groups """
    return m.group(2)

org_string = "Sample String"

result = re.sub("(^.{3})(.*)", remove_first_group, org_string)

print(result)

Output

ple String

Here sub() function matched the given pattern in the string and passed the matched object to our call-back function remove_first_group(). Match object internally contains two groups and the function remove_first_group() returned a string by selecting characters from group 2 only. Then sub() function replaced the matched characters in the original string by the characters returned by the remove_first_group() function. So, this is how we can delete first N characters from a string in python

To remove first character from string using regex, use n as 1 i.e.

import re

def remove_first_group(m): 
    """ Return only group 1 from the match object Delete other groups """ 
    return m.group(2)

org_string = "Sample String"
result = re.sub("(^.{1})(.*)", remove_first_group, org_string)

print(result)

Output

ample String

The complete example is as follows,

import re


def remove_first_group(m):
    """ Return only group 1 from the match object
        Delete other groups """
    return m.group(2)


def remove_first_n_char(org_str, n):
    """ Return a string by deleting first n
    characters from the string """
    mod_string = ""
    for i in range(n, len(org_str)):
        mod_string = mod_string + org_str[i]
    return mod_string

def main():
    print('*** Remove first N character from string using Slicing ***')

    print('*** Remove first 3 characters from string using slicing ***')

    org_string = "Sample String"

    # Slice string to remove first 3 characters from string
    mod_string = org_string[3:]

    print(mod_string)

    print('*** Remove first character from string using slicing ***')

    org_string = "Sample String"

    # Slice string to remove first 3 characters from string
    mod_string = org_string[1:]

    print(mod_string)

    print('***** Remove first N character from string using for loop *****')

    print('*** Remove first character from string using for loop ***')

    org_string = "Sample String"

    # Remove first character from string
    mod_string = remove_first_n_char(org_string, 1)

    print(mod_string)

    print('*** Remove first 3 characters from string using for loop ***')

    org_string = "Sample String"

    # Remove first character from string
    mod_string = remove_first_n_char(org_string, 3)

    print(mod_string)

    print('***** Remove first N characters from string using regex *****')

    print('*** Remove first character from string using regex ***')

    org_string = "Sample String"

    result = re.sub("(^.{1})(.*)", remove_first_group, org_string)

    print(result)

    print('*** Remove first 3 characters from string using regex***')

    org_string = "Sample String"

    result = re.sub("(^.{3})(.*)", remove_first_group, org_string)

    print(result)

if __name__ == '__main__':
    main()

Output:

*** Remove first N character from string using Slicing ***
*** Remove first 3 characters from string using slicing ***
ple String
*** Remove first character from string using slicing ***
ample String
***** Remove first N character from string using for loop *****
*** Remove first character from string using for loop ***
ample String
*** Remove first 3 characters from string using for loop ***
ple String
***** Remove first N characters from string using regex *****
*** Remove first character from string using regex ***
ample String
*** Remove first 3 characters from string using regex***
ple String