zoukankan      html  css  js  c++  java
  • python基础:字符串切割函数split

    字符串切割函数split,以及需要留意的深坑!

    # 字符串切割
    str = "我的昵称是奔奔,我的年龄是18,我的爱好是python"
    res = str.split(",")
    print(res)
    # 指定切割次数
    res = str.split(",", 1)
    print(res)
    
    # TODO: 如果切割符在左右两端. 那么⼀定会出现空字符串.深坑请留意
    str = "奔奔是我QQ的昵称,奔奔也是我微信的昵称,奔奔喜欢写python"
    res = str.split("奔奔")
    # ['', '是我QQ的昵称,', '也是我微信的昵称,', '喜欢写python']
    print(res)
    
    str = "我QQ的昵称是奔奔,我微信的昵称也是奔奔,喜欢写python的奔奔"
    res = str.split("奔奔")
    # ['我QQ的昵称是', ',我微信的昵称也是', ',喜欢写python的', '']
    print(res)

    运行结果:

    ['我的昵称是奔奔', '我的年龄是18', '我的爱好是python']
    ['我的昵称是奔奔', '我的年龄是18,我的爱好是python']
    ['', '是我QQ的昵称,', '也是我微信的昵称,', '喜欢写python']
    ['我QQ的昵称是', ',我微信的昵称也是', ',喜欢写python的', '']
  • 相关阅读:
    Bzoj1072--Scoi2007排列perm
    Bzoj1041--Haoi2008圆上的整点
    Bzoj3932--Cqoi2015任务查询系统
    HDU 1024 Max Sum Plus Plus(DP)
    HDU 1029 Ignatius and the Princess IV
    【noip模拟题】数列
    Hello World
    vue-router 进阶
    vue2.0 源码解读(二)
    vue2.0 源码解读(一)
  • 原文地址:https://www.cnblogs.com/benben-wu/p/13092240.html
Copyright © 2011-2022 走看看