DAY02 - 数据类型: 字符串

字符串

定义:在单引号\双引号\三引号内,由一串字符组成

name='PatHoo' #本质为 name=str('PatHoo')

常用方法:

索引

1 name='PatHoo'
2 print(name[0])
>>> P
3 print(name[1000])
>>> #IndexError: string index out of range

移除(默认移除空格)

1 name=input('username: ').strip()
2 print(name)
3 
4 name='***PatHoo********'
5 print(name.strip('*'))
>>> PatHoo
6 print(name.lstrip('*'))
>>>
PatHoo********
7 print(name.rstrip('*'))
>>> ***PatHoo

切分(默认按空格切分)

1 user_info='root:x:0:0::/root:/bin/bash'
2 print(user_info.split(':')[5])
>>> /root
3 cmd_info='get|a.txt|333333333'
4 print(cmd_info.split('|')[0])
>>> get
5 print(cmd_info.split('|',1)) #切分一次 >>> ['get', 'a.txt|333333333']
6 msg='name PatHoo age 18'
7 print(msg.split())
>>> ['name', 'PatHoo', 'age', '18']

取长度

1 name='PatHoo'
2 print(name.__len__())
>>> 6
3 print(len(name)) #name.__len__()
>>> 6

切出子字符串

1 name='hello world'
2 print(name[1])  #下标从0开始
>>> e
3 print(name[1:7]) #不包含末尾的下标
>>> ello w
4 print(name[1:7:3]) #每隔2个字符
>>> eo

 

字符的其他方法(掌握)

1 name='Patrick Hoo'
2 print(name.endswith('oo'))
>>> True
3 print(name.startswith('P'))
>>> True
1 name='PatHoo say: i have a dream, my name is PatHoo.'
2 print(name.replace('PatHoo','VIMMER',1))
>>> VIMMER say: i have a dream, my name is PatHoo.
1 print('{} {} {}'.format('PatHoo',18,'male'))
>>> PatHoo 18 male
2 print('{0} {1} {0}'.format('PatHoo',18,'male'))
>>> PatHoo 18 PatHoo
3 print('NAME:{name} AGE:{age} SEX:{sex}'.format(age=18,sex='male',name='PatHoo'))
>>> NAME:PatHoo AGE:18 SEX:male
1 num='123'
2 print(num.isdigit())
>>> True
1 while True:
2     age=input('>>: ').strip()
3     if len(age) == 0:continue
4     if age.isdigit():
5         age=int(age)
6         print(age,type(age))

 

字符其他需要了解的方法

1 name='PatHoo hello'
2 print(name.find('o')) #找到则显示第一个字符的下标
>>> 4
3 print(name.find('x')) #没找到显示-1, 不报错
>>> -1
4 print(name.find('l',8,11)) #按范围查找 >>> 9 5 print(name.index('o')) #找到则显示第一个字符的下标
>>> 4
6 print(name.index('x')) #没找到报错
>>> ValueError: substring not found
1 name='PatHoo hello'
2 print(name.count('o',3,6))
>>> 2
1 L=['PatHoo','say','hello','world'] #用join方法拼接, 列表内容必须都是字符串
2 print(':'.join(L))
>>> PatHoo:say:hello:world
1 name='PatHoo'
2 print(name.center(30,'*'))
>>> ************PatHoo************
3 print(name.ljust(30,'*'))
>>> PatHoo************************
4 print(name.rjust(30,'*'))
>>> ************************PatHoo
5 print(name.zfill(30))
>>> 000000000000000000000000PatHoo
1 name='PatHoo\thello'
2 print(name)
>>> PatHoo hello
3 print(name.expandtabs(1)) #TAB键用1个空格表示
>>> PatHoo hello
1 name='PatHoo'
2 print(name.lower())
>>> pathoo
3 name='PatHoo' 4 print(name.upper())
>>> PATHOO
1 name='PatHoo say'
2 print(name.capitalize()) #首字母大写
>>> PatHoo say
3 print(name.swapcase()) #大小写翻转
>>> pAThOO SAY
4 msg='PatHoo say hi' 5 print(msg.title()) #每个单词的首字母大写
>>> PatHoo Say Hi
6 name='PatHoo123' 7 print(name.isalnum()) #字符串由字母和数字组成
>>> True
8 name='PatHoo' 9 print(name.isalpha()) #字符串只由字母组成
>>> True
 1 num1=b'4' #Bytes
 2 num2=u'4' #unicode,python3中无需加u就是unicode
 3 num3='' #中文数字
 4 num4='' #罗马数字
 5 
 6 #bytes,unicode
 7 print(num1.isdigit())
>>> True
8 print(num2.isdigit())
>>> True
9 print(num3.isdigit())
>>> False
10 print(num4.isdigit())
>>> False
11 12 13 #isdecimal:unicode 14 print(num2.isdecimal())
>>> True
15 print(num3.isdecimal())
>>> False
16 print(num4.isdecimal())
>>> False
17 18 19 #isnumberic:unicode,汉字,罗马 20 print(num2.isnumeric())
>>> True
21 print(num3.isnumeric())
>>> True
22 print(num4.isnumeric())
>>> True

 

posted @ 2017-08-09 16:33  FlyInDance  阅读(186)  评论(0编辑  收藏  举报