In Python, the str.strip() method is used to remove leading and trailing whitespaces from a string. By default, the strip() method removes any whitespace characters from the beginning and end of the string, such as spaces, tabs, and newlines. 
The strip() function is a string method that removes leading and trailing whitespace characters from a string. By default, it removes spaces, tabs, and newline characters from the beginning and end of a string. 
However, it can also be used to remove any specified characters from the beginning and end of a string.
Here's an example of strip() method:

It's important to note that the strip() function returns a new string and does not modify the original string.
We can also use lstrip() and rstrip() to remove leading and trailing whitespaces respectively.
strip() function in Python is a useful method for removing unwanted characters from the beginning and end of a string, which helps in cleaning up the data and making it ready for further processing or analysis.

original_string = "   Hello World   "
stripped_string = original_string.strip()
print(stripped_string)

This will print "Hello World" with leading and trailing whitespaces removed.

In addition to removing whitespaces, the strip() method can also remove specific characters from the beginning and end of the string. You can pass one or more characters as arguments to the strip() method, and it will remove those characters from the beginning and end of the string. Here's an example:


original_string = "!!Hello World!!"
stripped_string = original_string.strip('!')
print(stripped_string)

This will print "Hello World" with the leading and trailing '!' characters removed.

Alternatively, you can use lstrip() and rstrip() method to remove leading and trailing whitespaces respectively.


original_string = "   Hello World   "
stripped_string = original_string.lstrip()
print(stripped_string)

This will print "Hello World " with leading whitespaces removed.


original_string = "   Hello World   "
stripped_string = original_string.rstrip()
print(stripped_string)

This will print " Hello World" with trailing whitespaces removed.

It's worth noting that, the strip() method returns a new string with the leading and trailing whitespaces removed, it does not modify the original string.
  • In python, we are having another in-built function called "strip()".
  • The strip() function is used to remove the spaces of the starting and end of the string.
  • It is having one parameter character used to remove the begging or end character.
  • By default, it removes the white spaces at the beginning or end.
  • If there is no white space between the beginning or end, the string remains the same and returns the same string.
  • When the character we give does not match the start or end, it returns the same string.
  • If the character is given at start to remove then the space will be removed and the remaining will remain the same.
syntax: str. strip(char)

#write a python program to demonstrate the strip function()

  1. txt1="   my name is python"
  2. txt2="***my name is python***"
  3. txt3=",,,my name is python"
  4. txt1=txt1.strip()
  5. txt2=txt2.strip("*")
  6. txt3=txt3.strip(".")
  7. print(txt1)
  8. print(txt2)
  9. print(txt3)
Output: my name is python
               my name is python
               my name is python

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu