📜  Python字符串|条()

📅  最后修改于: 2022-05-13 01:55:26.585000             🧑  作者: Mango

Python字符串|条()

strip()是Python编程语言中的一个内置函数,它返回删除了前导和尾随字符的字符串副本(基于传递的字符串参数)。
句法:

string.strip([chars])
Parameter: 
There is only one optional parameter in it:
1)chars - a string specifying 
the set of characters to be removed. 

If the optional chars parameter is not given, all leading 
and trailing whitespaces are removed from the string.
Return Value:
Returns a copy of the string with both leading and trailing characters removed.
Python3
# Python3 program to demonstrate the use of
# strip() method 
 
string = """    geeks for geeks    """
 
# prints the string without stripping
print(string)
 
# prints the string by removing leading and trailing whitespaces
print(string.strip())  
 
# prints the string by removing geeks
print(string.strip(' geeks'))


Python3
# Python Program to demonstrate use of strip() method
 
str1 = 'geeks for geeks'
# Print the string without stripping.
print(str1)
 
# String whose set of characters are to be
# remove from original string at both its ends.
str2 = 'ekgs'
 
# Print string after stripping str2 from str1 at both its end.
print(str1.strip(str2))


Python3
# Python Program to demonstrate use of strip() method without any argument
str1 = """    geeks for geeks    """
 
# Print the string without stripping.
print(str1)
 
# Print string after removing all leading
# and trailing whitespaces.
print(str1.strip())


Python
# Python3 program to demonstrate the practical application
# strip()
 
 
string = " the King has the largest army in the entire world the"
 
# prints the string after removing the from beginning and end
print(string.strip(" the"))


输出:
geeks for geeks     
geeks for geeks
for

Python3

# Python Program to demonstrate use of strip() method
 
str1 = 'geeks for geeks'
# Print the string without stripping.
print(str1)
 
# String whose set of characters are to be
# remove from original string at both its ends.
str2 = 'ekgs'
 
# Print string after stripping str2 from str1 at both its end.
print(str1.strip(str2))
输出:
geeks for geeks
 for

上述代码的工作:
我们首先构造一个字符串str1 = 'geeks for geeks'
现在我们在 str1 上调用 strip 方法并将 str2 = 'ekgs' 作为参数传递。
现在Python解释器从左边开始跟踪str1。如果str2中存在str1的字符,它会删除它。
否则它将停止跟踪。
现在Python解释器从右边跟踪str1。如果 str1 的字符存在于 str2 中,它将删除它。
否则它将停止跟踪。
现在它终于返回了结果字符串。
当我们在没有参数的情况下调用 strip() 时,它会删除前导和尾随空格。

Python3

# Python Program to demonstrate use of strip() method without any argument
str1 = """    geeks for geeks    """
 
# Print the string without stripping.
print(str1)
 
# Print string after removing all leading
# and trailing whitespaces.
print(str1.strip())
输入:
geeks for geeks     

输出:

geeks for geeks

实际应用:
给定一个字符串,从开头和结尾删除单词“the”的出现。

Python

# Python3 program to demonstrate the practical application
# strip()
 
 
string = " the King has the largest army in the entire world the"
 
# prints the string after removing the from beginning and end
print(string.strip(" the"))
输入:
the King has the largest army in the entire world the

输出:

King has the largest army in the entire world