Python
Python Tutorials for Beginners
What is Python? Python is an object-oriented programming language created by Guido Rossum in 1989....
Python strip() function is a part of built-in functions available in the Python library. The strip() method removes given characters from start and end of the original string. By default, strip() function removes white spaces from start and end of the string and returns the same string without white spaces.
In this Python tutorial, you will learn:
string.strip([characters])
The Python String strip() will return:
str1 = "Welcome to gtupapers!" after_strip = str1.strip()
Output:
Welcome to gtupapers!
The Python String strip() function works only on strings and will return an error if used on any other data type like list, tuple, etc.
Example when used on list()
mylist = ["a", "b", "c", "d"] print(mylist.strip())
The above will throw an error :
Traceback (most recent call last):
File "teststrip.py", line 2, in <module>
print(mylist.strip())
AttributeError: 'list' object has no attribute 'strip'
str1 = "Welcome to gtupapers!" after_strip = str1.strip() print(after_strip) str2 = "Welcome to gtupapers!" after_strip1 = str2.strip() print(after_strip1)
Output:
Welcome to gtupapers! Welcome to gtupapers!
str1 = "****Welcome to gtupapers!****"
after_strip = str1.strip("*")
print(after_strip)
str2 = "Welcome to gtupapers!"
after_strip1 = str2.strip("99!")
print(after_strip1)
str3 = "Welcome to gtupapers!"
after_strip3 = str3.strip("to")
print(after_strip3)
Output:
Welcome to gtupapers! Welcome to Guru Welcome to gtupapers!
Here, are reasons for using Python strip function
What is Python? Python is an object-oriented programming language created by Guido Rossum in 1989....
What is type() in Python? Python has a built-in function called type() that helps you find the...
What is Loop? Loops can execute a block of code number of times until a certain condition is met....
$20.20 $9.99 for today 4.5 (129 ratings) Key Highlights of Python Tutorial PDF 211+ pages eBook...
What are the modules in Python? A module is a file with python code. The code can be in the form...
We have prepared the most frequently asked Python Interview Questions and Answers that will help...