字符串(str)
test = "aLex"
print(test)
print(test.capitalize())#首字母大写,其他字母小写
print(test.lower())#所有字母小写,casefold
print(test.center(20, "*"))#设置宽度,并将内容居中;20代指总宽度;未知空白填充,只能一个字符,可有可无
v = "sdfghjkqwweeer"
print(v.count("we", 2, 15)) #字符出现次数,开始位置,结束位置
v1 = "alexaLex"
print(v1.endswith('x'))#以什么什么结尾
print(v1.endswith('a'))
print(v1.startswith('a'))#以什么什么开始
print(v1.find('ex', 5, 8))#从后开始往前找,>= 、<,返回字符位置,找不到返回-1
#expandtabs,断句
# test = "username\temail\tpassword\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123\n"
# print(test.expandtabs(20))
#格式化
test = "i an {name}, age {age}"
print(test)
v2 = test.format(name='yuqi', age=20)
print(v2)
test = "i an {0}, age {1}"
v3 = test.format('yoki', 22)
print(v3)
test = "i an {name}, age {age}"
v4 = test.format_map({"name": 'yq', "age": 19})#传入的值
print(v4)
v = "alexaLex"
v.index("ex")#找不到字符会报错
v5 = "abc123"
print(v5.isalnum())#判断字符串是否只包含字符和数字
print(v5.isalpha())#判断字符串是否只包含字母
#判断字符串是否只包含数字
v5 = "⑤"
print(v5.isdecimal())#十进制数字
print(v5.isdigit())#特殊的符号,比如:②
print(v5.isnumeric())#中文也支持,比如:二、三
#如果打印的时候都可以看到真实的值的时候,则输出true
test1 = "wertyuiofghj"
test2 = "sdfg\tssdsd"
print(test1.isprintable())
print(test2.isprintable())
#判断是否“全部”都是空格
test1 = "uyuiuhi oihioh"
test2 = " "
print(test1.isspace())
print(test2.isspace())
#判断是否是标题
test = "Be yourself ,everyone else is already taken"
print(test.istitle())
print(test.title())
print(test.title().istitle())
#字符串拼接,将字符串中的每一个元素按照指定分隔符进行拼接
test = "谁念西风独自凉?萧萧黄叶闭疏窗。沉思往事立残阳。被酒莫惊春睡重,赌书消得泼茶香。当时只道是寻常。"
v = "-"
print(v.join(test))
# print("+".join(test))
test = "alex"
# print(test.center(20, "*"))#设置宽度,并将内容居中;20代指总宽度;未知空白填充,只能一个字符,可有可无
print(test.ljust(20,'*'))
print(test.rjust(20,'*'))
print(test.zfill(20))#不能指定字符,默认用0填充
test = "ALex"
print(test.islower())#判断是否全部小写
print(test.lower())#检索时,无论用户输入什么,都变成小写再进行比较
print(test.lower().islower())
test = "ales"
print(test.isupper())
print(test.upper())
print(test.upper().isupper())
#去除空白,包括:" ",'\t','\n'
test = " yoki "
print(test.lstrip())
print(test.rstrip())
print(test.strip())
test = "\tyoki\n"
print(test)
print(test.strip())
#移除指定字符串
#优先最多匹配
test = "iugfasiugrwwuhhkhaiu"
print(test.rstrip('wwui'))
# 匹配替换
v1 = 'abcdef'
v2 = '123456'
v = 'Hope is a good thing ,maybe the best of things and no good thing ever dies'
m = str.maketrans(v1, v2)
print(v.translate(m))
# 字符串分割
test= "yokiyuqisaqdbqabv"
print(test.partition('q'))#只能分成三份
print(test.rpartition('q'))
# 字符串分割
test= "yokiyuqisaqdbqabv"
print(test.split('q', 3))#弊端:匹配到的字符拿不到;可以自定义匹配次数
print(test.rsplit())
# 分割,只能根据换行分割,true false:是否保留换行
test= "yoki\nyuqi\nsaqdb\nqabv"
print(test.splitlines(True))
print(test.splitlines(False))
#大写换小写,小写换大写
test = "yoki"
print(test.swapcase())
#标识符:字母、数字、下划线
# 判断是否是标识符
test = "yoki_yuqi"
print(test.isidentifier())
# 替换
test = "yokiyokiyoki"
print(test.replace('ki', 'qi'))
print(test.replace('ki', 'qi', 1))#只替换第一个
#通过索引的方式获取字符串的某一个字符
test = "yoki"
print(test[2])
#切片
print(test[0:1])
print(test[0:-1])
# 长度
test1 = "yoki"
test2 = "蒋雨奇"
print(len(test1))
print(len(test2))
#for循环
#for 变量名 in 字符串
# 变量名
test = "yoki"
for i in test:
print(i)
#帮助创建连续的数字
for item in range(100):
print(item)#0-99
#通过设置步长来指定不连续
for item in range(0, 100, 2):
print(item)
# 将文字对应的索引打印出来
test = input(">>>")
len = len(test)
for item in range(len):
print(item, test[item])
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律