python基础复习 - 字符串常用操作

字符串正序反序和切片

对于字符串

theString = 'a game of thrones'

 

基于切片的操作

 

原值

theString[:]
'a game of thrones'

theString[::1]
'a game of thrones'

 

反序

theString[::-1]
'senorht fo emag a'

 

一般切片

theString[:7]
'a game '

 

反序切片

theString[7::-1]
'o emag a'

 

设置步长

'123435'[::2] #'133'

 

#步长之前加符号则先反序再切片
'123435'[::-2] #'542'

 

 

基于内建函数的操作

 

首字母大写

theString.capitalize()
'A game of thrones'

 

字符串内的每个独立单词的首字母大写,单词识别以符号分隔为准

'asdDFF fjiII l(s'.title()
'Asddff Fjiii L(S'

 

 

全部转为大写

theString.upper()
'A GAME OF THRONES'

 

全部转为小写

'A GAME OF THRONES'.lower()
'a game of thrones'

 

大小写取反

theString.title().swapcase()
'a gAME oF tHRONES'

 

 

两边空格居中

theString.center(50)
'                a game of thrones                 '

 

两边填充居中

theString.center(50, '*')
'****************a game of thrones*****************'

 

扩充\t

 1 #原字符串长度
 2 print('a game\tof thrones') #a game  of thrones
 3 len('a game  of thrones') - len('of thrones')
 4 8
 5 #\t及之前的字符串,总长度为8
 6 
 7 #输入20
 8 'a game\tof thrones'.expandtabs(20)
 9 'a game              of thrones'
10 len('a game              of thrones')
11 30
12 #\t占两个长度,输入的数字为\t及之前的字符串的总长度,如果输入的数字大于等于这个总长度,则用空格补全差额;如果输入的数字小于这个总长度,则会出现bug
13 
14 #以下展示一下expandtabs会出现的bug
15 #输入8 == 总长度
16 'a game\tof thrones'.expandtabs(8)
17 'a game  of thrones'
18 len('a game  of thrones')
19 18
20 
21 #以下数字皆小于总长度
22 #输入7
23 'a game\tof thrones'.expandtabs(7)
24 'a game of thrones'
25 len('a game of thrones')
26 17
27 
28 输入6
29 'a game\tof thrones'.expandtabs(6)
30 'a game      of thrones'
31 len('a game      of thrones')
32 22
33 
34 输入5
35 'a game\tof thrones'.expandtabs(5)
36 'a game    of thrones'
37 len('a game    of thrones')
38 20
39 
40 输入3
41 'a game\tof thrones'.expandtabs(3)
42 'a game   of thrones'
43 len('a game   of thrones')
44 19

 

判断开头和结尾

theString.startswith('a ')    
True

theString.startswith('b')
False

theString.endswith('Thrones')
False

theString.endswith('thrones')
True

 

查询

#substring, start, end
theString.find('e',2,10)
5

theString.find('e',10)
15

 

索引

#基本功能与find一致,但是如果要查询的substring不存在的话,使用index会报错,而find会返回-1
theString.find('z',10)
-1

theString.index('z',10)
ValueError: substring not found

 

去除 strip

#strip默认为去掉字符串前后的空格
#strip的原理是按照输入的元素从前/后开始迭代查询删除对应元素,直到迭代到非对应元素为止
'*&*&*& *asdiju*#*# *#'.strip(' *&#')
'asdiju'
#删除右边的rstrip
'*&*&*& *asd#iju*#*# *#'.rstrip(' *&#')
'*&*&*& *asd#iju'

#删除左边的lstrip
'*&*&*& *asd#iju*#*# *#'.lstrip(' *&#')
'asd#iju*#*# *#'

 

分割 split

#split默认输入元素为空格
theString.split()
['a', 'game', 'of', 'thrones']

#也可以输入任何字符串格式的元素
theString.split('e')
['a gam', ' of thron', 's']

 

按行分割 splitlines

'ad\nasd'.splitlines()
['ad', 'asd']

 

元素计数count

theString.count('e')
2

 

填充 format

#format总共有三种用法
#按顺序填充
'My name is {}. I prefer {}s around age {}'.format('John', 'girl', 20)
'My name is John. I prefer girls around age 20'

#按标号填充
'My name is {2}. I prefer {0}s around age {1}'.format('John', 'girl', 20)
'My name is 20. I prefer Johns around age girl'

#按变量名填充
'My name is {name}. I prefer {boyORgirl}s around age {age}'.format(name='Tom', boyORgirl='girl',age=20)
'My name is Tom. I prefer girls around age 20'

 

填充字典 format_map

#导入字典的key必须与format的string的变量名相同。
John={'name':'John', 'boyORgirl':'girl','age':20}
'My name is {name}. I prefer {boyORgirl}s around age {age}'.format_map(John)
'My name is John. I prefer girls around age 20'

 

替代字串中的元素

#默认为替换全部符合条件的元素
theString.replace('e','*')
'a gam* of thron*s'

#加上次数,可以限制替换次数
theString.replace('e','*',1)
'a gam* of thrones'

 

is 判断

 1 dir(str)
 2 #有一下is开头的内建函数
 3 '''
 4  'isalnum',
 5  'isalpha',
 6  'isascii',
 7  'isdecimal',
 8  'isdigit',
 9  'isidentifier',
10  'islower',
11  'isnumeric',
12  'isprintable',
13  'isspace',
14  'istitle',
15  'isupper',
16 '''
17 
18 #isalnum 判断是否全为数字和字母
19 'ASDasd123'.islanum() #True
20 
21 #isalpha 判断是否全为字母
22 'asd123'.isalpha() #False
23 '123'.isalpha() #True
24 
25 #isdigit 判断是否全为数字
26 '123'.isdigit() #True

 

posted @ 2018-11-05 14:53  kaezah  阅读(139)  评论(0编辑  收藏  举报