Pythonz字符串用法大全
简介
自学python过程中的记录
str
name = "Wangdc"
user = "my name is {names}"
print(name.capitalize()) # 首字母大写
print(name.count("i")) # 统计出现的次数
print(name.center(50,"-")) # 共50字符,不够用-补全
print(name.endswith("x")) # 判断是否以x结尾
# print(name.expandtabs(tabsize=30))
print(name[name.find("wx"):])
print(user.format(names = "Wangdc"))
print(user.format_map({"names" : "Wangdcx"}))
print(name.isalnum()) # 是不是阿拉伯数字
print(name.isalpha())
print("A16".isdecimal()) # 是不是十进制
print("A16".isdigit())
print("A16".isidentifier()) # 判断是否为一个合适的标识符变量名
print(name.islower()) # 是否为小写
print(name.isnumeric()) # 是否为数字(整数)
print(name.isspace()) # 是否为空格
print("My Name Is ".istitle()) # 是否首字母大写
print("My Name Is ".isprintable()) # tty文件,driver文件
print("My Name Is ".isupper()) # 是否都是大写
print(",".join(["1","2","3"])) # 将字符串加到列表值中间
print(name.ljust(50,"*"))
print(name.rjust(50,"*"))
print(name.lower()) # 转换为小写
print(name.upper()) # 转换为大写
print(name.lstrip()) # 从左边去除回车
print(name.rstrip())
print(name.strip()) # 去除所有空格和回车
p = str.maketrans("abcdefghi","123456789") # 随机密码
print("Wangdc".translate(p))
print("Wangdc".replace("w","W")) # 大小写替换
print("Wangdc".rfind("x")) # 从左往右面开始查找,查找最后一个匹配项
print("Wangdc name".split("i")) # 默认空格,可指定分隔符
print("Wangdc
ame".splitlines()) # 按换行符分割
print("Wangdc name".swapcase()) # 原来大写,输出小写
print("Wangdc name".title())
print("Wangdc name".zfill(50)) # 位数不够,用0填充