Python String strip() Method

23-12-22 Ahmed Obaid 1445 0

​strip() method returns a copy of the string with characters or whitespace removed from the beginning or end of the string. argument, the chars argument comes by default to remove whitespace. If the string contains no whitespaces and no chars argument is provided, the string is returned as is.

 لا تحتوي على مسافات بيضاء ولم يتم توفير وسيطة chars ، يتم إرجاع السلسلة كما هي.


The way it is formulated is like thisThe way it is formulated is like this


 string.strip ([chars])

The chars argument is a string specifying the set of characters to remove


مثال:


# strip() method
string = " Welcome to Ahmed Obaid site "
# prints the string by removing leading and trailing whitespaces
print(string.strip())

Example:


# strip() method
string = " Welcome to Ahmed Obaid site "
# prints the string by removing leading and trailing whitespaces
print(string.strip())

The output will be:


Welcome to Ahmed Obaid site

in the exampleundefined The strip() function removes the white space from the beginning and end of the string.

 But if we want to remove the empty space (white) from the beginning only or from the end only. We can do this with the following functions:

 rstrip() : Returns a new string with whitespace removed from the end of the string. From the "right" side of the chain.

 lstrip() : Returns a new string with white space removed from the beginning of the string, from the "left" side of the string.

 Example:


# strip() method
string = " Welcome to Ahmed Obaid site "
# prints the string by removing leading and trailing whitespaces
print(string.rstrip())
print(string.lstrip())

 Example:


# strip() method
string = " Welcome to Ahmed Obaid site "
# prints the string by removing leading and trailing whitespaces

print(f'Remove white space from the beginning and end of the string = \'{string.strip()}\'')

print(f'Remove white space from the end of the string = \'{string.rstrip()}\'')

print(f'Remove white space from the beginning of the string = \'{string.lstrip()}\'')

 The output will be:


Remove white space from the beginning and end of the string = 'Welcome to Ahmed Obaid site'
Remove white space from the end of the string = ' Welcome to Ahmed Obaid site'
Remove white space from the beginning of the string = 'Welcome to Ahmed Obaid site '

 We can also remove any character from the string instead of removing the default white space via the chars argument.

 string. strip ([chars])

 Example:


# strip() method
string = "###Welcome to Ahmed Obaid site###"
print(string.strip('#'))
print(string.rstrip('#'))
print(string.lstrip('#'))

 The output will be:


Welcome to Ahmed Obaid site
###Welcome to Ahmed Obaid site
Welcome to Ahmed Obaid site###

 We can also remove any group of characters from the string instead of removing the default whitespace via the chars argument.

 string. strip ([chars])

 Example:


# strip() method
string = "www.ahmedobaid.com".strip('cmow.')
print(string)

 The output will be:


ahmedobaid

External sources:

 Built-in functions - official Python documentation

 If you have any questions or concerns, leave them in the comments



Tags


Python String string methods python python string strip method python string strip

Share page