Remove Last Character from String in Python

Here, we will develop a Python program to remove the last character from the string. If the string was “Knowprogram” then the new string will be “Knowprogra”. We will discuss how to remove the last character from the given string using native methods, Regular Expression, rstrip() function, and slice operator.

Python Program to Remove Last Character From String

We will take a string while declaring the variables. Then, We will run the loop from 0 to len(string)-1 and append the string into the empty string. Calculated the length of the string using len() function to access the last character of the string. Finally, a new string will be displayed on the screen.

# Python program to remove last character from string

# take input
string = input("Enter any string: ")

# remove last character from string
out_string = ""
for i in range(0, len(string)-1):
    out_string = out_string + string[i]
   
# print string after removal
print ("New string:", out_string)

Output for the different input values:-

Enter any string: Python
New string: Pytho

Enter any string: Know Program
New string: Know Progra

Python Replace Last Character in String

We will remove the last character from the string using negative index by slicing. By slicing it from the -1 index, we have removed the last character of the string. The string[: -1] specifies all the characters of the string except the last one. The negative index -1 specifies the last character in the string. The [:-1] specifies the character at index 0 and goes up to the index before the last one.

# Python program to remove last character from string

# take input
string = input("Enter any string: ")

# remove last character using slicing
out_string = string[:-1]
   
# print string after removal
print ("New string:", out_string)

Output:-

Enter any string: negative index
New string: negative inde

In the previous program, we used negative index by slicing but in this program, We are using positive index by slicing. Calculated the length of the string using len() function to access the last character of the string. We have used slicing for removing the last character of the string.

# Python program to remove last character from string

# take input
string = input("Enter any string: ")

# remove last character using slicing
l = len(string)
out_string = string[:l-1]
   
# print string after removal
print ("New string:", out_string)

Output:-

Enter any string: positive index
New string: positive inde

Replace the Last Character in String Python using rstrip() Method

Python rstrip() method is used to removes all the trailing characters from the string. It means it removes the characters from the right side of the string. This method can be done only in a single line of code and straightforward method to remove the last character from the string.

# Python program to remove last character from string

# take input
string = input("Enter any string: ")

# remove last character using rstrip()
out_string = string.rstrip(string[-1])
   
# print string after removal
print ("New string:", out_string)

Output:-

Enter any string: rstrip
New string: rstri

Python String Delete Last Character

We can use the Regular Expression or regex() function in python, to match 2 groups in a string i.e.

  • Group 1: Every character in string except the last characters.
  • Group 2: Last character of a string.

We will first be importing the RegEx module. Take a second_group function which will only return only group 1 from the match object delete other groups. Then, we have applied the function with the proper syntax and tried to remove the last character only by putting value = 1 in the given syntax. The sub() function replaced the matched characters in the string with the characters returned by the second_group() function.

# Python program to remove last character from string

# importing RegEx module
import re

def second_group(m):
    ''' Return only group 1 from the match object
        Delete other groups '''
    return m.group(1)

# take input
string = input("Enter any string: ")

# remove last character using re.sub()
out_string = re.sub("(.*)(.{1}$)", second_group, string)
   
# print string after removal
print ("New string:", out_string)

Output:-

Enter any string: RegEx
New string: RegE

Also See:- Python Program to Check Special Character

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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