How many ways can you remove the last character from a string in python? One? Maybe two?
In this tutorial, I am going to show you 5 interesting ways to remove the last character from the string in Python.
String plays an important role in our programming language, and every time we use it in our program.
There are many times we need to remove the last character from a string in Python to avoid an error.
The five ways to do this are:
- Negative Index by Slicing:
- Positive Index by Slicing:
- Regular Expression method:
- The rstrip function:
- for loop:
1. Using Negative Index by Slicing:
Negative index [:-1] is used to remove the last character from the string and return the rest string.
Let’s go for an example for a better understanding.
Program
str1 = “This is python”
print ("original string is:", str1)
str2 = str1[: -1]
print ("after removing the last character, the string is:", str2)
Output:
original string is: This is python
after removing the last character, the string is: This is pytho
Explanation:
- On line number 1, we have taken a string as str1.
- On line number 2, then we have to print it using the print function.
- On line number 3, we have used a negative index for removing the last character from a string which is [:-1] because n is present in the last index.
- On line number 4, we have to print the rest string.
2. Using Positive Index by Slicing:
Just like negative indexing we can also use positive indexing to remove the first element from the string.
Let’s take a look at the example for a better understanding.
Program
str1 = "This is python"
print ("original string is:", str1)
length= len (str1)
str2 = str1[ :length-1]
print ("after removing the last character, the string is:", str2)
Output:
original string is: This is python
after removing the last character, the string is: This is pytho
Explanation:
-
On line number 1, we have taken a string as str1.
-
On line number 2, then we have to print it using the print function.
-
On line number 3, we have used positive index for removing the last character from string using length function,
1 . 1st we have calculated the length of st1,
2 . then we subtract -1 from the length,
3 . then stored the rest character in str2 -
On line number 4, we have to print the rest string.
3. Using the Regular Expression method:
Using the regular expression method we can easily remove the last character from the string.
For using the regular expression method, you need to import the re library.
For a better understanding, let’s look into the example.
Program
import re
str1 = "This is python"
print ("original string is:", str1)
str2 = re.sub("n","", str1)
print ("after removing the last character, the string is:", str2)
Output:
original string is: This is python
after removing the last character, the string is: This is pytho
Explanation:
- On line number 1, we have import a library called re, that is import re (for using regular expression method we have to import the library)
- On line number 2, we have taken a string as str1.
- On line number 3, then we have to print it using the print function.
- On line number 4, we have used the regular expression method for removing the last character from a string, which is re.sub (“n”,””, str1) that means n which is present in the last index will be replaced by space in str1,
- Then we have to print the rest character using the print function.
4. Using the rstrip function:
The rstrip method is used to remove or delete the characters from the right side of the string, so we can easily remove the last character from the string using rstrip function.
For a better understanding, let’s look into the example.
Program
str1 = "This is python"
print ("original string is:", str1)
str2 = str1.rstrip(str1[-1])
print ("after removing the last character, the string is:", str2)
Output:
original string is: This is python
after removing the last character, the string is: This is pytho
Explanation:
- On line number 1, we have taken a string as str1.
- On line number 2, then we have to print it using the print function.
- On line number 3, we have the rstrip function to remove the last character from the string. which is rstrip(str[-1]).
- On line number 4, we have to print the rest character.
5. Using for loop:
We can easily remove or delete the last character from the string using for loop. In for loop, we have to start equaling the character from the first index, when the loop got the same character that we have given in for loop, then the loop will be stopped.
For a better understanding, let’s look into the example.
Program
str1 = "This is python"
result=""
for i in str:
if i=="n":
Pass
else:
result+=i
print ("original string is:", str1)
print ("after removing the last character, the string is:", result)
Output:
original string is: This is python
after removing the last character, the string is: This is pytho
Explanation:
- On line number 1, we have taken a string as str1.
- On line number 2, then we have taken a variable named as a result.
- On line number 3, we have used for loop for removing the last character from string and we have initialized the for loop inside the str.
- On line number 4, we have used the if condition, because when the I will be equal to n, then the loop will be stopped.
- On line number 5, we have passed it to the next line which is else part.
- On line number 6, we have given an else condition, If the I will be not equal to n, then the else part will be executed.
- On line number 7, we have incremented one more time to result.
- On line number 8, we have printed the original string which is “This is python”.
- On line number 9, we have print the result which we need which is “This is python”.
Conclusion
In this article, we have discussed the top 5 best methods to remove/delete the last character from a string with the example, output, and full explanation.
We hope you enjoy this article and found this helpful for removing/deleting the last character from the string.
Originally published at https://arcanevalley.com on June 2, 2021.