zoukankan      html  css  js  c++  java
  • 字符串的格式化format和字符串相关函数

    一 字符串的格式化format

    1.1 顺序传参:

    """{}是占位符"""
    strvar = "{}向{}开了一枪,饮蛋而亡".format("小张","小菲")
    print(strvar)

    1.2索引传参:

    strvar = "{1}向{0}开了一枪,饮蛋而亡".format("小张","小菲")
    print(strvar)

    1.3关键字传参:

    strvar = "{who1}从后面掏了{who2}一下,回头一记天马流星拳".format(who1="小王",who2="小方")
    print(strvar)

    1.4容器类型数据(列表或元组活着的字典)传参:

    strvar = "{0[1]}亲了{1[0]}一下,鼻血直冒三万多尺".format(["小王","建康"],("荷叶","永捐"))
    strvar = "{group2[1]}亲了{group1[1]}一下,鼻血直冒三万多尺".format(group1=["小王","建康"],group2 = ("荷叶","永捐"))
    # group2[yj] 如果是字典在format格式化字符串的使用中,不能加上引号""
    strvar = "{group2[yj]}亲了{group1[0]}一下,鼻血直冒三万多尺".format(group1=["小王","建康"],group2 = {"wz":"小王","yj":"永捐"})
    print(strvar)

    1.5 format的填充符号的使用( ^ > < )

    """
    ^  原字符串居中
    >  原字符串居右
    <  原字符串居左
    
    {who:*^10}
    who : 关键字传参
    *   : 要填充的符号
    ^   : 原字符串居中
    10  : 填充符号的个数 + 原字符串的个数
    
    如果不写填充符号,默认填充空格;
    """
    strvar = "{who:*^10}在{where:>>10},坐着{dosomething:!<10},感觉{feel:^10}".format(who="雅琪",where="电影院",dosomething="吃饭",feel="自己萌萌哒")
    print(strvar)

    1.6 进制转换等特殊符号的使用( :d :f :s :, )

    # :d 整型占位符 (必须是整型)
    strvar = "子豪,昨天晚上买了{:d}个风油精".format(100)
    # :3d 占三位
    strvar = "子豪,昨天晚上买了{:^3d}个风油精".format(3)
    print(strvar)
    
    # :f 浮点型占位符 (必须是浮点型) 默认小数点保留六位
    strvar = "盛林毕业了,一个月工资:{:f}".format(9.91234567)
    # :.2f 小数点保留两位 存在四舍五入
    strvar = "盛林毕业了,一个月工资:{:.2f}".format(9.91934567)
    print(strvar)
    
    # :s 字符串占位符 (必须是字符串) 
    strvar = "{:s}".format("欢庆真胖~")
    print(strvar)
    
    # :, 金钱占位符
    strvar = "{:,}".format(12345678)
    print(strvar)

    二 . 字符串相关函数

    part1:

    *capitalize 字符串首字母大写 

    strvat = 'how old sre you'
    
    res = strvar.capitalize()
    
    print(res)

    # *title 每个单词的首字母大写

    strvar = 'how are you'
    res = strvar.title()
    print(res)

    # *upper 将所有字母变成大写

    strvar = 'to be or NOT to be'
    res = strvar.upper()
    print(res)

    # *lower 将所有字母变成小写 

    res = strvar.lower()
    print(res)

    # *swapcase 大小写互换 

    res = strvar.swapcase()
    print(res)

    # *len 计算字符串的长度

    res = len(strvar)
    print(res)

    # *count 统计字符串中某个元素的数量 

    """count(字符,[start,end])"""
    strvar = "to be or NOT to be"
    res = strvar.count("b")
    res = strvar.count("b",4)
    res = strvar.count("b",4,8)
    print(res)

    # *find 查找某个字符串第一次出现的索引位置 (推荐)

    """find(字符,[start,end]) 如果找不到对应字符,返回-1"""
    strvar = "oh father this is my favorate dog"
    res = strvar.find("this")
    res = strvar.find("is",15)
    res = strvar.find("is",15,16) # -1
    print(res)

    # *startswith 判断是否以某个字符或字符串为开头 

    """startswith(字符,[start,end]) 成立返回True,反之返回False"""
    strvar = "oh father this is my favorate dog"
    res = strvar.startswith("oh")
    res = strvar.startswith("oh",3)
    res = strvar.startswith("father",3,9)
    print(res

    # *endswith 判断是否以某个字符或字符串结尾

    """endswith(字符,[start,end]) 成立返回True,反之返回False"""
    strvar = "oh father this is my favorate dog"
    res  = strvar.endswith("dog")
    res  = strvar.endswith("dog",-4)
    res  = strvar.endswith("dog",-4,-2) # d
    print(res)

    part2:

    # *isupper 判断字符串是否都是大写字母

    strvar = 'TO BE'
    res = strvar.isupper()
    print(res)

    # *islower 判断字符串是否都是小写字母 

    res = strvar.islower()
    print(res)

    # *isdecimal 检测字符串是否以数字组成  必须是纯数字

    strvar = "12345"
    res = strvar.isdecimal()
    print(res)

     part3

    # *split 按某字符将字符串分割成列表(默认字符是空格)

    """split("字符",切割次数) 从左至右"""
    strvar = "you can you up no can no bb"
    lst = strvar.split()
    strvar = "you@can@you@up"
    lst = strvar.split("@")
    # 从左至右切割
    lst = strvar.split("@",1)
    # 从右至左切割
    lst = strvar.rsplit("@",1)
    print(lst)

    # *join  按某字符将列表拼接成字符串(容器类型都可

    lst = ['you','can','you','up']
    res = "@".join(lst)
    print(res)

    # *center 填充字符串,原字符居中 (默认填充空格)

    strvar = "***"
    res = strvar.center(10) # 默认填充空格
    res = strvar.center(10,"#") # 可选择填充的符号
    print(res)

    # *strip  默认去掉首尾两边的空白符(空格 ... ) 

    strvar = "               刘德华    "
    print(strvar.strip())                            
    #rstrip 去掉右边某个字符 
    print(strvar.rstrip())
    #lstrip 去掉左边某个字符 
    print(strvar.lstrip())

    # replace() 把字符串的旧字符换成新字符

    """
        功能:   把字符串的旧字符换成新字符
        格式:   字符串.replace('旧字符','新字符'[, 限制替换的次数])
        返回值: 替换之后的字符串
    """
    
    strvar = "可爱的小狼狗喜欢吃肉,有没有,有没有,还有没有"
    res = strvar.replace("有没有","真没有")
    # 替换一次
    res = strvar.replace("有没有","真没有",1)
    print(res)
  • 相关阅读:
    LeetCode 226. Invert Binary Tree
    LeetCode 221. Maximal Square
    LeetCode 217. Contains Duplicate
    LeetCode 206. Reverse Linked List
    LeetCode 213. House Robber II
    LeetCode 198. House Robber
    LeetCode 188. Best Time to Buy and Sell Stock IV (stock problem)
    LeetCode 171. Excel Sheet Column Number
    LeetCode 169. Majority Element
    运维工程师常见面试题
  • 原文地址:https://www.cnblogs.com/whc6/p/14056784.html
Copyright © 2011-2022 走看看