zoukankan      html  css  js  c++  java
  • python 常用 字符串操作(06)

    https://blog.csdn.net/weixin_43158056/article/details/92798114

    # -*- coding: cp936 -*-
    母串='cadefgh'
    子串='f'
    s3 = 母串.find(子串)#结果为4
    s4 = 母串.index(子串)#结果从0开始
    #find方法和index方法都是用来查找目标字符串的索引位置,当目标字符串不存在,find查询返回-1,index则抛出异常。”
    print(母串,子串, s3 , s4)
    
    #求前子串
    s='0123456n'
    s0=s[0:2]#前两个字符,其中0可省略
    s1=s[:2] # 01
    print(s0,s1)
    
    str = '0123456789′
    print str[0:3] #截取第一位到第三位的字符
    print str[:] #截取字符串的全部字符
    print str[6:] #截取第七个字符到结尾
    print str[:-3] #截取从头开始到倒数第三个字符之前
    print str[2] #截取第三个字符
    print str[-1] #截取倒数第一个字符
    print str[::-1] #创造一个与原字符串顺序相反的字符串
    print str[-3:-1] #截取倒数第三位与倒数第一位之前的字符
    print str[-3:] #截取倒数第三位到结尾
    print str[:-5:-3] #逆序截取

    将”to be or not to be”字符串倒序输出
    >>> 'to be or not to be'[::-1]
    将”sxtsxtsxtsxtsxt”字符串中所有的 s 输出
    >>> 'sxtsxtsxtsxtsxt'[::3]

    #求尾子串
    s='0123456n'
    s2=s[3:]  # 3456n
    print(s2)
    
    s.upper() #s大写

    # -*- coding: cp936 -*-母串='cadefgh'子串='f's3 = 母串.find(子串)#结果为4s4 = 母串.index(子串)#结果从0开始#find方法和index方法都是用来查找目标字符串的索引位置,当目标字符串不存在,find查询返回-1,index则抛出异常。”print(母串,子串, s3 , s4)
    #求前子串s='0123456n's0=s[0:2]#前两个字符,其中0可省略s1=s[:2]print(s0,s1)
    #求中子串


    #求尾子串s='0123456n's2=s[3:]print(s2)
    s.upper() #s大写

  • 相关阅读:
    1月10日 TextView
    1月9日 布局2
    30 Adapter适配器
    29 个人通讯录列表(一)
    28 ListView控件
    27 登录模板
    26 Activity的启动模式
    25 Activity的生命周期
    24 得到Activity返回的数据
    23 Activity的传值2(bundle)
  • 原文地址:https://www.cnblogs.com/tulater/p/13322677.html
Copyright © 2011-2022 走看看