Open In App
Related Articles

Python program to Remove Last character from the string

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Given a string, the task is to write a Python program to remove the last character from the given string.

Example:

Input:  “GeeksForGeeks”
Output: “GeeksForGeek”

Input:  “1234”
Output: “123”
Explanation: Here we are removing the last character of the original string.

Note: Strings are immutable in Python, so any modification in the string will result in the creation of a new string.

Using list Slicing to Remove the Last Element from the string 

The slicing technique can also remove the last element from the string. str[:-1] will remove the last element except for all elements. Here we are using the concept of slicing and then updating the original string with the updated string.

Python3

Str = "GeeksForGeeks"
 
# using Positive indexing
Str = Str[:len(Str)-1]
print(Str)
Str = "GeeksForGeeks"
 
# using negative indexing
Str = Str[:-1]
print(Str)

                    

Output
GeeksForGeek
GeeksForGeek

Using loops and extra space to Remove the Last Element from the string

Here we are using some extra space i.e. O(N) and storing all the characters except for the last character.

Python3

Str = "GeeksForGeeks"
a = ""
 
for i in range(len(Str)-1):
    a += Str[i]
print(a)

                    

Output
GeeksForGeek

Time Complexity: O(n)
Auxiliary Space: O(n), where n is length of string.

Using rstrip() function to Remove the Last Element from the string 

The rstrip() is a Python function that returns a string copy after removing the trailing characters.

Python3

Str = "GeeksForGeeks"
Str = Str.rstrip(Str[-1])
print(Str)

                    

Output
GeeksForGeek

Using regex to Remove Last Element from string 

Here we are using the concept of regular expression and removing the last character of the string.

Python3

import re
 
def reg(m):
    return m.group(1)
 
# sample string
Str = "GEEKSFORGEEKS"
 
# Remove last character from string
new_string = re.sub("(.*)(.{1}$)", reg, Str)
print(new_string)

                    

Output
GEEKSFORGEEK

Using list(),pop() and join() methods

Python3

#Python program to Remove Last character from the string
Str = "GeeksForGeeks"
x=list(Str)
x.pop()
x="".join(x)
print(x)

                    

Output
GeeksForGeek

Using list comprehension and join method

Algorithm:

  1. Initialize the given string “Str”.
  2. Create a list “x” by iterating through each character in the string “Str” using a list comprehension.
  3. Remove the last character from the list “x” using the pop() method.
  4. Join the characters in the list “x” back to form a string and store it in “x”.
  5. Print the resulting string “x”.

Python3

#Python program to Remove Last character from the string
Str = "GeeksForGeeks"
x = "".join([Str[i] for i in range(len(Str)-1)])
print(x)

                    

Output
GeeksForGeek

Time Complexity: O(n), where n is the length of the input string “Str”.
Auxiliary Space: O(n), where n is the length of the Out[ut string “x”.



Last Updated : 27 Jul, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads