How to remove the last character from a string?

There are a few ways to remove the last character from a string in Python. Here are three approaches you can use:

  1. Using the slicing method:
string = "Hello, World!"
new_string = string[:-1]
  1. Using the rstrip method:
string = "Hello, World!"
new_string = string.rstrip('!')
  1. Using the rsplit method:
string = "Hello, World!"
new_string = string.rstrip('!')

Note that the rstrip and rsplit methods will only work if you want to remove a specific character or set of characters from the end of the string. If you just want to remove the last character, regardless of what it is, you can use the slicing method.