how to remove space from end of string in python

To remove white spaces from the end of a string, one can use the rstrip() method.

Here is a code snippet showing how to use it:

main.py
my_str = "  hello world!   "

# removing white spaces from end of string
my_str = my_str.rstrip()

# prints "  hello world!"
print(my_str)
139 chars
8 lines

In this example, rstrip() removes the white spaces at the end of the string my_str, and the output is " hello world!".

gistlibby LogSnag