这个 strip()方法 Python的内置函数用于删除字符串中的所有前导空格和尾随空格。
null
语法: 一串条带([字符])
参数:
字符(可选): 需要从字符串中删除的字符或一组字符。
返回: 删除前导字符和尾随字符的字符串副本。
使用strip()方法:
- 如果左边字符串的字符与 烧焦 参数时,该方法停止删除前导字符。
- 如果右侧字符串的字符与中的字符不匹配 烧焦 参数时,该方法停止删除尾随字符。
示例#1:
Python3
# Python code to illustrate the working of strip() string = ' Geeks for Geeks ' # Leading spaces are removed print (string.strip()) # Geeks is removed print (string.strip( ' Geeks' )) # Not removed since the spaces do not match print (string.strip( 'Geeks' )) |
输出:
Geeks for Geeksfor Geeks for Geeks
示例2:
Python3
# Python code to illustrate the working of strip() string = '@@@@Geeks for Geeks@@@@@' # Strip all '@' from beginning and ending print (string.strip( '@' )) string = 'www.Geeksforgeeks.org' # '.grow' removes 'www' and 'org' and '.' print (string.strip( '.grow' )) |
输出:
Geeks for GeeksGeeksforgeeks
示例#3: 下面的代码显示了strip()在python中的应用。
Python3
# Python code to check for identifiers def Count(string): print ( "Length before strip()" ) print ( len (string)) # Using strip() to remove white spaces str = string.strip() print ( "Length after removing spaces" ) return str # Driver Code string = " Geeks for Geeks " print ( len (Count(string))) |
输出:
Length before strip()17Length after removing spaces15
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END