s1 ="I love python"print(s1[7])print(s1[11])print(s1[15])
p
o
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-3-1edf35eec0a4> in <module>
2 print(s1[7])
3 print(s1[11])
----> 4 print(s1[15])
IndexError: string index out of range
字符串格式化:静态和动态两部分。Hello Bill Hello 李宁 Hello 张三。Hello param_name 模板。字符串格式化就是把一个或多个值替换另一个字符串的某个标记。%:格式化字符串。
# 定义一个模板,%s:字符串
formatStr ="Hello %s,Today is %s, Are there any activities today?"# 准备要替换标记的值
values =("John","Wednesday")# step3:替换标记print(formatStr % values)
Hello John,Today is Wednesday, Are there any activities today?
# %f %dfrom math import pi
formatStr ="PI是圆周率,PI的值是%.2f(保留小数点后%d位)"
values1 =(pi,2)print(formatStr % values1 )
template4 = Template("$dollar$$相当于多少$pounds")
data ={}
data['dollar']=100
data['pounds']='英镑'print(template4.substitute(data))
100$相当于多少英镑
使用format方法格式化字符串
标记(格式化参数):{…}
如何进行格式化:“template”.format(…)
# 按顺序来指定格式化参数值
s1 ="Today is {},the temperature is {} degrees."print(s1.format("saturday",30))
Today is saturday,the temperature is 30 degrees.
# 使用命名格式化参数
s2 ="Today is {week},the temperature is {degree} degrees."print(s2.format(week ="sunday",degree =21))print(s2.format(degree =21,week ="sunday"))
Today is sunday,the temperature is 21 degrees.
Today is sunday,the temperature is 21 degrees.
# 混合使用顺序格式化参数和命名格式化参数
s3 ="Today is {week}, {},the {} temperature is {degree} degrees"print(s3.format("abcd",1234,degree =43, week ="sunday"))#print(s3.format("abcd",degree = 43,1234, week = "sunday"))会抛出异常。
Today is sunday, abcd,the 1234 temperature is 43 degrees
# 使用序号格式化参数
s4 ="Today is {week},{1}, the {0} temperature is {degree} degrees."print(s4.format("abcd",1234,degree=44,week="sunday"))
Today is sunday,1234, the abcd temperature is 44 degrees.
s4 ="Today is {week},{1}, the {1} temperature is {degree} degrees."print(s4.format("abcd",1234,degree=44,week="sunday"))
Today is sunday,1234, the 1234 temperature is 44 degrees.
for lang inlist:if"kotlin"== lang.lower():print("找到kotlin了")break;
找到kotlin了
from string import capwords
s ="i not only like Python, but also like kotlin"print(capwords(s))
I Not Only Like Python, But Also Like Kotlin
字符串方法:replace和strip
s ="abcdef"print(s.replace("a","12345"))
12345bcdef
print(s.replace("xyz","aa"))
abcdef
# strip函数print(" geeko ri.com ".strip())
geeko ri.com
langList =["python","java","ruby","scala","perl"]
lang =" python "if lang in langList:print("找到了python")else:print("未找到python")if lang.strip()in langList:print("找到python")else:print("未找到python")
未找到python
找到python
s ="*** $$* Hello * World ***$$$ *"print(s.strip(" *$"))
Hello * World
字符串方法:translate和maketrans
translate:替换单个字符
s ="I not only like python,but also like kotlin."
table = s.maketrans("ak","*$")print(table)print(s.translate(table))
{97: 42, 107: 36}
I not only li$e python,but *lso li$e $otlin.
s =input("请输入一个字符串:")whileTrue:
subStr =input("请输入要统计的字符串:")if subStr ==":exit":break;
i =0
count =0while i <len(s):
index = s.find(subStr, i)if index >-1:
count +=1
i = index +len(subStr)else:break;print("'{}'在'{}'中出现了{}次".format(subStr,s,count))
请输入一个字符串:I love python.
请输入要统计的字符串:l
'l'在'I love python.'中出现了1次
请输入要统计的字符串:o
'o'在'I love python.'中出现了2次
请输入要统计的字符串:exit
'exit'在'I love python.'中出现了0次
请输入要统计的字符串::exit
s1 ="Ib love python."
subStr ="Ib"
index = s1.find(subStr,0)print(index)