基本数据类型 str 的方法
name = "zhengkUI" new_name = name.capitalize() # 1.首字母大写,其它字母小写 print(new_name)
str1 = "abcd" str1_new = str1.upper() # 2.将字符串中的字母转成大写 print(str1_new)
str2 = "AbCDE" str2_new = str2.lower() # 3.将字符串中的字母转成小写 print(str2_new)
name = "zhengkui" new_name = name.center(20,"-") # 4.字符串居中(共多少个字符,空位用什么补充-补充的字符必须是单字符) print(new_name)
str1 = "zhengkui lisi abao keke" num = str1.count("en") # 5.子序列的个数-可以是单个字符的字符串也可以是多个字符的字符串 print(num)
temp = "for windows" print(temp.endswith("ws")) # True 6.是否以xx字符串结尾
temp = "for windows" print(temp.endswith("r", 0, 3)) # True 6.是否以r字符串结尾,范围包含头,不包含尾。
temp = "for\tMac" print(temp.expandtabs()) # 7.将Tab默认键替换成8个空格 print(temp.expandtabs(30)) # 将Tab键替换成30个空格
temp = "Hello Python" print(temp.find("l")) # 8.从前往后查找l第一次出现在第几个位置 print(temp.find("A")) # 如果没找到就返回-1 print(temp.find("y", 2, 8)) # 从第几个位置找到第几个位置
temp = "name:{0} age:{1}" # {0} 代表占位符 print(temp.format("kui", 22)) # 9.将占位符格式化成指定的字符
temp = "abc123" print(temp.isalnum()) # True 10.判断字符串中只包含字母和数字
temp = "abc123" print(temp.isalpha()) # False 11.判断字符串中只包含字母
temp = "abc123" print(temp.isnumeric()) # False 12.判断字符串中只包含数字
temp = "abc123" print(temp.islower()) # True 13.判断字符串中的所有字母是否为小写
temp = "abc123" print(temp.isupper()) # False 14.判断字符串中包含的所有字母是否为大写
temp = " " print(temp.isspace()) # True 15.判断字符串是否为空格
temp = "Hello Python..." print(temp.istitle()) # True 16.判断字符串是否是标题(首字母是否为大写)
temp = ["zhangSan", "liSi", "wangWu"] mark = "_" new_temp = mark.join(temp) # 17.将字符串标记连接到可迭代的序列中 print(new_temp)
temp = "Hello Python" print(temp.ljust(30, "-")) # 18.左对齐后,右边用某种字符填充。
temp = "Hello Python" print(temp.rjust(30, "-")) # 19.右对齐后,左边用某种字符填充。
temp = " Hello Python " new_temp = temp.lstrip() # 20.移除左边的空格 print(new_temp)
temp = " Hello Python " new_temp = temp.rstrip() # 21.移除右边的空格 print(new_temp)
temp = " Hello Python " new_temp = temp.strip() # 22.移除左右两边的空格 print(new_temp)
temp = "HelloPython2018" new_temp = temp.partition("Python") # 23.用字符串中的第一个字符串作为分割标志进行分割字符串,分割后的字符串会成为一个元组。 print(new_temp)
temp = "Hello_Python_2018" new_temp = temp.replace("_", "-") #24. 替换字符中的字符串 print(new_temp)
temp = "fhwofjwifrhwlk" new_temp = temp.split("w") # 25.用字符串中的字符分割成一个字符串列表(从左往右) print(new_temp)
temp = "fhwofjwifrhwlk" new_temp = temp.rsplit("w") # 26.用字符串中的字符分割成一个字符串列表(从右往左) print(new_temp)
temp = "Hello Python..." print(temp.startswith("He")) # True 27.判断一个字符串是否以某个字符串开头
temp = "Hello Python..." new_temp = temp.swapcase() # 28.交换大小写字符(大写变小写,小写变大写) print(new_temp)
temp = "hello Python..." new_temp = temp.title() # 29.变成标题(首字母变成大写) print(new_temp)
####################################################
# 一、增
# # 1.使用+号
# old_str = "process finished with exit code 0"
# new_str1 = old_str+" ABC"
# print(new_str1)
# 二、删
# 1.删除左边的空字符串
# old_str = " process finished with exit code 0 "
# new_str = old_str.lstrip()
# print(new_str)
# 2.删除右边的空字符串
# old_str = " process finished with exit code 0 "
# new_str = old_str.rstrip()
# print(new_str)
# 3.删除左边指定的字符串
# old_str = "process finished with exit code 0 "
# new_str = old_str.lstrip("pro") # 删除不成功也不会报错
# print(new_str)
# 4.删除左右空格
# old_str = " process finished with exit code 0 "
# new_str = old_str.strip()
# print(new_str)
# 三、改
# 1.替换字符串
# old_str = "process finished with exit code 0 "
# new_str = old_str.replace("pro", "PRO") # 会生成一个新的字符串
# print(new_str)
# 2.首字母大写
# old_str = "process finished with exit code 0 "
# new_str = old_str.title()
# print(new_str)
# 3.首字母小写
# old_str = "A. process finished with exit code 0 "
# new_str = old_str.lower()
# print(new_str)
# 4.大小写转换
# old_str = "process finished with EXIT code 0 "
# new_str = old_str.swapcase()
# print(new_str)
# 5.指定长度左对齐
# old_str = " process finished with EXIT code 0"
# new_str = old_str.ljust(40, "-")
# print(new_str)
# 6.指定长度右对齐
# old_str = " process finished with EXIT code 0"
# new_str = old_str.rjust(40, "-")
# print(new_str)
# 7.字符串居中
# old_str = " process finished with EXIT code 0"
# new_str = old_str.center(80, "-")
# print(new_str)
# 8.切割
# old_str = "process finished with EXIT code 0"
# new_str = old_str.split(" ")
# print(new_str)
# 9.将占位符替换成只指定的字符串
# old_str = "name:{0} age:{1}"
# new_str = old_str.format("keke", 33)
# print(new_str)
# 9.将指定的字符串连接到可迭代的字符序列中
# old_str = "process finished with EXIT code 0"
# new_str = "-".join(old_str)
# print(new_str)
# 四、查
# 1.字符串从左往右第一次出现的位置索引
# old_str = "process finished with EXIT code 0 "
# index_num = old_str.index("c")
# print(index_num)
# 2.统计字符串出现的次数
# old_str = "process finished with EXIT code 0 "
# re_str = old_str.count("i")
# print(re_str)
# 五、判断
# 1.首字母是否是大写
# old_str = "Hello "
# is_title = old_str.istitle()
# print(is_title)
# 2.首字母是否是小写
# old_str = "hello "
# is_title = old_str.islower()
# print(is_title)
# 3.字符串是否全为字母
# old_str = "hello"
# is_alpha = old_str.isalpha()
# print(is_alpha)
# 4.字符串是否全为数字
# old_str = "1234"
# is_num = old_str.isnumeric()
# print(is_num)
# 5.字符串是否只包含字母和数字
# old_str = "sfw1234w"
# is_al_num = old_str.isalnum()
# print(is_al_num)
# 6.字符串是否以某个字符串开头
# old_str = "Hello Python"
# is_start = old_str.startswith("He")
# print(is_start)
# 7.字符串是否以某个字符串结尾
# old_str = "Hello Python"
# is_end = old_str.endswith("n")
# print(is_end)
# 8.字符串是否为空字符串
# old_str = " "
# is_none = old_str.isspace()
# print(is_none)