How to remove last n characters from a string in Python?

Here is a Python tutorial that shows how to remove the last n characters from a string. The last n characters can still be removed from strings despite the fact that they are unchangeable. Four methods and functions of Python are presented in this article for removing the last n characters from a string.

Method 1: Using positive index slicing to remove last n characters from a string

Using this method, firstly we will introduce and calculate the length of the sample string to get the last character of the string, then by using slicing at index l-1 of the sample string in Python, we can remove the last character of the string, and print the output with removed last character.

Example:

#Introducing the sample string
str = "Codespeedy Technologyy"
#calculating the length of the string
l= len(str)
#removing the last character from positive index slicing
remove_lastchar = str[:l-1]
#print the string with the removed last character
print("string: ",remove_lastchar)

Output:

string: Codespeedy Technology

Method 2: Using negative index slicing

This method begins by introducing the sample string and removing the last character which is in the string always at index -1 in Python, so we slice it at that position using negative index slicing and then print the remaining string with removed last character.

Example:

#Introducing the sample string
Str = "Codespeedy Technologyy"
 
#last character is always at -1 position and removing it using negative index slicing
remove_lastchar = Str[:-1]
#print the remaining string
print("String : ",remove_lastchar)

Output:

string: Codespeedy Technology

Method 3: Using rstrip function

Using this method, let us take an example if we want to remove the 3 last characters from a string in Python. To remove the last 3 characters from the string, take the input from the user and then remove the last 3 unwanted characters from the string using the rstrip function (it removes the right side characters from the end of the string) and at last print the output. For reference, look at the example given below.

Example:

#take the input from the user
str = input("Enter : ")
#removing the last 3 characters using rstrip function
remaining_string = str.rstrip()[:-3]
#print the string obtained after removing the characters
print("String : ",remaining_string)

Output:

Enter : Codespeedy Technologyies

String : Codespeedy Technology

You can check: How to remove digits from end of a string in Python

Method 4: Using For loop

You can also remove the last n characters from the string using the for loop. Initially, the length of the sample string must be calculated, and then an empty string is added to make a new string. After this the next step is to use the for loop in the range of 0th index to l-2 of the string in Python then by repeating, loop through the empty string, we append a character each time. At the last print the final string obtained.

#Introduce the sample string
str = "Codespeedy Technologyi"
 
#Calculate the length of the string
l = len(str)
 
#Take an empty string
empty_string = ""
 
#using for loop to remove the last character

for i in range(0,l-2):
    empty_string = empty_string + str[i]
#print the final string 
print("Final String : ",empty_string)

Output:

Final String : Codespeedy Technology

Leave a Reply

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