• python字符串


    #查看字符串对象有哪些方法
    str1 = 'admin'
    print(dir(str1))
    【结果】

    ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

    带下划线的方法全部忽略,不需要了解

    #字符串替换
    print(str1.replace('admin','baidu'))
    【结果】baidu abc
    #索引
    print(str1.find('m'))
    【结果】2

    在接口测试中用得比较多的字符串方法
    #字符串是否以...开头
    print(str1.startswith('a'))
    【结果】True

    #字符串以...结果
    print(str1.endswith('m'))
    【结果】False

    #字符串是不是数字
    print(str1.isdigit())
    【结果】False

    #字母大小写切换
    print(str1.upper())
    print(str1.lower())

    #字符串拆分
    str2 = 'hello,little boy'
    str3 = str2.split(',')
    print(str3)
    print(type(str3))
    【结果】['hello', 'little boy']       <class 'list'>
    #将列表合并成字符串
    str4 = ':'.join(str3)
    print(str4)
    【结果】hello:little boy

    #  %格式化,即用%作为占位符
    name = 'a litte tree'
    age = '6'
    print('我的名字是%s,我今年%s岁了'%(name,age))
    # format格式化,用{}作为占位符,推荐
    print('我的名字是{0},我今年{1}岁了'.format(name,age))
    【结果】

    我的名字是a litte tree,我今年6岁了
    我的名字是a litte tree,我今年6岁了





  • 相关阅读:
    SAR图像处理 MSTAR数据库利用问题
    python 获取系统环境变量 os.environ and os.putenv
    python 模块中的 __init__.py __main__.py
    pylint python2.7 安装记录
    Python--字典
    哈希表
    AC自动机模板
    平衡树(Splay)模板
    矩阵快速幂 模板
    非递归线段树
  • 原文地址:https://www.cnblogs.com/zyamei/p/11389592.html
Copyright © 2020-2023  润新知