字符串常用操作

 1 常用:分割、长度、索引、切片
 2 r = (1,2,3,4,5)#只读列表元组
 3 name = "liangml"
 4 strip
 5 username = input("user:")
 6 if username.strip() == "liangml":#strip可以将输入前后的空格都换掉
 7     print("welcome")
 8 
 9 split
10 names = "liangml","tom","jack"
11 name2 = names.split(",")#split是names拆分开来(得到一个列表)
12 print("|".join(name2))#join是将name2结果以|形式合并到一起
13 
14 print("" in name)#有没有空格
15 
16 capitalize
17 print(name.capitalize())#capitalize是将名字的首字母大写
18 
19 format
20 #format字符串两种赋值方式
21 msg = "Hello,{name},It's been a long {age} since last time sopke...."
22 #msg2 = msg.format(name="zhangsan",age=111)
23 #print(msg2)
24 msg2 = "lisi{0},dddd{1}"
25 print(msg2.format("liangml",22))
26 
27 center
28 print(name[2:4])
29 print(name.center(40,"-"))#center可以指定长度,将字符串两边的长度均匀的分开
30 
31 find
32 print(name.find("a"))#find查找赋值中有几个包含字符串
33 
34 isdigit
35 age = input("your age:")
36 if age.isdigit():#isdigit判断他是否为数字
37     age = int(age)
38 else:
39     print("invalid data type")
40 
41 isalnum
42 name = "liangml!dfs"
43 print(name.isalnum())#不容许复制里面有特殊字符
44 
45 endswith、startswith
46 print(name.endswith("dfsd"))
47 print(name.startswith("dfsfds"))
48 
49 upper、lower
50 print(name.upper().lower())#upper赋值全部大写,lower全部变小

 

posted @ 2016-10-12 17:08  Callum  阅读(146)  评论(0编辑  收藏  举报