zoukankan      html  css  js  c++  java
  • python字符串魔法

    test = 'alEx'
    
    print(test.capitalize())  # 首字母大写 其他都变小写
    
    print(test.casefold())  # 变小写 更牛逼
    
    print(test.lower())  # 全部变小写
    
    print(test.center(20, '*'))  # 设置宽度,一共20个位置,将test放中间,其他用*拼接
    
    print(test.count('E', 1, 2))  # test中存在E的数量,从哪开始到哪结束,不填表示从头到尾,左开右闭
    
    print(test.endswith('x'))  # 判断是否以x结尾
    
    print(test.endswith('E', 2, 4))
    
    print(test.startswith('a'))  # 以a开始
    
    test = 'alExalEx'
    
    print(test.find('E'))  # 从前往后找寻找E出现在哪个位置(首次),可以加位置,返回-1代表没找到
    
    print(test.index('E'))  # 未找到就报错
    
    test = 'I am {name}'
    
    print(test.format(name='alex'))  # format格式化
    
    print(test.format_map({'name': 'alex'}))
    
    test = 'afds324353dcz3fads5sd中'
    
    print('*******')
    print(test.isalnum())  # 只有数字和字母的时候返回True,汉字也可以
    
    print(test.isalpha())  # 判断是否只是字母
    
    s = 'fasd	324	wklds'
    
    print(s.expandtabs(3))  # 三个一组寻找	 找到之后剩余的用空格填充
    
    test = '123'
    
    print(test.isdigit())  # 判断是否只是数字
    
    print(test.isdecimal())  # 是否是数字 有局限
    
    print(test.isnumeric())
    
    test = '_qw12'
    
    # 字母 数字 下划线
    print(test.isidentifier())  # 判断是否符合标识符
    
    test = 'asdfh	jfas
    '
    # 是否存在不可显示的字符
    
    print(test.isprintable())
    
    test = '    '
    print(test.isspace())  # 判断是否是空格
    
    test = 'hello world'
    
    print(test.title())  # 转换成标题
    
    print(test.istitle())  # 判断是否是标题
    
    test = '你是风儿我是沙'
    
    print(' '.join(test))  # 插入空格
    
    print(test.center(20, '*'))  # 设置宽度,一共20个位置,将test放中间,其他用*拼接
    
    test = 'alexA'
    
    print(test.ljust(20, '*'))
    
    print(test.rjust(20, "*"))
    
    print(test.zfill(20))  # 前边用0填充
    
    print(test.lower())  # 转化成小写
    
    print(test.islower())  # 判断是否全部是小写
    
    print(test.upper())  # 转化成大写
    
    print(test.isupper())  # 判断是否是大写
    
    test = '  ale x  '
    
    print(test.lstrip())  # 去除左边空格,换行
    
    print(test.rstrip())  # 去除右边空格
    
    print(test.strip())  # 去除两边空格
    
    test = 'alex'
    print(test.lstrip('a'))  # 去除a(以a开头)
    
    v = 'aeiuo'
    m = str.maketrans('aeiou', '12345')
    print(v.translate(m))  # 替换
    
    test = 'alexafdsfffsiensfls'
    
    print(test.partition('s'))  # 以s做分割
    
    print(test.rpartition('s'))
    
    print(test.split('s'))  # 分割
    
    print(test.rsplit('s'))
    
    test = 'afsd
    fda'
    
    print(test.splitlines())  # 根据换行分割

    print('name', 'alex', 'age', '18', sep=':')  # 字符串拼接

    import string


    values = {'var': 'foo'}

    t = string.Template('''
    Variable: $var
    Escape: $$
    Variable in text: ${var}iable
    ''')
    print('TEMPLATE:', t.substitute(values))

    s = '''
    Variable: %(var)s
    Escape: %%
    Variable in text: %(var)siable
    '''
    print('INTERPOLATION', s % values)

    s = '''
    Variable: {var}
    Escape: {{}}
    Variable in text: {var}iable
    '''
    print('FORMAT', s.format(**values))

    t = string.Template('$var')
    print(t.pattern.pattern)
  • 相关阅读:
    JS基础算法题(一)
    JS数组的迭代器方法
    使用IcoMoon制作字体图标
    CSS浮动属性
    使用hibnate向数据库写入图片对象
    hibernate_基本配置和初始化步骤
    ORM框架
    Jsp中的session
    mysql的自我复制
    分页技术
  • 原文地址:https://www.cnblogs.com/majianyu/p/10129616.html
Copyright © 2011-2022 走看看