• Python新特性


    字符串格式化输出
    1. # 旧版本
    2. name = "Archer"
    3. age = 18
    4. print("姓名:%s, 年龄:%d" %(name, age))
    5. # format格式化输出
    6. print("姓名:{}, 年龄:{}".format(name, age))
    7. # 新版本
    8. print(f"姓名:{name}, 年龄:{age}")
    9. a, b = 1, 2
    10. print(f"a+b={a+b}")
    1. # 使用=显示变量名
    2. a = "竹筒饭"
    3. b = 10
    4. print(f"{a=},{b=}") # a='竹筒饭',b=10
    5. name = "竹筒饭"
    6. # ^表示居中显示name,用*补全,总共显示20个字符
    7. # 旧版本
    8. print("{:*^20}".format(name))
    9. # 新版本
    10. print(f"{name:*^20}")
    11. # 居右显示
    12. print(f"{name:*>20}")
    13. # 居左显示
    14. print(f"{name:*<20}")
    15. # 数值数据格式化
    16. price = 9.151
    17. print("{:.2f}".format(price))
    18. print(f"{price:.2f}")
    19. """
    20. 所有输出:
    21. a='竹筒饭',b=10
    22. ********竹筒饭*********
    23. ********竹筒饭*********
    24. *****************竹筒饭
    25. 竹筒饭*****************
    26. 9.15
    27. 9.15
    28. """
    字符串新增方法
    方法名功能
    str.removeprefix()如果str以它开头,将返回一个删除过前缀的新字符串,否则它将返回原始字符串
    str.removesuffix()如果str以其结尾,则返回删除过后缀的新字符串,否则它将返回原始字符串
    1. info = "Archer"
    2. print(info.removeprefix("Ar")) # cher
    3. print(info.removeprefix("Saber")) # Archer
    4. print(info.removesuffix("er")) # Arch
    5. print(info.removesuffix("Saber")) # Archer
    字典新特性

            在Python3.10中,针对字典的三个方法items、keys、values都增加了一个mapping属性。

    1. mydict = {"one":1, "two":2, "three":3}
    2. # 旧版本
    3. print(f"{mydict.keys()}\n{mydict.values()}\n{mydict.items()}")
    4. # 新版本
    5. keys = mydict.keys()
    6. values = mydict.values()
    7. items = mydict.items()
    8. print(f"{keys.mapping}\n{values.mapping}\n{items.mapping}")
    9. """
    10. 输出结果
    11. # 旧版本
    12. dict_keys(['one', 'two', 'three'])
    13. dict_values([1, 2, 3])
    14. dict_items([('one', 1), ('two', 2), ('three', 3)])
    15. # 新版本
    16. {'one': 1, 'two': 2, 'three': 3}
    17. {'one': 1, 'two': 2, 'three': 3}
    18. {'one': 1, 'two': 2, 'three': 3}
    19. """
    字典合并

    字典添加两个新的运算符|=。分别用于合并字典和更新字典

    1. dict1 = {"name": "竹筒饭"}
    2. dict2 = {"age": 18}
    3. dict1.update(dict2)
    4. print(dict1) # {'name': '竹筒饭', 'age': 18}
    5. dict3 = dict1 | dict2
    6. print(dict3) # {'name': '竹筒饭', 'age': 18}
    7. dict1 |= dict2
    8. print(dict1) # {'name': '竹筒饭', 'age': 18}
    match语法

    match...case结构化模式匹配。类似于Java中的switch...case

    1. status = 300
    2. match status:
    3. case 200:
    4. print("访问成功")
    5. case 404:
    6. print("页面丢失")
    7. case _:
    8. print("阿巴阿巴..")
    9. # 阿巴阿巴..
    1. person1 = ("Archer", 23, "male")
    2. person2 = ("Saber", 22, "female")
    3. person3 = ("竹筒饭", 26, "***")
    4. def match_func(person):
    5. match person:
    6. case (name, _, "female"):
    7. print(f"{name} is woman")
    8. case (name, _, "male"):
    9. print(f"{name} is man")
    10. case (name, _, gender):
    11. print(f"{name} is {gender}")
    12. match_func(person1)
    13. match_func(person2)
    14. match_func(person3)
    15. # Archer is man
    16. # Saber is woman
    17. # 竹筒饭 is ***

  • 相关阅读:
    Java之IO流详解(三)——字符流
    离线部署神器yumdownloader
    SpringBoot中xml映射文件
    Linux下使用Nginx搭建Rtmp流媒体服务器,实现视频直播功能
    国际版腾讯云/阿里云:云解析DNS是什么
    九. Linux网络命令
    18、ajax、fetch、axios区别
    vue3 vite2 封装 SVG 图标组件 - 基于 vite 创建 vue3 全家桶项目续篇
    计算机毕业设计之java+ssm基于web的校园短期闲置资源置换平台
    vivo 消息中间件测试环境项目多版本实践
  • 原文地址:https://blog.csdn.net/qq_42797317/article/details/139672840