字符串 列表

xiaoxiao2021-02-28  76

一.字符串的定义

第一种方式:

str = ‘str’

In [1]: str='hello world' In [2]: print str hello world

第二种方式:

srt=”str”

In [4]: str="hello python" In [5]: print str hello python

第三种方式:

str = “”“str”“”

In [6]: str="""hello westos""" In [7]: print str hello westos

二.转义符号

一个反斜线加一个单一字符可以表示一个特殊字符,通常是不可打印的字符

\n: 代表换行符 \": 代表双引号本身 \t: 代表tab符 \': 代表单引号本身 In [8]: s='\thello' In [9]: print s hello In [11]: s='\'hello' In [12]: print s 'hello

三.字符串的操作

字符串属于序列,序列支持以下操作

1.索引

str[索引号] 0–h;1–e;2–l;…10–d

In [15]: str='hello world' In [16]: str[0] Out[16]: 'h' In [17]: str[-1] Out[17]: 'd'

2.切片

str[0:5:2] 索引0-5步长为2,取0–h;2–l;4–o

In [18]: str[0:5:2] Out[18]: 'hlo'

3.判断子串

‘字串’ (not) in ‘母串’;返回值为布尔型

In [19]: str Out[19]: 'hello world' In [20]: 'world' in str Out[20]: True In [21]: 'world' not in str Out[21]: False

4.重复 *

In [21]: 'world' not in str Out[21]: False In [22]: str*2 Out[22]: 'hello worldhello world' In [23]: "*" * 4 Out[23]: '****'

5.连接 +

In [24]: str Out[24]: 'hello world' In [25]: str+"python" Out[25]: 'hello worldpython' In [26]: str+" python" Out[26]: 'hello world python' In [27]: "*"*5+"ATM"+"#"*5 Out[27]: '*****ATM#####'

6.计算长度 len

In [28]: str Out[28]: 'hello world' In [29]: len(str) Out[29]: 11

四.字符类型的转换

str(obj) 将其他类型内容转换为字符串 int(obj) 将其他类型转换为为整数 float(obj) 将其他类型转换为浮点型 long(obj) 将其他类型转换为长整型 complex(obj)将其他类型转换为复数型 In [49]: a=1 In [50]: type(a) Out[50]: int In [51]: long(a) Out[51]: 1L In [52]: complex(a) Out[52]: (1+0j) In [53]: str(a) Out[53]: '1'

五.字符串的操作参数

1:*.is****()返回值为布尔型

s='hello world' s.isalnum() ##判断字符串s是否为字母或者数字(注意是或者);为真 s.isalpha() ##判断字符串s是否为字母;为真 s.isdigit() ##判断字符串s是否为数字;为假 s.islower() ##判断字符串s是否为小写字母;为真 s.isupper() ##判断字符串s是否为大写字母;为假 s.istitle() ##判断字符串s是否为标题,字符串的每个子串的首字母大写;为假 s.isspace() ##判断字符串s是否为空格符;为假

判断标示符是否合法: 准则:标示符只能由字母或者下划线开头:标示符只能含字母数字或下划线

while 1: s = raw_input("请输入标示符:") if s[0].isalpha() or s[0] == "_": for i in s[1:]: if i.isalnum() or i == "_": continue else: print "%s 标示符不合法!ERROR:标示符后边的字符" % (s) break else: print "%s 标示符合法" % (s) else: print "%s 标示符不合法!ERROR:标示符第一个字符" % (s)

2:*.startswith()和*.endtswith(),返回值为布尔型

*.startswith()用于判断字符串开头的匹配字符 *.endtswith()用于判断字符串结尾的匹配字符

In [13]: s1='http://www.baidu.com' In [14]: s2='ftp://www.baidu.com' In [15]: s1.startswith("http") Out[15]: True In [16]: s1.startswith("ftp") Out[16]: False 查找/var/log/下以.log结尾的文件 In [23]: import os In [24]: [ i for i in os.listdir('/var/log/') if i.endswith('.log')] Out[24]: ['wpa_supplicant.log', 'boot.log', 'Xorg.0.log', 'yum.log']

3:清除字符串的空格

*.strip()用于自动清除字符串两边的空格 *.lstrip()用于自动清除字符串左边的空格 *.rstrip()用于自动清除字符串右边的空格 *.replace()用于替换字符串中的字符 清除字符串两边的空格

In [25]: s= " hello " In [26]: s Out[26]: ' hello ' In [27]: s.strip() Out[27]: 'hello'

清除字符串左边的空格/右边的空格

In [26]: s Out[26]: ' hello ' In [27]: s.strip() Out[27]: 'hello' In [28]: s Out[28]: ' hello ' In [29]: s.lstrip() Out[29]: 'hello ' In [30]: s.rstrip() Out[30]: ' hello'

用*.replace实现字符串所有空格的清除

In [32]: s= ' he llo ' In [33]: s Out[33]: ' he llo ' In [34]: s.replace(" ","") Out[34]: 'hello'

4:*..lower() 和*.upper()把字符串转换小写或大写

In [39]: b="hello" In [40]: b Out[40]: 'hello' In [41]: b.upper() Out[41]: 'HELLO' In [44]: c="HELLO" In [45]: c Out[45]: 'HELLO' In [46]: c.lower() Out[46]: 'hello'

5.字符串的位置

*center()使字符串居中 *ljust()使字符串居左 *rjust()使字符串居右

In [54]: c Out[54]: 'HELLO' In [55]: c.center(10) Out[55]: ' HELLO ' In [56]: c.center(20) Out[56]: ' HELLO ' In [57]: c.ljust(10) Out[57]: 'HELLO ' In [58]: c.rjust(10) Out[58]: ' HELLO'

添加其他参数

In [59]: c Out[59]: 'HELLO' In [60]: c.center(10,"*") Out[60]: '**HELLO***' In [61]: c.ljust(10,"#")+"%" Out[61]: 'HELLO#####%'

6:*split()分解字符串

In [65]: str="my age is 19" In [66]: str Out[66]: 'my age is 19' In [67]: str.split() Out[67]: ['my', 'age', 'is', '19']

倒序 *.split()[::-1]

In [68]: str.split()[::-1] Out[68]: ['19', 'is', 'age', 'my']

7:*join()用于连接多个字符串

In [77]: s Out[77]: 'hello xiao mi' In [78]: s.split() Out[78]: ['hello', 'xiao', 'mi'] In [79]: new=s.split() In [80]: new Out[80]: ['hello', 'xiao', 'mi'] In [81]: "*".join(new) Out[81]: 'hello*xiao*mi'

8:字符串的比较cmp

In [85]: s1='hello1' In [86]: s2='hello2' In [87]: cmp(s1,s2) Out[87]: -1 In [88]: cmp(s2,s1) Out[88]: 1

help (cmp)获得帮助信息

Help on built-in function cmp in module __builtin__: cmp(...) cmp(x, y) -> integer Return negative if x<y, zero if x==y, positive if x>y. 返回值为负前者小于后者,返回值为正前者大于后者,返回值为0前者等于后者。字符串的比较按ASCCI码值比较

cmp函数定义

def cmp(a,b) if a < b: return -1 elif a>b: return 1 else: return 0

9:枚举enumerate

for i,j in enumerate ("hello"): print i,j

执行结果

0 h 1 e 2 l 3 l 4 o

10:zip

In [96]: si='abc' In [97]: s2="123" In [98]: print zip(s1,s2) [('h', '1'), ('e', '2'), ('l', '3')]

11.*title()将会把每一个字符串的首字母转为大写,为其余转为小写字母

s= "To give LeTian the first see face at eating-desktop" print s.title() To Give Letian The First See Face At Eating-Desktop

六.列表

可以理解为字符串的集合 c语言的数组,python的列表其中元素可以为不同类型;对于列表li li= [1, 1.0, ‘hello’, 1L, (1+7j)] 可执行操作:

1:索引

print li[0] --1 li[-1]--(1+7j)

2:切片

print li[::-1] ---[(1+7j), 1L,'hello', 1.0, 1]

3:重复

print li * 2 [1, 1.0, ‘hello’, 1L, (1+7j), 1, 1.0, ‘hello’, 1L, (1+7j)]

4:连接

array = [1,2,3,4] print li + array [1, 1.0, ‘hello’, 1L, (1+7j), 1, 2, 3, 4]

5:成员符操作

print 1 in li --Ture print 1 not in li --False

6:对列表的操作参数

append列表末尾追加元素

给定列表allow_ip=[‘172.25.254.1’,’172.25.254.2’]

allow_ip.append(172.25.254.3)#追加ip到列表allow_ip末尾
insert制订列表序列插入元素

allow_ip=[‘172.25.254.1’,’172.25.254.2’]

allow_ip.insert(1,‘172.25.254.3’)#追加ip3到列表第一个元素后
extend合并两个列表
allow_ip=['172.25.254.1','172.25.254.2'] allow1_ip=['172.25.254.11','172.25.254.12'] allow_ip.extend(allow1_ip)#追加allow1_ip所有元素按序列到列表allow_ip后
列表元素修改给列表元素重新赋值
allow_ip=['172.25.254.1','172.25.254.2'] allow_ip[0] = '172.0.0.0' 修改后 allow_ip=['172.0.0.0','172.25.254.2']
count查看显示列表中指定元素出现的次数,该元素不存在输出为0
print allow_ip.count('172.25.254.1') 1
index显示指定元素的索引值;如果出现多个,显示最小的那个,如果该元素不存在报错ValueError
print allow_ip.index('172.25.254.1')
删除:pop是删除指定索引的值,如果列表为空或者索引不再范围内,则报错;如果不指定索引值默认删除列表中最后一个元素
allow_ip=['172.25.254.1','172.25.254.2'] allow_ip.pop(1) print allow_ip 删除后为: allow_ip=['172.25.254.2']
remove删除列表中最先出现的值,不存在则报错
allow_ip=['172.25.254.1','172.25.254.2'] allow_ip.remove('172.25.254.2') print allow_ip 删除后 allow_ip=['172.25.254.1']
反转列表
allow_ip=['172.25.254.1','172.25.254.2'] allow_ip.reverse() 反转后: allow_ip=['172.25.254.2''172.25.254.1']
排序相同元素统一等级,不同元素按不同元素的第一个字符从小到大排序,类似于shell中的sort -r
allow_ip=['172.25.254.21','172.25.254.1','172.25.254.43','172.25.254.9'] allow_ip.sort() print allow_ip 重排序为 ['172.25.254.1', '172.25.254.21', '172.25.254.43', '172.25.254.9']
转载请注明原文地址: https://www.6miu.com/read-2623140.html

最新回复(0)