大小写转换
1 __author__ = "KuanKuan" 2 print("name is JankinYu".capitalize())#转为首字母大写 3 print("name is JankinYu".title())#转为标题形式 Name Is Jankinyu 4 print("NAMW".lower())#转换为小写 5 print("name".upper())#转换为大写 6 print("Name".swapcase())#大写变小写小写变大写 7 """ 8 输出结果: 9 Name is jankinyu 10 Name Is Jankinyu 11 namw 12 NAME 13 nAME 14 """
字符串判断
1 __author__ = "KuanKuan" 2 print("name is JankinYu".startswith("name")) # 以...开头 如果正确则为True 3 print("name is JankinYu".endswith("JankinYu"))#以...结尾 如果正确则为True 4 print("asd".isalpha())#如果是字母则为True 5 print("123".isdigit())#如果是数字则为True 6 print("asd123".isalnum())#如果是数字或者字母则为True 7 print("jjk".isidentifier())#判断是否为合法变量名合法为True 8 print(" ".isspace()) # 空格 -->True 9 print("My Name Is Jankinyu".istitle()) # 标题 -->True 10 print("My Name Is Jankinyu".isprintable()) # 可打印 tty file, drive file -->True 11 print("A".isupper()) # 大写 -->True 12 print("a".islower()) # 小写 -->True
输出显示
1 __author__ = "KuanKuan" 2 print(",".join(["1", "2", "3"])) # 连接 3 # ->1,2,3 4 print("name is JankinYu".split()) # 拆分为列表,默认拆分符为空格 5 # ->[‘name‘, ‘is‘, ‘Alex‘] 6 print("name\n is\r\n JankinYu".splitlines()) # 拆分为列表,默认拆分符为回车 7 # ->[‘name‘, ‘ is‘, ‘ Alex‘] 8 print("name".center(50, "-")) # 内容居中,两侧填充 9 # ->-----------------------name----------------------- 10 print("name".ljust(50, "*")) # 内容靠左,以*填充 11 # ->name********************************************** 12 print("name".rjust(50, "-")) # 内容靠右,以-填充 13 # ->----------------------------------------------name 14 print("name".zfill(50)) # 内容靠右,以0填充 15 # ->0000000000000000000000000000000000000000000000name 16 print("name is\t JankinYu".expandtabs(tabsize=18)) # 设置‘\t‘宽度 17 # ->name is JankinYu 18 print("I am from {city},and {year} years old".format(city="kuankuan", year=18)) # 格式化 19 # ->I am from Wuhan,and 18 years old 20 print("I am from {city},and {year} years old".format_map({"city": "kuankuan", "year": 18})) # 字典参数格式化 21 # ->I am from Wuhan,and 18 years old 22 print("\nname".lstrip()) # 去除左侧回车和空格 ->name 23 print("name\n".rstrip()) # 去除右侧回车和空格 ->name 24 print(" name\n".strip()) # 去除两侧回车和空格 ->name 25 print("~~~~~~~~~~~~~~~~~~~~")
查找替换
1 __author__ = "KuanKuan" 2 p = str.maketrans("abc", "123") # 创造翻译字典 3 print(type(p)) # --><class ‘dict‘> 4 print("abc".translate(p)) # 翻译,参数:dict -->123 5 print("name is JankinYu".replace("n", "N")) # 替换 -->Name is JankinYu 6 print("name is JankinYu".count("i")) # 字符计数 -->2 7 print("name is JankinYu".find("n")) # 查找,返回下标,没有则返回-1 -->2 8 print("name is JankinYu".rfind("n")) # 右边开始查找 -->13 9 print("name is JankinYu".index("n")) # 返回索引,不存在则报错 -->0 10 print("name is JankinYu".rindex("n")) # 右边开始查找 -->13 11 """ 12 输出结果: 13 <class ‘dict‘> 14 123 15 Name is JaNkiNYu 16 2 17 0 18 13 19 0 20 13 21 """
原文地址:https://www.cnblogs.com/JankinYu/p/8455490.html
时间: 2024-11-05 13:07:14