zoukankan      html  css  js  c++  java
  • Python String 字符串内置方法 Cheatsheet

    >>> st='hello kitty {name} is {age}'
    >>> print(st) 
    hello kitty {name} is {age}
    >>> print(st.count('l'))       #  统计元素个数
    2
    >>> print(st.capitalize())     #  首字母大写  
    Hello kitty {name} is {age}
    >>> print(st.center(50,'#'))   #  居中
    ###########hello kitty {name} is {age}############
    >>> print('ABCD'.casefold())  # 把整个字符串的所有字符改为小写  # 使用后面的lower() 比较好     
    abcd
    >>> print(st.endswith('tty3')) #  判断是否以某个内容结尾
    False
    >>> print(st.startswith('he')) #  判断是否以某个内容开头
    True
    >>> print('hello	kitty'.expandtabs(tabsize=20)) 
    hello               kitty
    >>> print(st.find('t'))        #  查找到第一个元素,并将索引值返回
    8
    >>> print(st.format(name='alex',age=37))  # 格式化输出的另一种方式   待定:?:{}
    hello kitty alex is 37
    >>> print(st.format_map({'name':'alex','age':22}))
    hello kitty alex is 22
    >>> print(st.index('t'))
    8
    >>> print('asd'.isalnum())     # 判断是否包含数字和字母(汉字返回TRUE)
    True
    >>> print('12632178'.isdecimal())  # 判断十进制数
    True
    >>> print('1234.5678'.isdigit())       # 判断是否是整数
    False
    >>> print('1269999.uuuu'.isnumeric())
    False
    # isdigit()
    # True: Unicode数字,byte数字(单字节),全角数字(双字节),罗马数字
    # False: 汉字数字
    # Error: 无
    #####
    # isnumeric()
    # True: Unicode数字,全角数字(双字节),罗马数字,汉字数字
    # False: 无
    # Error: byte数字(单字节)
    >>> print('abc'.isidentifier())   # 判断标识符,值是否是合法变量名
    True
    >>> print('Abc'.islower())   # 判断是否全是小写
    False
    >>> print('ABC'.isupper())   # 判断是否全是大写
    True
    >>> print('  e'.isspace())
    False
    >>> print('My title'.istitle())   # 每个单词首字母大写
    False
    >>> print('*****'.join(['ABC','123456','hello']))       # 用一段字符连接另外几段字符 
    ABC*****123456*****hello
    >>> print('My tLtle'.lower())
    my tltle
    >>> print('My tLtle'.upper())
    MY TLTLE
    >>> print('My tLtle'.swapcase())    # 大小写翻转
    mY TlTLE
    >>> print('My tLtle'.ljust(50,'*'))     # 生成左对齐的一行
    My tLtle******************************************
    >>> print('My tLtle'.rjust(50,'*'))    # 右对齐
    ******************************************My tLtle
    >>> print('	My tLtle
    '.strip())     # 去掉开头结尾的空格、换行符
    My tLtle
    >>> print('	My tLtle
    '.lstrip())
    My tLtle
    
    >>> print('	My tLtle
    '.rstrip())
            My tLtle
    >>> print('ok')
    ok
    >>> print('My title title'.replace('itle','lesson',1))  # 替换,第三个参数标识替换几次,默认全替换
    My tlesson title
    >>> print('My title title'.rfind('t'))
    11
    >>> print('My title title'.split('i',1))     # 按照某个字符分割,第二个参数表示分割几次
    ['My t', 'tle title']
    >>> print('My title title'.title())
    My Title Title
    
    ################
    #摘一些重要的字符串方法
    # print(st.count('l'))
    # print(st.center(50,'#'))   #  居中
    # print(st.startswith('he')) #  判断是否以某个内容开头
    # print(st.find('t'))
    # print(st.format(name='alex',age=37))  # 格式化输出的另一种方式   待定:?:{}
    # print('My tLtle'.lower())
    # print('My tLtle'.upper())
    # print('	My tLtle
    '.strip())
    # print('My title title'.replace('itle','lesson',1))
    # print('My title title'.split('i',1))
    

      

  • 相关阅读:
    计算机考研复试真题 数字求和
    计算机考研复试真题 简单计算器
    计算机考研复试真题 N阶楼梯上楼问题
    P1082 [NOIP2012 提高组] 同余方程
    进制转换
    浮点数加法
    N的阶乘
    1055 The World's Richest (25 分)
    1028 List Sorting (25 分)
    1062 Talent and Virtue (25 分)
  • 原文地址:https://www.cnblogs.com/sddai/p/13955361.html
Copyright © 2011-2022 走看看