Python strip()方法学习

描述: strip()方法用于去除字符串头尾指定的字符(默认为空格或者 换行)

语法:

str.strip([chars])

参数chars 是指定字符

返回值:去除字符口串头尾指定的字符chars 生成的新字符串

e.g去除‘0’: str = 0012345000

      print(str.strip('0'))

结果: ‘12345’

e.g去除空格: str= '      study  up    '

     print(str.strip())

结果: 'study  up'

e.g指定字符'xy',包含x或y或xy去除 str = 'xabcdxy'

    print(str.strip('xy'))

结果:'abcd'

e.g 去除换行


结果: abc

             ee


结果: abc   ee







猜你喜欢

转载自blog.csdn.net/hou_angela/article/details/80607358