字符串的内置方法
一、while补充:
while嵌套:
while True: name=input('please input your name: ') password=input('please input your password: ') if name == 'egon' and password == '123': print('login successfull') while True: cmd=input('>>: ') if cmd == 'quit': break print('====>',cmd) break
上面代码表示:用户名和密码输入正确后,用户可以输入命令,当用户输入quit后,程序就退出,但这样做稍微有点麻烦,下面这段代码要简明一点;
tag=True while tag: name=input('please input your name: ') password=input('please input your password: ') if name == 'egon' and password == '123': print('login successfull') while tag: cmd=input('>>: ') if cmd == 'quit': tag=False else: print('====>',cmd)
while...else...
count=0 while count < 10: if count == 3: count+=1 continue print(count) count+=1 else: #最后执行 print('在最后执行,并且只有在while循环没有被break打断的情况下才执行')
二、格式化输出
__author__ = 'ctz' while True: name=input("name:") age=int(input("age:")) sex=input("sex:") height=input("height:") msg=''' --------------------%s info---------------------- name:%s age:%d sex:%s height:%s -------------------------------------------------- '''%(name,name,age,sex,height) print(msg)
%s既能接收字符串,也能接收数字,%d只能表示数字
三、input与raw_input
python2 的input 与raw_input python3只有input x=raw_input('>>: ') #python2的raw_input与python3的input的一样的 print(x,type(x)) x=input('>>: ') #python2的input,用户输入什么类型,就原封不动的存成什么类型 print(x,type(x))
四、数字的内置方法
#=====>part1:数字类型 #掌握:int,float #了解:Long(在python2中才有),complex # num=10 # num=int(10) # print(type(num),num) # salary=12.5 # salary=float(12.5) # print(type(salary),salary) # 90 #9*(10**1)+0*(10**0) #进制转换(了解部分) #二进制:0 1 # 1010101 # res=1*(2**6)+1*(2**4)+1*(2**2)+1*1 # print(res) # bin(11) #十进制的11转成二进制 #八进制:0-7 print(oct(11))#十进制的11转成八进制 #十六进制:0-9 a-f # print(hex(11)) # print(hex(16)) #复数 # x=1-2j # print(x.real) # print(x.imag)
五、字符串的内置方法
作用:名字,性别,国籍,地址等描述信息
定义:在单引号\双引号\三引号内,由一串字符组成
name='egon'
常用操作:
移除空白strip
切分split
长度len
索引
切片
其他操作(包括常用)
__author__ = 'ctz' name='*egon**' #移除空白 print(name.strip("*")) print(name.lstrip("*")) print(name.rstrip("*")) #以什么开头以什么结尾 print(name.startswith("*")) print(name.endswith("*")) #替换字符串 str="lol is a great ganme,i like paly lol" print(str.replace("lol","英雄联盟")) print(str.replace("lol","英雄联盟",1)) #format的用法 格式化字符串的函数str.format() #根据位置 res1="{}{}{}".format("aa","18","aa") res2="{1}{0}{1}".format("is","aa") #根据关键字 res3="{name}{age}{addr}".format(name="aa",age=21,addr="aa") print(res1) print(res2) print(res3) #find index count msg="ctz say hello" print(msg.find("t",0,3))#找到了返回索引,找不到就返回-1不会报错,find顾头不顾尾 print(msg.find("e",1,4)) print(msg.count("t",1,4))#返回找到的个数 print(msg.index("t",1,3))#返回找到的索引,找不到会报错 name='root:x:0:0::/root:/bin/bash' print(name.split("/"))#默认分隔符为空格来了; name='C:/a/b/c/d.txt' #只想拿到顶级目录 #从第一个“/”分割 print(name.split("/",1)) #从右边的第一个“/”分割 print(name.rsplit("/",1)) #join tag="" print(tag.join(["ctz","pwf","xpy"]))#可迭代对象必须都是字符串 #center,ljust,rjust,zfill name='egon' print(name.center(30,'-')) print(name.ljust(30,'*')) print(name.rjust(30,'*')) print(name.zfill(50)) #用0填充 name="egon\thello" print(name) print(name.expandtabs(18)) name="cTz" print(name.lower()) print(name.upper()) print(name.capitalize())#首字母大写 print(name.swapcase())#字母大小写翻转 msg="ctz is good" print(name.title())#每个单词的首字母大写
#在python3中 num0='4' num1=b'4' #bytes num2=u'4' #unicode,python3中无需加u就是unicode num3='四' #中文数字 num4='Ⅳ' #罗马数字 isdigt:str,bytes,unicode print(num0.isdigit()) print(num1.isdigit()) print(num2.isdigit()) print(num3.isdigit()) print(num4.isdigit()) isdecimal:str,unicode num0='4' num1=b'4' #bytes num2=u'4' #unicode,python3中无需加u就是unicode num3='四' #中文数字 num4='Ⅳ' #罗马数字 print(num0.isdecimal()) # print(num1.) print(num2.isdecimal()) print(num3.isdecimal()) print(num4.isdecimal()) isnumeric:str,unicode,中文,罗马 num0='4' num1=b'4' #bytes num2=u'4' #unicode,python3中无需加u就是unicode num3='四' #中文数字 num4='Ⅳ' #罗马数字 print(num0.isnumeric()) # print(num1) print(num2.isnumeric()) print(num3.isnumeric()) print(num4.isnumeric())
''' 总结: 最常用的是isdigit,可以判断bytes和unicode类型,这也是最常见的数字应用场景 如果要判断中文数字或罗马数字,则需要用到isnumeric ''' #is其他 print('===>') name='egon123' print(name.isalnum()) #字符串由字母和数字组成 print(name.isalpha()) #字符串只由字母组成 print(name.isidentifier()) print(name.islower()) print(name.isupper()) print(name.isspace()) print(name.istitle())
浙公网安备 33010602011771号