zoukankan      html  css  js  c++  java
  • python_字符串

    a = '我不吃,我不喝,我就要钱'
    b = '太社会了'
    c = 'hello word!'
    #1. find是非贪婪模式,意思就是从左边开始找,找到一个输出即可,不管后边有无,
    #2. 而且是整体搜索
    find:
    print(a.find('我')) #在字符串a中寻找‘我’  输出结果:0
    print(a.find('我',2,5))  #在字符串a中的2-5中寻找‘我’  输出结果:4
    rfind:从右边开始找,但是索引正序的索引
    print(a.rfind('我'))  #从字符串a的右边中寻找‘我’  输出结果:8

    ##通常解决异常的方案:
    """ try:
        pass
    except expression as identifier:
        pass """
    # 在python中捕获一切错误的基类。
    # 当你不确定一个错误是否会发生的时候
    # try:
    #     print(a.find('小'))
    # except Exception as e: #出现异常,输出-1
    #     print(e)
    # print('hello') """

    # 2.# 检查字符串是否以指定的字符串开头
    print(a.startswith('我不吃'))  #true

    # 3.检查字符串是否以指定的字符串结尾
    print(c.endswith('!'))  # True

    # 4.将字符串以指定的宽度居中并在两侧填充指定的字符
    print(a.center(21, '*'))   #输出 *****我不吃,我不喝,我就要钱****

    # 5.将字符串以指定的宽度靠右放置左侧填充指定的字符
    print(b.rjust(20, ' '))   #输出:                太社会了

    # 6.# 通过len函数计算字符串的长度
    print(len(c))  # 11

    # 7. # 获得字符串首字母大写的拷贝
    print(c.capitalize())     #输出:Hello word!

    # # 8.# 获得字符串变大写后的拷贝
    print(c.upper())  # HELLO WORD!

    # 9.从字符串中取出指定位置的字符(下标运算)
    print(c[6])  # 输出 w

    # 10.字符串切片(从指定的开始索引到指定的结束索引)
    print(c[2:5])  # llo    从第2-5
    print(c[2:])  # llo word!  从2开始到最后
    print(c[2::2])  # lowr!    从第2个开使,步长为2
    print(c[::2])  # hlowr!     步长为2
    print(c[::-1])  # !drow olleh  倒序输出
    print(c[-3:-1])  # rd       从倒数第2个到倒数第3个

    # 11.检查字符串是否由数字构成
    print(c.isdigit())  # False

    # 12.检查字符串是否以字母构成
    print(c.isalpha())  # False

    # 13.检查字符串是否以数字和字母构成
    print(c.isalnum())  # False

    #14.
    str3 = '  jackfrued@126.com '
    print(str3)
    # 获得字符串修剪左右两侧空格的拷贝
    print(str3.strip())  #jackfrued@126.com
    print(str3.strip())  #jackfrued@126.com
    print(str3.rstrip())  #  jackfrued@126.com
  • 相关阅读:
    lintcode254- Drop Eggs- easy
    lintcode462- Total Occurrence of Target- easy
    lintcode63- Search in Rotated Sorted Array II- medium
    lintcode62- Search in Rotated Sorted Array- medium
    lintcode74- First Bad Version- medium
    lintcode75- Find Peak Element- medium
    lintcode160- Find Minimum in Rotated Sorted Array II- medium
    lintcode159- Find Minimum in Rotated Sorted Array- medium
    lintcode447- Search in a Big Sorted Array- medium
    VS2005、vs2008+WinXPDDK+DDKWizard配置驱动开发环境 分类: VC++ 2015-01-05 09:38 448人阅读 评论(0) 收藏
  • 原文地址:https://www.cnblogs.com/TiAmoLJ/p/11515207.html
Copyright © 2011-2022 走看看