1.元组
#只读列表,元组,当希望生成后不被修改则用元组 r=(1,2,3,4,5)
2.字符串,python字符串操作非常丰富,编程时可先查询python本身是否已设计了相关函数
1 #移除空白 2 username='alex ' 3 if username.strip()=='alex': 4 print('welcome') 5 #如需移除其它字符,比如: ,就用strip(' ')的方式 6 7 #分割 8 names='alex,jack,rain' 9 print("names====",names) 10 name2=names.split(',') 11 print("name2====",name2) 12 #将列表用|连起来 13 name3="|".join(name2) 14 print("name3=",name3) 15 16 #格式化 17 msg="hello,{name},it's been a long {age} since last time spoke" 18 print("msg===",msg) 19 msg2=msg.format(name='Minghu',age=333) 20 print("msg2===",msg2) 21 22 msg3="haha {0},dddd {1}" 23 print("msg3==",msg3) 24 print("format msg3==",msg3.format('Alex',33)) 25 26 #切片 27 name='alex Li' 28 print(name[2:4]) 29 print(name.center(40,'-')) 30 31 32 #其它操作 33 name='alex li' 34 print("name==",name) 35 print ("if space in name",(' ' in name))#有没有空格 36 print(name.capitalize())#首字母大写 37 print(name.find('l'))#如脜在返回下标,不存在返回-1 38 39 age=input("your age:") 40 if age.isdigit(): 41 age=int(age) 42 else: 43 print("invalid data type") 44 45 46 print(name.isalnum()) 47 print(name.startswith('al')) 48 print(name.endswith('li')) 49 print(name.upper())