Python教程-Python中的strip()函数
strip()函数是Python的一个预定义库功能。它通过移除传递给strip()函数的前导和尾随空格、字符和符号,用于返回原始字符串的副本。换句话说,将一组字符作为参数传递给Python字符串的strip()函数,然后从字符串的左右两端删除字符。如果不给strip()函数传递参数,则默认删除字符串开头和结尾的空格。
Python中的strip()函数
语法
strip( 'characters' )
参数:
- strip或"chars":strip()函数的可选参数。因此,如果程序员不传递任何参数,strip()函数会从字符串的开头和结尾删除空格。
- 如果传递给strip()函数的字符集,它会从第一个字符串中删除字符或图像。
- 返回值:通过从原始字符串中删除字符集或空格,它会返回其副本。
使用strip()函数从给定字符串中删除字符或符号
让我们考虑一个示例,通过在Python中删除给定字符串的前导或尾随字符来执行strip()函数。
Strip.py
strinput = " $$$$$ No. 1 Welcome to javatiku!! No. 1 $$$ "
# use strip() function to remove the set of characters
res = strinput.strip ( ' $No. 10 !' ) # store result into res variable
print ( " Given string is: ", strinput)
print ( " After removing the set of characters: ", res)
str3 = ' 1 11 111 111 1111 Learn Python Programming Tutorial 1111 111 11 1 '
str4 = str3. strip ( '1' )
print (" \n Given string is ", str3)
print (" Stripping 1 from both ends of the string using strip ('1') function ", str4)
# define new string
str5 = '++++++Python Programming Tutorial****** $$$$$'
print ("\n Given string is = ", str5)
# use strip function to remove the symbols from both ends
str6 = str5. strip ( ' $*+' )
print (" Stripping the '+', '*' and '$' symbols on both sides of the string is = ", str6)
输出
Given string is: $$$$$ No. 1 Welcome to javatiku!! No. 1 $$$
After removing the set of characters: Welcome to javatiku
Given string is 1 11 111 111 1111 Learn Python Programming Tutorial 1111 111 11 1
Stripping 1 from both ends of the string using strip ('1') function 1 11 111 111 1111 Learn Python
Programming Tutorial 1111 111 11 1
Given string is = ++++++Python Programming Tutorial****** $$$$$
Stripping the '+', '*' and '$' symbols on both sides of the string is = Python Programming Tutorial
使用strip()函数从给定字符串中删除空格
让我们考虑一个示例,通过在Python中删除给定字符串的前导或尾随空格来执行strip()函数。
Strip2.py
str1 = ' Welcome to the World! '
# print the original string
print (' Given string is: ', str1)
# use strip() function to remove whitespace
str2 = str1.strip()
print(" After removing whitespaces from an original string: ", str2)
str3 = ' Learn Python programming '
# print the original string
print (' Given string is: ', str3)
# use strip() function to remove whitespace
str4 = str3.strip()
print(" After removing whitespaces from an original string: ", str4)
输出
Given string is: Welcome to the World!
After removing whitespaces from an original string: Welcome to the World!
Given string is: Learn Python programming
After removing whitespaces from an original string: Learn Python programming
在上面的程序中,我们使用strip()函数从给定字符串的开头和结尾删除空格,但它不会删除字符串中间的空格。
从用户获取任何字符串并使用strip()函数删除任何字符或符号的程序
Prog.py
str1 = input( " Enter the string ") # take a string
print (" Your string is ", str1)
ch = input (" Enter any character or symbol that you don't want to see in the string ")
res = str1.strip (ch)
# print the string after using a strip() method
print (" After performing the strip() function, \n Your string is ", res)
输出
Enter the string *** $$ Welcome to javatiku. Learn Python programming !!! &&
Your string is *** $$ Welcome to javatiku. Learn Python programming !!! &&
Enter any character or symbol that you don't want to see in the string * $ Wel ! &
After performing the strip() function,
Your string is come to javatiku. Learn Python programming
为什么要使用Python的strip()函数?
使用Python的strip函数的原因如下:
- 它帮助从第一个字符串的前面和后面删除字符,考虑到strip()函数之前的字符。
- 如果用户不传递字符给它,strip函数默认只删除字符串两端的空格。
- 如果在开头或结尾没有空格,它会返回原始字符串而不做任何更改。
- 如果传递的字符与原始字符串不匹配,strip函数会返回原始字符串。
结论:
在前面的主题中,我们了解了Python库的strip()函数。这是一个用于从第一个字符串的开头和结尾删除字符或空格的strip()函数。然而,如果用户不传递字符,strip()函数只会删除主字符串的前导和尾随空格。