方法 |
作用 |
示例 |
输出 |
upper |
全部大写 |
"hello".upper() |
"HELLO" |
lower |
全部小写 |
"Hello".lower() |
"hello" |
startswith() |
是否以a开头 |
"Yuan".startswith("Yu") |
True |
endswith() |
是否以a结尾 |
"Yuan".endswith("a") |
False |
isdigit() |
是否全数字 |
'123'.isdigit() |
True |
strip() |
去两边空格 |
" hi yuan \n".strip() |
"hi yuan" |
join() |
将多个字符串连接在一起 |
"-".join(["yuan","alvin","eric"]) |
"yuan-alvin-eric" |
split() |
按某字符分割字符串,默认按空格分隔 |
"yuan-alvin-eric".split("-") |
['yuan', 'alvin', 'eric'] |
find() |
搜索指定字符串,没有返回-1 |
"hello world".index("w") |
6 |
index() |
同上,但是找不到会报错 |
"hello world".index("w") |
6 |
count() |
统计指定的字符串出现的次数 |
"hello world".count("l") |
3 |
replace() |
替换old为new |
'hello world'.replace(‘world',‘python') |
"hello python" |
s1 = "yuan"
s2 = "RAIN"
s3 = s1.upper()
s4 = s2.lower()
print(s3)
print(s4)
s5 = "hello yuan"
s6 = "hi world"
print(s5.startswith("hello"))
print(s6.startswith("hello"))
print(s6.startswith("hi wor"))
print(s6.endswith("hi wor"))
s7 = "123"
s8 = "123A"
print(s7.isdigit())
print(s8.isdigit())
s9 = "123SAA%#"
print(s9.isalnum())
s10 = " I am yuan "
print(s10)
print(s10.strip())
name = input("请输入姓名>>").strip()
print(name)
s11 = "rain-yuan-alvin-eric"
print(s11.split("-"))
names_list = ['rain', 'yuan', 'alvin', 'eric']
s12 = "-".join(names_list)
print(s12,type(s12))
【5】类型转换
i = int("3")
print(i,type(i))
s = str(3.14)
print(s,type(s))
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)