Python String strip method with Example – Python Tutorial

1 year ago Lalit Bhagtani 0

In this tutorial, we will learn about python string strip method.
Python String strip

strip method :

This method returns a copy of the string in which all the occurrences of chars ( passed as an argument ) has been removed from both beginning ( left side ) and end ( right side ) of the string object. If chars arguments is omitted or None, then chars argument defaults to whitespace.

Syntax : < String Object >.strip( [ chars ] ) :  < String Object >

              chars: characters to be removed.

Example:

[datacamp_exercise lang=”python” height=”400″ min-height=”300″ max-height=”500″]
[datacamp_pre_exercise_code]

[/datacamp_pre_exercise_code]
[datacamp_sample_code]
# create one string and assign it to variable s1
s1 = ‘ strip example ‘

# call strip() method on string s1
output = s1.strip()

# print the value of variable output
print(output)

# create one string and assign it to variable s2
s2 = ‘strip example strip’

# call strip() method on string s2 by passing ‘strip’ as an argument
output = s2.strip(‘strip’)

# print the value of variable output
print(output)
[/datacamp_sample_code]
[datacamp_solution]

[/datacamp_solution]
[/datacamp_exercise]

References :-

  1. strip() method Docs

That’s all for Python String strip method. If you liked it, please share your thoughts in comments section and share it with others too.

Previous