美文网首页python整理
python方法:split()

python方法:split()

作者: 随风化作雨 | 来源:发表于2017-05-22 15:03 被阅读150次

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

    格式: str.split(str="", num=string.count(str))

    参数:
    str -- 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等
    num -- 分割次数

    返回值:
    返回分割后的字符串列表

    实例:
    以下实例展示了split()函数的使用方法:

    #!/usr/bin/python
    
    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']
    

    相关文章

      网友评论

        本文标题:python方法:split()

        本文链接:https://www.haomeiwen.com/subject/tfabxxtx.html