Advertisement



< Prev
Next >



Python - String strip() Method




The strip() string method is used to strip all the leading and trailing whitespace characters present in the invoked string.

Note : None of the string methods change the original string and only return a new string which reflects the change performed by the string method, hence the returned string must be stored or used(if needed to reflect the result), otherwise it is lost.



Signature of strip


The strip method has no parameters, so we don't have to worry about passing it some value.

# Signature of strip() method 
strip()




Method strip example


Here in program below, we have a string object that contains a string value with many leading and trailing whitespaces and we are going to strip all these by calling the strip() method on it.

# Python -  Program to strip the leading and trailing blank spaces from a string by calling the strip() method



# Creating the first string
s1 = '         hello       '


# Printing the first string
print('The value of string : ',  s1)


# Calling the strip method on string object 
print('The stripped value of first string : ', s1.strip())




# Creating the second string
s2 = '    Do     not    give    up       '


# Printing the second string
print('The value of string : ',  s2)


# Calling the strip method on string object 
print('The stripped value of second string : ', s2.strip())


Output is :


The value of string :           hello       
The strippedvalue of first string :  hello
The value of string :      Do     not    give    up       
The strippedvalue of second string :  Do     not    give    up





The method strip() does not modify the original content in a String object


The method strip() only returns the content of string object, minus the leading and trailing whitespaces in it. The method strip() does not modify the original content of a string object. Let's understand this by an example.


Here in program below, we are trying to remove all the leading and trailing whitespace characters present in a string by calling the strip method.
#Python - Method strip() does not modify the invoked string


# Creating a string
s1 = '       Lean something new everyday       '

# Printing the original string
print('The value of string : ',  s1)


# Calling  the strip method on string object 
print('The strippedvalue of string : ', s1.strip())


# Printing the original string after calling strip method on it.
print('The value of string value : ', s1)


Output is :


The value of string :         Lean something new everyday       
The stripped value of string :  Lean something new everyday
The value of string value :         Lean something new everyday 

The String object contains the same original value even after the method strip was called on it, because the method strip() only returns the content of string object, minus the leading and trailing whitespaces in it. It does not modify the original content of a string object.


Advertisement




Strings are immutable but reference variables are mutable.


String objects are immutable, but the reference variables pointing to them are not, therefore in order to display the result of calling the strip method on the original object, we can make the reference variable to the original string, point to the stripped string value returned by the strip method(but in this case, the original string will be lost).

#Python - Another example of string() method


# Creating a string
s1 = '       you will never have this day again, so make the most out of it!      '

# Printing the original string
print('The value of string : ',  s1)


# Result of calling strip() is made to reference by the reference of original string.
s1 = s1.strip()


# Old reference variable pointing to the new string
print('The stripped value of the string  : ', s1)


Output is :


The value of string :         you will never have this day again, so make the most out of it!      
The stripped value of the string  :  you will never have this day again, so make the most out of it!   





Please share this article -





< Prev
Next >
< String lstitle() Method
String lstrip() Method >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON


Advertisement