#字符串的操作及其部分格式化 i=3 str="FGdV" while i>0: i=i-1 user_input=input("please enter security code:") if str.upper()==user_input.upper(): print("Input success") break else: print("please enter again:") print("You have %d more chances" %(i)) print("Game over") #这个方法是让验证码与用户输入都变成大写来匹配,如果验证时就是 #大写的匹配大写的,小写的匹配小写的又该怎么做呢,字母的大小写的操作对数字是没影响的哦 #字符串的操作 #capitalize 使字符串的首字母大写 a="hello LInda" b=a.capitalize()#Hello linda 会把原来字符串中的大写的变为小写 print(b) b1=a.upper() print(b1)#HELLO LINDA upper 把字符串全部变为大写 b2=a.lower()#hello linda 把字符串全变为小写 print(b2) #字符串的用途:验证码 不区分大小写,你怎样判断验证码正确 i=3 while i>0: i=i-1 str="hvhGH" user_input=input("please enter security code:") if str.upper()==user_input.upper(): print("login successfully") break else: print("please enter again") print("you only have %d chance" %(i)) print("Your chance is up") #把原来的验证码与用户输入的验证码都转换成一个标准,让用户最多可输入三次 b3=a.swapcase()#把字符串中大写的变为小写,小写的边为大写 print(b3)#HELLO liNDA swapcase ''' ''' b="you are the timeless memony in my heart" b1=b.title()#把字符串的首字母变为大写 print(b1)#You Are The Timeless Memony In My Heart title c="you are the6timeless memony in my56heart" b2=c.title() print(b2) c1="you are the6timeless me#mony in my56he$art" b3=c1.title()# 当然title也会把用特殊字符或数字隔开的单词首字母大写 print(b3)#You Are The6Timeless Me#Mony In My56He$Art b="you are the timeless memony in my heart" b1=b.center(80)#center(可填数字)用于使str居中,此例就是使str位于80个位的中间 print(b1)# you are the timeless memony in my heart a="helloworld hello" a1=a.startswith("he")#用于判断字符串是否以括号里的字符开始 print(a1)#true 结果 a2=a.startswith("l",3,6)#判断字符l是否在位置为2且到5的字符串中 print(a2) ''' ''' a="helloworld hello" a3=a.find("w") a4=a.find("k") print(a3)#5 print(a4)#-1 #find 找到字符,返回该字符的索引,找不到则返回-1 a5=a.find("wo") a6=a.find("gh") print(a5)#5 print(a6)#-1 #如果是寻找多个字符,则返回第一个字符所在的索引,若没有查找到则返回-1 a7=a.index("l") #a8=a.index("m") print(a7)#返回字符的索引,查找不到就报错 #print(a8) name=input("please enter your name:").strip() if name=="Linda": print("Hello Linda") else: print("name is error") #加了strip就算再输入姓名时,误输入了空格也不会出错,strip默认删除前后空格 #lstrip()删除左边的空格,rstrip()删除右边的空格 d="IloveChina I love china LOVE" d1=d.count("o")#数出字符串中的字母o有多少个 print(d1)#2 d2=d.split("o")#按照你所写的拆分规则,把字符串拆分为列表 d3=d.split(" ")#被分割的元素就不存在了 print(d2)#['Il', 'veChina I l', 've china LOVE'] print(d3)#['IloveChina', 'I', 'love', 'china', 'LOVE'] ''' ''' s="I am {},age is {},I am {}".format("Linda",18,"Linda") print(s) s1="I am {0},age is {1},I am {0}".format("Linda",18) print(s1) s2="I am {name},age is {age},I am {name}".format(name="Linda",age=18) print(s2) s3=s.replace("Linda","Chichy")#把Linda替换成Chichy s4=s.replace("Linda","Chichy",1)#有数字1就只替换一次 print(s3) print(s4) I am Linda,age is 18,I am Linda I am Linda,age is 18,I am Linda I am Linda,age is 18,I am Linda I am Chichy,age is 18,I am Chichy I am Chichy,age is 18,I am Linda f="Hello Linda,Welcome to China" if "Linda" in f: print("Welcome to China") else: print("Linda not exist") #Welcome to China '''