# Python Case 示例代码
def string_case():
# 字符串大小写转换示例
text = "Hello, World!"
# 将字符串转换为全大写
upper_case = text.upper()
print(f"Upper case: {upper_case}") # 输出: Upper case: HELLO, WORLD!
# 将字符串转换为全小写
lower_case = text.lower()
print(f"Lower case: {lower_case}") # 输出: Lower case: hello, world!
# 将字符串的首字母大写,其余小写
title_case = text.title()
print(f"Title case: {title_case}") # 输出: Title case: Hello, World!
def match_case_example(value):
# Python 3.10 引入的 match-case 语句示例
match value:
case 0:
print("Value is zero")
case 1:
print("Value is one")
case _:
print("Value is something else")
if __name__ == "__main__":
string_case()
match_case_example(1)
string_case():
upper() 方法将字符串中的所有字符转换为大写。lower() 方法将字符串中的所有字符转换为小写。title() 方法将字符串中每个单词的首字母大写。match_case_example():
match-case 语法,类似于其他语言中的 switch-case 语句。match 关键字后面跟着要匹配的值,case 后面跟着匹配的模式。_ 是一个通配符,表示如果前面的条件都不匹配,则执行这一分支。上一篇:python env
下一篇:python gil
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站