What is the strip() method?

QuestionsQuestions8 SkillsProDec, 10 2025
070

The strip() method in Python is used to remove specified characters from both ends (beginning and end) of a string.

By default, without any arguments, strip() removes whitespace characters (spaces, tabs, newlines) from both the left and right sides of the string.

Here's an example from the current step:

spam = '    Hello World     '
print(spam.strip())

This will output:

Hello World

You can also pass a string argument to strip() to specify which characters to remove. It will remove any character present in the argument from the ends of the string until it encounters a character not in the argument.

For instance:

spam = 'SpamSpamBaconSpamEggsSpamSpam'
print(spam.strip('ampS'))

This will remove all 'S', 'p', 'a', 'm' characters from both ends, resulting in:

BaconSpamEggs

Do you have any other questions about strip() or other string methods?

0 Comments

no data
Be the first to share your comment!