'rstrip(), lstrip(), and strip() in Python

I am using Sublime Text version 3.0 and my input is:

little_known_person = "' Christopher '"

print(little_known_person.strip())

but my output is ' Christopher ' instead of 'Christopher'. The output is the same if I try the rstrip() and lstrip() methods.



Solution 1:[1]

You need to strip leading/trailing whitespace and single quotes. The default, with no argument, is to strip only whitespace.

little_known_person.strip(" '")

The argument is an arbitrary iterable (str, list, tuple, etc) containing characters that should be stripped. The order doesn't matter.

Solution 2:[2]

You need to strip whitepaces and single quotes and then re-add the quotes:

little_known_person = "'" + little_known_person.strip(" '") + "'"

Solution 3:[3]

Please try if the following approach solves your issue:

>>> little_known_person = "' Christopher '"
>>> little_known_person.replace("'", "").strip()
'Christopher'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 chepner
Solution 2 mrCarnivore
Solution 3 Freddy Mcloughlan