Remove last character from a string in Python

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

Remove last character from string in python using slicing

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

str[start:end]

Here, the start and end are the index positions. The above expression returns a new string containing only selected characters i.e. from index position start to end -1. If neither start nor the end positions are provided while slicing the string, like some_str[:], then it returns a new string containing all the characters from the original string. That is because the default values of start & end are 0 & N respectively, where N is the size of the string. A part from this, start and can also be negative numbers, in that case negative indexing will work. For example, negative index positions are as follows,

  • Index position of last character: -1
  • Index position of second last character: -2
  • Index position of third last character: -3
  • ….

and the sequence go on like this. So, to remove last character from a string, we can use the negative slicing i.e.

sample_str = sample_str[ : -1]

It selects the characters from index position 0 i.e. start to end-1 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 last character of string.

Complete example is as follows,

sample_str = "Sample String"

# Remove last character from string
sample_str = sample_str[ : -1]

print(sample_str)

Output:

Sample Strin

It deleted the last character from string.

In case you don’t want to go forward with negative indexing while slicing the string, then you can also use the positive indexing. For that select the characters from index position 0 to N-1, where N is the size of string. For example,

sample_str = "Sample String"

# Remove last character from string
sample_str = sample_str[ : len(sample_str) - 1]

print(sample_str)

Output:

Sample Strin

It removed the last character from string.

Remove last character from string in python using Regex

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

To delete the last character from string using rege’s sub() function, you can pass a pattern that selects the last 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 last character of string, will replace it with the given replacement string i.e. empty string. Finally returns a new string containing remaining characters. Then we assigned back that new string to the same existing variable, it gave an effect that we have deleted the last character of string.

Complete example is as follows,

import re

sample_str = "Sample String"

# Remove last characters from string
sample_str = re.sub(".$", "", sample_str)

print(sample_str)

Output:

Sample Strin

It deleted the last character from string.

Summary:

We learned about different ways to delete the last 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