Python学习记录6

it2023-10-20  104

字符串:有三种表现形式 1.单引号 2.双引号 3.三引号标识字符串 字符串不能被修改

r表示保留原样 例:s=r"C\test\a" # C\test\a

字符串的格式化输出 age=18 print(“小千的年龄为%d”%age) #输出小千的年龄为18 d为整型 f为浮点型 print("age is %d%.2f%(age,age)) #age is 18 18.00 #.2表示保留几位小数 #“输出格式化变量%格式%格式%(age,age,age)” %c或%s是字符串格式 ―左对齐 +输出正负号

例: a=3.1415926 print(’%―f’%a)左对齐的3.1415926 s=“ABCDEFHKL” print(s[0]) #A,取第一个数从0开始 print(s[0:2]) #AB,取0到2范围间的数 print(s[0:6:2]) #ACE,取0到6范围间的数,每隔两个取一个 print(s[-1]) #L,最后一个 print(s[::-1]) #LKHFEDCBA,将字符串反转

s=“hello” s1=“python” s2=s*3 #hello hello hello s3=s+s1 #hello Python *表示重复 +表示连接

字符串常用函数 upper #大写 lower #小写 print(s.upper()) 判断字符.都是以is开头

k=“hello123” print(len(s)) #8,返回字符串长度 print(chr(97))#a print(chr(20202))#仪 print(ord(“A”))#65 v=“ABC789” m=“12345” print(v.isdigit())#False print(m.isdigit())#Ture print(v+m) #ABC78912345

最新回复(0)