Python String strip()

The strip() method returns a copy of the string with both leading and trailing characters removed (based on the string argument passed).

The strip() removes characters from both left and right based on the argument (a string specifying the set of characters to be removed).

The syntax of strip() is:

string.strip([chars])

strip() Parameters

  • chars (optional) - a string specifying the set of characters to be removed.

If the chars argument is not provided, all leading and trailing whitespaces are removed from the string.


Return Value from strip()

The strip() returns a copy of the string with both leading and trailing characters stripped.

  • When the combination of characters in the chars argument mismatches the character of the string in the left, it stops removing the leading characters.
  • Similarly, when the combination of characters in the chars argument mismatches the character of the string in the right, it stops removing the trailing characters.

Example: Working of strip()

string = ' xoxo love xoxo   '

# Leading whitepsace are removed
print(string.strip())

print(string.strip(' xoxoe'))

# Argument doesn't contain space
# No characters are removed.
print(string.strip('sti'))

string = 'android is awesome'
print(string.strip('an'))

When you run the program, the output will be:

xoxo love xoxo
lov
 xoxo love xoxo   
droid is awesome