Remove last character from String in Python [5 Methods]

In this Python article, I will explain how to remove last character from string in Python using different methods. I will also explain all the methods present in Python remove last character from string with examples.

Remove last character from string in Python

There are various methods in Python remove last character from string.

  • String Slicing
  • str.rstrip()
  • String Concatenation
  • Regular Expressions
  • str.removesuffix() (Python 3.9+)

Let’s see them one by one with demonstrative examples:

Method 1: How to remove last character from string Python using string slicing

String slicing is a technique in Python used to extract a portion of a string. It allows us to work with a specific range of characters within a Python string. Here’s how string slicing works:

The syntax of string slicing in Python:

string[start:end]
NameDescription
StringThis is the original Python string that we want to slice.
StartThe index where the slice begins. It is inclusive, meaning the character at the start index is included in the slice. If not mentioned it starts from the 0 index number of the Python string.
EndThe index where the slice ends. It is exclusive, meaning the character at the end index is not included in the slice. If not mentioned it ends at the end of the Python string.
Parameters in string slicing in Python.

The result of the slicing is a new string that contains the characters from the start index up to, but not including, the end index. We can get an index number of a character in a string with the help of the index() function in Python.

We can use string slicing to remove the last character from string Python by creating a new Python string that includes all characters except the last one. Here’s how it works:

Example: Consider a situation where we have a Python string with the name of states and we have to create a abbreviation for it. So, we will start removing the characters from the last one by one until we get only two characters in the result.

For the above-mentioned scenario, we’ll use string slicing to remove the last character from a USA state name through Python:

# Original state name
state_name = "California"

# Removing the last character to get the abbreviation
abbreviation_in_progress = state_name[:-1]

print("Original State Name:", state_name)
print("Abbreviation after removing one character from last:", abbreviation_in_progress)

Output: As we can see, by using string slicing in Python, we removed the last character ‘a’ from the state name, resulting in the abbreviation “Californi”

Original State Name: California
Abbreviation after removing one character from last: Californi
remove last character from string python

This demonstrates how string slicing can remove the last character from the string in Python.

Method 2: How to remove the last character of a string in Python using rstrip() method

The rstrip() method in Python is a built-in string method used to remove trailing (rightmost) whitespace characters, such as spaces, tabs, or newline characters, from the end of a string. It operates on the right side of the string and returns a new string with the trailing whitespace removed, leaving the original string unchanged. Here’s how rstrip() works:

The syntax of rstrip() function in in Python:

string.rstrip([characters])
NameDescription
stringThis is the original Python string on which we want to perform the operation on.
characters(optional)We can specify a string or substring containing characters to be removed from the right end of the original Python string. If we don’t provide this argument, it will remove all whitespace characters by default.
Return ValueThe rstrip() method returns a new Python string with the specified trailing characters removed.
Parameters and Return value of rstrip() method in Python.

Example: In this scenario, we have a single Python string that represents a list of U.S. cities separated by commas, with a trailing comma at the end. The goal is to remove the last character from Python string, which is a comma, from the string using the rstrip() method in Python.

# Original string containing a comma at the end
city_string = "New York, Los Angeles, Chicago, Houston, Philadelphia,"

# Remove the last character (comma) using rstrip()
cleaned_string = city_string.rstrip(",")

print(cleaned_string)

Output: We use the rstrip(“,”) method to remove the last character (the comma) from the city_string Python string.

New York, Los Angeles, Chicago, Houston, Philadelphia
python remove the last character from a string

The rstrip() method effectively remove last character from string Python.

Method 3: Python remove last character from string using string concatenation

String concatenation in Python is the process of combining or joining two or more Python strings to create a new string. This can be done using the + operator.

To remove the last character from string Python using string concatenation, we can concatenate all characters except the last one to create a new Python string.

Scenario: Let’s create a scenario where we have a single Python string representing a U.S. state name, and we want to remove the last character from that state’s name using string concatenation:

# Original U.S. state name
state_name = "Texas"

# Remove the last character using string concatenation
modified_state_name = ""
for char in state_name[:-1]:
    modified_state_name += char

print(modified_state_name)

Output: We initialize an empty Python string modified_state_name to store the modified state name. We use a for loop to iterate through each character in state_name original Python string except for the last character (achieved with state_name[:-1]). Inside the loop, we concatenate each character to empty Python string using the += operator.

Texa
how to remove last character from string in python

As we can see, the code remove last character from string Python using string concatenation, and the modified state name is printed as “Texa“.

Method 4: Remove last letter from string Python using regular expression

The sub() method is a function provided by the Python re module, which stands for “regular expressions.” It is used for performing regular expression-based substitution or replacement within a string. The re.sub() function allows us to search for a specific pattern (defined by a regular expression) in a given Python string and replace all occurrences of that pattern with a specified replacement string.

Here’s the basic syntax of the re.sub() method:

re.sub(pattern, replacement, string)
NameDescription
patternThis is the regular expression pattern to search for within the original Python string.
replacementThis is the string that will replace each occurrence of the pattern in the string.
stringThis is the input original Python string in which we want to perform the substitution.
Parameters within the re.sub() method in Python.

Example: Consider a situation where we have a Python string where we want to remove the last character (a hyphen) from a string representing a ZIP code:

import re

# Input string representing a ZIP code
zip_code = "90210-"
pattern = r'-$'
# Use re.sub() to remove the last character (hyphen) from the ZIP code
cleaned_zip_code = re.sub(pattern, '', zip_code)

print(cleaned_zip_code)

Output: We define a regular expression pattern r’-$’ to match the last character, which is a hyphen (-), followed by the end of the string ($). Then, we use the re.sub() method to remove the hyphen from the ZIP code.

90210
python remove last character

This way we can use re module sub() function to remove last character from string Python.

Method 5: Python string remove last character using str.removesuffix() (Python 3.9+)

The str.removesuffix() method is introduced in Python 3.9. It allows us to remove a specified suffix (including the last character) from a Python string. To remove the last character, we pass the last character of the string in Python as the suffix to be removed. This method is concise and designed specifically for removing suffixes.

Scenario: Removing the last character (an exclamation mark) from a U.S. slogan through Python.

slogan = "Buy American, Support Local!"
modified_slogan = slogan.removesuffix("!")
print(modified_slogan)

Output: We call the removesuffix() method on that Python string and provide the suffix we want to remove as an argument, in this case, “!”. The method checks if the string in Python ends with the specified suffix. If it does, it removes the suffix and returns the modified Python string.

Buy American, Support Local
string remove last character python

This way we can use the removesuffix() method in Python to delete last character in string Python.

Conclusion

Understanding how to remove last character from string Python using five different methods like string slicing, or using rstrip() method, using string concatenation, using regular expression’s sub() function, and using the removesuffix() method. Additionally, we have also seen different examples for each of the methods.

The choice of method depends on our specific requirements and coding style in how we want to remove last character from string Python. Generally, using string slicing is the most straightforward and efficient way to accomplish this task, but we have the flexibility to choose the method that suits our needs best.

You may also like to read the following Python tutorials.