Remove first character from a string in Python

In this article, we will discuss different ways to remove first characters from a string.

Remove first character from string in python using slicing

In python, we can slice a string to select a range of characters from it. For example,

str[start:end]

start and end are the index positions. The above expression returns a new string containing characters from index position start to end -1. The default values of start & end are 0 & N respectively, where N is the size of the string. So, if neither start nor the end positions are provided like some_str[:], then it returns a new string containing all the characters of the original string.

We can use this to remove first character from string i.e.

sample_str = sample_str[1 : ]

It selects the characters from index position 1 to end and creates a new string containing them. Then we assigned back that string to the same string variable, it gave an effect that we have deleted the first character of string.

Complete example is as follows,

sample_str = "Sample String"

# Slice the string to remove first character
sample_str = sample_str[1 : ]

print(sample_str)

Output:

ample String

It deleted the first character from string.

Remove first character from string in python using Regex

We can use the regex modules’s sub() function to delete the first character of string. In regex, the the re.sub() function matches the given pattern in the string and replaces the matched characters with a given replacement string.

To delete the first character from string using rege’s sub() function, you can pass a pattern that selects the first character of string only and as a replacement string pass the empty string. For example,

sample_str = re.sub("^.", "", sample_str)

It will select the first character of string, replace it with the given replacement string i.e. empty string. Finaly returns a new string containing remaining characters. Then we assigned back that string to the same string variable, it gave an effect that we have deleted the first character of string.

Complete example is as follows,

import re

sample_str = "Sample String"

# Remove first characters from string
sample_str = re.sub("^.", "", sample_str)

print(sample_str)

Output:

ample String

It deleted the first character from string.

Python: Remove first character from string if match

In some scenarios, it might be possible that you want to delete the first character from a string only if it is amatch to another character. For example, lt’s see how to remove first character from string if it is ‘S’,

# Example 1

sample_str = "Sample String"
char_to_check = 'S'

# Remove first character from string if matches the given character
if char_to_check == sample_str[0]:
    sample_str = sample_str[1:]

print(sample_str)

# Example 2

sample_str = "An apple a day"
char_to_check = 'S'

# Remove first character from string if matches the given character
if char_to_check == sample_str[0]:
    sample_str = sample_str[1:]

print(sample_str)

Output:

ample String  
An apple a day

In first example, it deleted the first character from string because it was ‘S’. But in second example it didn’t deleted the first character because it was not ‘S’

Summary:

We learned about different ways to delete first character from a string in python.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top