Python全栈之路---数据类型—字符串

字符串:有序的字符的集合,用于存储和表示基本的文本信息,一对单、双、或三引号中间包含的内容称之为字符串

     1.特性:有序,不可变(开辟新地址存储字符串,python解释器会定期清空不用了的已存储的)

>>> a = 'alex'
>>> id(a)
1806635111064
>>> a = 'Jack'
>>> a
'Jack'
>>> id(a)
1806635111232

     2.操作  常用:isdigit  replace  find  count  strip  center  split  format  join

(1)得出数据的操作:

 1 >>> s = 'Hello World!'
 2 >>> s.swapcase() #大小写反过来
 3 'hELLO wORLD!'
 4 >>> s = 'Hello World!'
 5 >>> s.capitalize() #第一个字母大写,其余全部小写
 6 'Hello world!'
 7 >>> s.casefold() #变成统一的小写字母
 8 'hello world!'
 9 >>> s.center(50,'-') #把s放到中间,两边用'-'填充,总长度为50
10 '-------------------Hello World!-------------------'
11 >>> s.count('o') #统计字符'o'的数量
12 2
13 >>> s.count('o',0,5) #统计从0到5字符'o'的数量,顾头不顾尾
14 1
15 >>> s2 = 'a\tb' #\b是Tab键
16 >>> s2
17 'a\tb'
18 >>> print(s2)
19 a b
20 >>> s2.expandtabs()
21 'a b'
22 >>> s2.expandtabs(20) #将Tab键的长度扩展到20
23 'a                    b'
24 >>> s.find('o') #查找字符o 找到返回索引值,找不到返回负数
25 4
26 >>> s.find('op')
27 -1
28 >>> s.find('o',0,3) #从0到3查找字符o,找到返回从左边找到的第一个索引值,找不到返回-1,rfind -> 从右边开始数
29 -1
30 >>> s.find('o',0,5) #顾头不顾尾
31 4
32 >>> s3 = 'my name is {0}, i am {1} years old.'
33 >>> s3
34 'my name is {0}, i am {1} years old.'
35 >>> s3.format('Alex','22') #格式化,用括号里的依次代替{0}{1},有点像列表
36 'my name is Alex, i am 22 years old.'
37 >>> s3 = 'my name is {0}, his name is {0}.'
38 >>> s3.format('Alex') #括号里的第一个就是{0},两个{0}一样
39 'my name is Alex, his name is Alex.'
40 >>> s3.format('Alex','Jane')
41 'my name is Alex, his name is Alex.'
42 >>> s3 = 'my name is {name}, i am {age} years old.'
43 >>> s3.format(name = 'Alex',age = '22') #根据定义命名来赋值,直接写是不行的
44 'my name is Alex, i am 22 years old.'
45 >>> s.index('o') #返回第一个o的索引值,rindex -> 从右边开始数,其余一致
46 4
47 >>> s.index('o',5,6) #从5到6返回从左边数第一个o的索引值,没有就报错
48 Traceback (most recent call last):
49     File "<stdin>", line 1, in <module>
50     ValueError: substring not found
51 >>> names = ['alex','jack','rain']
52 >>> '-'.join(names) #以 - 来将names里的元素拼接起来,并且进行区分
53 'alex-jack-rain'
54 >>> ''.join(names)
55 'alexjackrain'
56 >>> ','.join(names)
57 'alex,jack,rain'
58 >>> s = 'Hello World'
59 >>> s.ljust(50) #把字符串的长度变成50,长度不够,就在字符串窜后面用空格来填
60 'Hello World '
61 >>> s.ljust(50,'-') #把字符串的长度变成50,长度不够,就在字符串后面用 - 来填,rjust与ljust相反,rjust在左边填 - 
62 'Hello World---------------------------------------'
63 >>> s.zfill(40) #把s长度变成40,不够的在前面用0y填,一般是底层二进制用到
64 '00000000000000000000000000000hello world'
65 >>> s = '\n hello world '
66 >>> s
67 '\n hello world '
68 >>> s.strip() #去掉前后的空格   lstrip ->只去掉左边的空格   rstrip ->只去掉右边的空格
69 'hello world'
70 >>> str_in = 'abcdef'
71 >>> str_out = '!@#$%^' # str_in和str_out的字符数量必须一致,如此才能一一对应
72 >>> str.maketrans(str_in,str_out)  #maketrans生成密码对应表,表中为ASCII码
73 {97: 33, 98: 64, 99: 35, 100: 36, 101: 37, 102: 94}
74 >>> table = str.maketrans(str_in,str_out)
75 >>> table
76 {97: 33, 98: 64, 99: 35, 100: 36, 101: 37, 102: 94}
77 >>> s = 'hello world'
78 >>> s.translate(table) #将s按照table进行翻译
79 'h%llo worl$'
80 >>> s.partition('o') #将字符串以从左找到的第一个'o'分成两半,rpartition以从右边找到的第一个'o'分成两半
81 ('hell', 'o', ' world')
82 >>> s.replace('o','-') #把'o'全部换成'-'
83 'hell- w-rld'
84 >>> s.replace('o','-',1) #把'o'换成'-',只换一次
85 'hell- world'
86 >>> s.split() #以空格将s分开,放入列表
87 ['hello', 'world']
88 >>> s.split('o') #以'o'将s分开,放入列表,'o'消失
89 ['hell', ' w', 'rld']
90 >>> s.split('o',1) #以左边第一个'o'将s分开,放入列表,'o'消失,rsplit -> 从右边开始数 , splitlines -> 以行,即'\n'来分
91 ['hell', ' world']

(2)判断 True or False 的操作

 1 >>> s.endswith('!') #s是否以!结尾,startswith -> 以...开始
 2 True
 3 >>> s.endswith('!sdf') #s是否以!sdf结尾
 4 False
 5 >>> '22'.isalnum() #判断是否是数字和字符,不包括特殊符号
 6 True
 7 >>> '22s'.isalnum()
 8 True
 9 >>> '22s!'.isalnum()
10 False
11 >>> '33'.isdecimal() #判断是否是整数,用法和  isdigit  、  isnumeric
12 True
13 >>> '33s'.isdecimal()
14 False
15 >>> '33.3'.isdecimal()
16 False
17 >>> '33'.isidentifier() #是否是合法的变量名,合法变量名条件:以下划线、字母、数字组成,开头不能是数字,不能纯用数字组合
18 False
19 >>> '3asd'.islower() #字母是否都是小写字母
20 True
21 >>> '2asd56ER'.islower()
22 False
23 >>> '33'.isprintable() #能否被打印,只有文本文件,字节格式的能被打印,纯二进制文件之类的不能被打印(Linux中涉及)
24 True
25 >>> ' '.isspace() #是否是空格
26 True
27 >>> '3de '.isspace()
28 False
29 >>> 'Hello World'.istitle() #单词的第一个字符是否都是大写,就像新闻标题一样
30 True
31 >>> '45ASD'.isupper() #字母是否都是大写
32 True
33 >>> '45sA'.isupper()
34 False

猜你喜欢

转载自www.cnblogs.com/Moxiaoyu666/p/10258156.html