How to remove the last character from a String in Python

If you are looking for a tutorial topic in Python, I suggest the topic Remove the last character from a String in Python. If you want to remove the last character from a String, you can use functions like str.rsplit(), replace() function, or string to list conversion to use delete functions on the list. Post details below. 

Remove the last character from a String in Python.

Use the str.rstrip() function.

Syntax:

string.rsplit(separator, maxsplit)

Parameters:

  • separator – Optional. By default, any space is a separator. When specifying a separator, perform a split based on that character.
  • maxsplit – Optional. The default value is -1. Specify the maximum number of times to perform the split.

The rsplit() function returns a list of strings after splitting them based on the given delimiters in right-to-left order. It is similar to the split() method we are familiar with but different in the order of elements in the result. 

Example:

testStr = 'This is a tutorial article in Python!'
print(testStr)

# Use the rstrip() function to remove the last element of the string.
newStr = testStr.rstrip(testStr[-1])
print(newStr)

Output:

This is a tutorial article in Python!
This is a tutorial article in Python

An index of -1 means that a delete is performed from the end of the string. As you can see, the character ‘!’ has been removed from the original string.

Convert to a list to use delete functions on it.

You can convert strings to a list using the compatible delete functions on the list to remove the last element of the list and then convert them back to a string using the join() function. Here are some methods:

  1. list. pop( index ): Specify the element’s index to be removed as a parameter. The pop() function will remove that element from the list.
  2. del list [ index ]: The del command helps us to delete elements in a specified range in the list. You can use the del list [ start: end] syntax to remove multiple elements in the specified range.

Example:

  • Initialize a string and convert it to a list using the list() function.
  • Use functions to remove elements.
  • Use the join() function, which returns the string.
testStr = 'This is a tutorial article in Python!'
print(testStr)

# Use the list() function to convert string to list.
myList = list(testStr)

# Remove the last element of the list by the del keyword.
del myList[-1]
strAgain = ''.join(myList)
print(strAgain)

Output:

This is a tutorial article in Python!
This is a tutorial article in Python

Or you can use the pop() function.

Example:

testStr = 'This is a tutorial article in Python!'
print(testStr)

# Use the list() function to convert string to list.
myList = list(testStr)

# Remove the last element of the list by the pop() method.
myList.pop(-1)
strAgain = ''.join(myList)
print(strAgain)

Output:

This is a tutorial article in Python!
This is a tutorial article in Python

Note: To remove the last element, the index is -1.

Use the replace() function.

Syntax:

str.replace(old, new, count)

Parameters:

  • old: Original string.
  • new: String will replace the original string.
  • count: Specify the maximum number of character substitutions.

The replace() function will return a string that is a copy of the original string when the new substring has been replaced with the old substring.

Example:

  • The function replace() with the old parameter is the character ‘!’, the new parameter is a space (adequate as to remove the last element), and the count parameter is -1 to indicate traversing from the end of the string.
testStr = 'This is a tutorial article in Python!'
print(testStr)

# Use the replace() function to remove the last element of the string.
newStr = testStr.replace('!', '', -1)
print(newStr)

Output:

This is a tutorial article in Python!
This is a tutorial article in Python

Summary:

Hopefully, the article has given you ideas to implement the topic remove the last character from a String in Python. It would be best to use the replace() function because it is more optimal. Thank you for taking the time to read my post.

Leave a Reply

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