Home » Python Tutorial » Python Strip() Function with Examples

Python Strip() Function with Examples

Python strip is a built-in function that returns the string without white space if there any at the start or the end. If you pass a string that has white spaces at the end, the strip function will return the string without white spaces. If there are no white spaces in the string, then the function will return the string as it is.

Syntax of python strip

string.strip(characters)

The parameter passed to the strip function is an optional parameter. If you want, you can pass it or ignore it.

How to use the python strip function?

Since it is a string function, we can only use this function on a string. There are many ways to use the strip function. We will discuss one by one below.

Directly using python strip function in the print function.

Example

print('         Pawan Misal      '.strip())

Output

Pawan Misal

In the above example, we can see the white spaces at the starting and end of the string are removed by the strip function.

Storing a string a variable and use the strip function of the variable.

Example

str = '    hello world     '
print(str.strip())

Output

hello world

In this example, we are storing the string in a variable and using the strip function on it. We will understand this example in detail.

str = '    hello world     '
print('the number of characters in the string is:',len(str))
result = len(str.strip())
print('After using strip() the number of characters in the string is:', result)

Output

the number of characters in the string is: 20
After using strip() the number of characters in the string is: 11

You can see the number of characters present in the string before using the strip() are 20, and after using the strip(), the character count is 11. That means the strip() python function removers white spaces at the start and the end of the string. It does not remove the spaces in between the string.

Passing the character as a parameter to the strip function.

Example 1

str = '00000hello world00000'
result = str.strip('0')
print(result)

Output

hello world

In this example, 0 is passed as a parameter to the strip function. So strip function returns the string without 0. 

Example 2

str = '    00000hello world00000   '
result = str.strip(' 0')
print(result)

Output

hello world

In the above example, String has white spaces and 0 in it. So we pass a white space and 0 as a parameter to the strip function. The result of the strip function is in front of you.

If the characters passed as a parameter are not present in the string, the strip function would return the string as it is.

Example

str = 'hello world'
result = str.strip('xyz')
print(result)

Output

hello world

In this example, ‘xyz’ is passed to the function which is not present in the string. So the function strip python returns the string as it is.

FAQ

What does strip do in python?

Python strip function removes the white space at the leading edge of the string and the string’s trailing edge and returns it.

How to strip punctuation in python?

Pass the punctuation symbol as a parameter to the function. The strip function will return the string without punctuation.

Example:
str = ‘hello world…’
result = str.strip(‘.’)
print(result)


Output:
hello world

How to strip newline in python?

Python provides rstrip function to remove the newline from the string.

Example:
str = ‘hello world. \n’
print(len(str))
result = str.rstrip()
print(result)
print(len(result))


Output :
14
hello world.
12

What does .strip do in python?

It is a built-in string function provided by python to remove the leading and trailing white spaces in the string.

Did you know?
1. Armstrong Number In Python
2. Absolute Value in Python
3. Leap Year Program in Python
4. Factorial Program in Python
5. Python Split with Examples
6. Python Substring
7. Features of Python
8. Python Square Root
9. Python Round() Function
10. Python Strip

Pin It on Pinterest