python strip()和split()函数

1.strip函数
  str.strip([chars])    返回移除字符串头尾指定的字符生成的新字符串

  例1:

              str = "00000003210Runoob01230000000";

              print str.strip( '0' );

  输出:3210Runoob0123

 例2:

             str2 = "   Runoob ";

             print str2.strip();

 输出:Runoob

例3:

            str = "123abcrunoob321"

            print (str.strip( '12' ))

输出:3abcrunoob3

2.split函数

str.split(str="", num=string.count(str)) 

通过指定分隔符对字符串进行切片,如果参数 num 有指定值,则仅分隔 num 个子字符串

例1:

    str = "Line1-abcdef \nLine2-abc \nLine4-abcd";
    print str.split( );
    print str.split(' ', 1 );

 输出:

   ['Line1-abcdef', 'Line2-abc', 'Line4-abcd']
   ['Line1-abcdef', '\nLine2-abc \nLine4-abcd']

猜你喜欢

转载自blog.csdn.net/u013952812/article/details/83584685