Strips characters from the right end of a string
strip() method removes whitespace from the right end (trailing) of the string by default.
By adding chars parameter, you can also specify the characters you want to strip.
|chars||optional||A list of characters to be removed from the string|
The method return a copy of the string with the specified characters removed from the right end of a string.
By default, the method removes trailing whitespace.
S = ' Hello, World! ' x = S.rstrip() print(x) # Prints Hello, World!
'\t' and carriage return
'\r' are also considered as whitespace characters.
S = ' Hello, World! \t\n\r ' x = S.rstrip() print(x) # Prints Hello, World!
By adding chars parameter, you can also specify the character you want to strip.
# Strip single character 'a' S = 'baaaaa' x = S.rstrip('a') print(x) # Prints b
Strip Multiple Characters
The chars parameter is not a suffix; rather, all combinations of its values are stripped.
In below example,
strip() would strip all the characters provided in the argument i.e. ‘w’, ‘o’ and ‘/’
S = 'example.com/wow' x = S.rstrip('wo/') print(x) # Prints example.com
More About rstrip() Method
Characters are removed from the trailing end until reaching a string character that is not contained in the set of characters in chars.
S = 'xxxxSxxxxSxxxx' x = S.rstrip('x') print(x) # Prints xxxxSxxxxS
Here is another example:
S = 'Version 3.2 Model-32 - ...' x = S.rstrip('.- ') print(x) # Prints Version 3.2 Model-32