字符串的用法
前两天问小朋友冰箱上的Siemens是什么意思,被答是睡觉的席梦思。问整型和长整型的区别,答是拉双眼皮和经常拉双眼皮。语言真的是神奇的东西。想开始归纳一些常用的用法方便自己查阅。
先从字符串说起
telma = "franÇais" a = telma.capitalize() #首字母大写 b = telma.casefold() # 变小写,包括各种特殊文字 c = telma.lower() # 变小写 常用 d = telma.center(20, "大") # 宽度, 填充字符,只能填一个字符,汉字也可以,2个就不行 print(a) print(b) print(c) print(d)
运行结果
Français français français 大大大大大大franÇais大大大大大大
telma = "itisabeautifulnight"
a = telma.count("i") # 去字符串中寻找子字符串的数目
b = telma.count("i", 5, 17) # 去字符串中寻找子字符串的数目 开始结束位置 比如第一个i前后分别为位置0,1
c = telma.endswith("ht") # 以什么什么结束
d = telma.endswith("i",0,3) #在一定范围内以什么什么结束
e = telma.startswith("iti") # 以什么什么开始
print(a)
print(b)
print(c)
print(d)
print(e)
4 2 True True True
telma = "我爱你塞北的雪" #0(-7)我1(-6)爱2(-5)你3(-4)塞4(-3)北5(-2)的6(-1)雪7
print(telma[0])
print(telma[4:6])
print(telma[4:-2])
print(telma[-7:-2])
print(len(telma))
index = 0
while index < len(telma):
i = telma[index]
index += 1
print(i)
print("~~~~")
for _ in telma:
print(_)
我 北的 北 我爱你塞北 7 我 爱 你 塞 北 的 雪 ~~~~ 我 爱 你 塞 北 的 雪
#通过tab制表, telma = "name\tage\tsex\nChris\t25\tM\nLucy\t24\tF\nJohn\t23\tM" a = telma.expandtabs(6) # 6代表将整个字段包括tab延长到6 print(a)
name age sex Chris 25 M Lucy 24 F John 23 M
telma = "couldyouplssayhellotoher?"
a = telma.find("ou") #查找第一个位置
b = telma.find("ou", 5, 8) #在具体位置内查找
c = telma.find("ou", 6, 7)
print(a)
print(b)
print(c)
1 6 -1
# format将占位符换成指定内容,可以定义变量名也可以按出现顺序0,1
telma = "I am {name} and I am {age}"
a = telma.format(name = "Chris", age = 33)
print(a)
amlet = "I am {0} and I am {1}"
b = amlet.format("Chris", 33)
print(b)
c = telma.format_map({"name":"Chris", "age":33}) # 记得有中括号
print(c)
I am Chris and I am 33 I am Chris and I am 33 I am Chris and I am 33
telma = "Iloveyou520" a = telma.isalnum() b = telma.isalpha() amlet = "②" c = amlet.isdecimal() d = amlet.isdigit() e = amlet.isidentifier() #字母数字下划线 print(a) print(b) print(c) print(d) print(e)
True False False True False
lem = "三" print(lem.isdecimal()) print(lem.isdigit()) print(lem.isnumeric())
False False True
telma = "adr\nfd"
amlet = " "
lem = ""
print(telma.isprintable())
print(amlet.isspace())
print(lem.isspace())
title = "I Have Not Been Here Before"
print(telma.istitle())
tit = "I have not been here before"
print(tit.istitle())
print(tit.title())
t = "风"
print(t.join(tit))
x = " "
print(x.join("有多少爱可以重来"))
False True False False False I Have Not Been Here Before I风 风h风a风v风e风 风n风o风t风 风b风e风e风n风 风h风e风r风e风 风b风e风f风o风r风e 有 多 少 爱 可 以 重 来
浙公网安备 33010602011771号