zoukankan      html  css  js  c++  java
  • python3基础:介绍几个函数的用法


    1 find()函数:找到返回对应字符的索引 返回-1表示未找到字符串
    
    
    s = 'hello python apple'
    print(s.find('n'))
    print(s.find('5'))
    print(s.find('python'))   #返回字符串第一个索引值
    print(s.find('p',7))      #寻找目标子字符串  寻找开始索引位置
    
    

    输出结果

    11
    -1
    6
    14
    2  isdigit()函数:如果只包含数字,就返回true,否则返回false
    
    
    s = 'hello python apple'
    print(s.isdigit())
    print('111'.isdigit())

    输出结果

    False
    True
    3.  replace()函数    指定替换内容以及替换字符串,并且可以指定替换次数  默认全部替换
    s = 'hello python apple'
    new_s = s.replace('p','k')
    print(new_s)
    new_s = s.replace('p','k',1)   #  1表示替换的位置
    print(new_s)

    输出结果

    hello kython akkle
    hello kython apple

    4. split()函数 根据指定字符串经销切割

    s = 'hello python apple'
    print(s.split())  
    print(s.split(' ',1))   
    print(s.split('l'))     #  切割的字符为空字符串

    输出结果:

    ['hello', 'python', 'apple']
    ['hello', 'python apple']
    ['he', '', 'o python app', 'e']

    5. strip()函数:去掉头和尾指定的字符

    e = '@@@hello python apple@'
    e_1 = e.strip('@')
    print(e_1)

    输出结果

    hello python apple

    6. upper() 把字符串转换成大写

    lower()把字符串转换成小写
    s = 'hello python apple'
    s1="KfSHDfKJdfH"
    print(s.upper())
    print(s1.lower())

    输出结果:

    HELLO PYTHON APPLE
    kfshdfkjdfh

    7. swapcase()函数  大小写互换

    s1="KfSHDfKJdfH"
    print(s1.swapcase())

    输出结果:

    kFshdFkjDFh
     
    转载请附上原文链接。
  • 相关阅读:
    Cocos2d-x win7下 android环境搭建
    cocos2dx 环境搭建 win7 +vs2012+ cocos2dx-2.1.4
    IOS 通过界面图标启动Web应用 + 全屏应用 + 添加到主屏幕
    js 魔鬼训练
    远程调试 Weinre
    PHP uniqid 高并发生成不重复唯一ID
    html5 炫酷的字幕雨
    学习建模
    jquery 购物车飞入效果
    jquery/zepto 圣诞节雪花飞扬
  • 原文地址:https://www.cnblogs.com/bugbreak/p/12425193.html
Copyright © 2011-2022 走看看