标签:bcd 复杂 数据 stdin last 一起 三引号 效率 个数
字符串:
1 >>> a = ‘ab‘ + ‘c‘+ ‘d‘ 2 >>> type(a) 3 <class ‘str‘> 4 >>> id(a) 5 12128640 6 >>> a = ‘ab‘ + ‘c‘+ ‘d‘+ ‘e‘ 7 >>> id(a) 8 12128672 9 # 内存分配的空间不一样 10 >>> a 11 ‘abcde‘ 12 >>> a[1] 13 ‘b‘ 14 >>> a[1] = l 15 Traceback (most recent call last): 16 File "<stdin>", line 1, in <module> 17 NameError: name ‘l‘ is not defined 18 # 和列表一样的用法,也是不可改变
字符串的定义和初始化
1 s1 = ‘string‘ 2 print(s1) 3 string 4 ############################## 5 s2 = "string2" 6 print(s2) 7 string2 8 ############################## 9 s3 = ‘‘‘this‘s a "String"‘‘‘ 10 print(s3) 11 this‘s a "String" 12 ############################## 13 s4 = ‘hello \n magedu.com‘ 14 print(s4) 15 hello 16 magedu.com 17 ############################## 18 s5 = r"hello \n magedu.com" 19 print(s5) 20 hello \n magedu.com 21 ############################## 22 s6 = ‘c:\windows\nt‘ 23 print(s6) 24 c:\windows 25 t 26 ############################## 27 s7 = R"c:\windows\nt" 28 print(s7) 29 c:\windows\nt 30 ############################## 31 s8 = ‘c:\windows\\nt‘ 32 print(s8) 33 c:\windows\nt 34 ############################## 35 sql = """select * from user where name=‘tom‘""" 36 print(sql) 37 select * from user where name=‘tom‘ 38 39 # 注意各种标点和转义符
字符串元素访问
1 sql = """select * from user where name=‘tom‘""" 2 print(sql[4]) 3 sql[4] = ‘o‘ 4 print(sql[4]) 5 ####################################### 6 c 7 # 这里返回的是字符串‘c‘,而不是字符c 8 sql[4] = ‘o‘ 9 TypeError: ‘str‘ object does not support item assignment 10 # 这里注意不可进行修改
1 sql = """select * from user where name=‘tom‘""" 2 for c in sql: 3 print(c,end=‘ ‘) 4 print() 5 print(type(c)) 6 ##################################### 7 s e l e c t * f r o m u s e r w h e r e n a m e = ‘ t o m ‘ 8 <class ‘str‘>
1 sql = """select * from user where name=‘tom‘""" 2 lst = list(sql) 3 print(lst) 4 t = tuple(sql) 5 print(t) 6 ####################################### 7 [‘s‘, ‘e‘, ‘l‘, ‘e‘, ‘c‘, ‘t‘, ‘ ‘, ‘*‘, ‘ ‘, ‘f‘, ‘r‘, ‘o‘, ‘m‘, ‘ ‘, ‘u‘, ‘s‘, ‘e‘, ‘r‘, ‘ ‘, ‘w‘, ‘h‘, ‘e‘, ‘r‘, ‘e‘, ‘ ‘, ‘n‘, ‘a‘, ‘m‘, ‘e‘, ‘=‘, "‘", ‘t‘, ‘o‘, ‘m‘, "‘"] 8 (‘s‘, ‘e‘, ‘l‘, ‘e‘, ‘c‘, ‘t‘, ‘ ‘, ‘*‘, ‘ ‘, ‘f‘, ‘r‘, ‘o‘, ‘m‘, ‘ ‘, ‘u‘, ‘s‘, ‘e‘, ‘r‘, ‘ ‘, ‘w‘, ‘h‘, ‘e‘, ‘r‘, ‘e‘, ‘ ‘, ‘n‘, ‘a‘, ‘m‘, ‘e‘, ‘=‘, "‘", ‘t‘, ‘o‘, ‘m‘, "‘")
字符串连接
1 a = ‘abcd‘ 2 b = ‘ ‘.join(a) 3 print(b) 4 a b c d 5 ################### 6 c = "@".join(a) 7 print(c) 8 a@b@c@d 9 ################### 10 lst = [‘1‘,‘2‘,‘3‘] 11 print(‘ ‘.join(lst)) 12 1 2 3 13 ################### 14 print("\n".join(lst)) 15 1 16 2 17 3 18 ################### 19 lst1 = [‘1‘,[‘a‘,‘b‘],‘3‘] 20 print(‘ ‘.join(lst1)) 21 Traceback (most recent call last): 22 File "D:/untitled/project2/day1/zifuchuan.py", line 2, in <module> 23 print(‘ ‘.join(lst1)) 24 TypeError: sequence item 1: expected str instance, list foun 25 # 这种嵌套类型的会报错
字符串分割
1 >>> s1 = "I‘m \ta super student." 2 3 >>> s1.split() 4 ["I‘m", ‘a‘, ‘super‘, ‘student.‘] 5 6 >>> s1.split(‘s‘) 7 ["I‘m \ta ", ‘uper ‘, ‘tudent.‘] 8 9 >>> s1.split(‘super‘) 10 ["I‘m \ta ", ‘ student.‘] 11 12 >>> s1.split(‘ ‘) 13 ["I‘m", ‘\ta‘, ‘super‘, ‘student.‘] 14 15 >>> s1.split(‘ ‘,2) 16 ["I‘m", ‘\ta‘, ‘super student.‘] 17 18 >>> s1.split(‘\t‘,2) 19 ["I‘m ", ‘a super student.‘]
1 >>> ‘ab c\n\nde fg\rk|\r\n‘.splitlines() 2 [‘ab c‘, ‘‘, ‘de fg‘, ‘k|‘] 3 # 行分隔符包括\n、\r\n、\r等等 4 5 >>> ‘ab c\n\nde fg\rk|\r\n‘.splitlines(True) 6 [‘ab c\n‘, ‘\n‘, ‘de fg\r‘, ‘k|\r\n‘] 7 #True保留行分隔符 8 9 >>> s1 = ‘‘‘I‘m a super student.You‘re a super teacher.‘‘‘ 10 >>> print(s1) 11 I‘m a super student.You‘re a super teacher. 12 13 >>> print(s1.splitlines()) 14 ["I‘m a super student.You‘re a super teacher."] 15 16 >>> print(s1.splitlines(True)) 17 ["I‘m a super student.You‘re a super teacher."]
1 >>> s1 = "I‘m a super student." 2 >>> s1.partition(‘s‘) 3 ("I‘m a ", ‘s‘, ‘uper student.‘) 4 5 >>> s1.partition(‘stu‘) 6 ("I‘m a super ", ‘stu‘, ‘dent.‘) 7 8 >>> s1.partition(‘‘) 9 Traceback (most recent call last): 10 File "<stdin>", line 1, in <module> 11 ValueError: empty separator 12 # 分隔符不能为空 13 14 >>> s1.partition(‘ ‘) 15 ("I‘m", ‘ ‘, ‘a super student.‘) 16 17 >>> s1.partition(‘abc‘) 18 ("I‘m a super student.", ‘‘, ‘‘)
字符串的大小写
1 >>> s1 = "I‘m a super student." 2 >>> s1.upper() 3 "I‘M A SUPER STUDENT." 4 5 >>> s1.lower() 6 "i‘m a super student." 7 8 >>> s1.swapcase() 9 "i‘M A SUPER STUDENT."
字符串排版
1 >>> s = "I‘m a super STUDENT." 2 >>> s.title() 3 "I‘M A Super Student." 4 5 >>> s.capitalize() 6 "I‘m a super student." 7 8 >>> s.center(20) 9 "I‘m a super STUDENT." 10 11 >>> s.center(50) 12 " I‘m a super STUDENT. " 13 14 >>> s.center(50,‘#‘) 15 "###############I‘m a super STUDENT.###############" 16 17 >>> s.zfill(50) 18 "000000000000000000000000000000I‘m a super STUDENT." 19 20 >>> s.ljust(50,‘#‘) 21 "I‘m a super STUDENT.##############################" 22 23 >>> s.rjust(50,‘#‘) 24 "##############################I‘m a super STUDENT."
字符串修改
1 >>> ‘www..magedu.com‘.replace(‘w‘,‘p‘) 2 ‘ppp..magedu.com‘ 3 4 >>> ‘www..magedu.com‘.replace(‘w‘,‘p‘,2) 5 ‘ppw..magedu.com‘ 6 7 >>> ‘www..magedu.com‘.replace(‘w‘,‘p‘,1) 8 ‘pww..magedu.com‘ 9 10 >>> ‘www..magedu.com‘.replace(‘ww‘,‘p‘,2) 11 ‘pw..magedu.com‘ 12 13 >>> ‘www..magedu.com‘.replace(‘www‘,‘python‘,2) 14 ‘python..magedu.com‘
1 >>> s = "\r \n \t Hello Python \n \t" 2 >>> s.strip() 3 ‘Hello Python‘ 4 # 默认去掉两端的空白字符 5 6 >>> s1 = " I am very very very sorry " 7 >>> s1.strip() 8 ‘I am very very very sorry‘ 9 10 >>> s1.strip(‘r‘) 11 ‘ I am very very very sorry ‘ 12 # 因为首尾两端都没有r,因此无修改 13 14 >>> s1.strip(‘r y‘) 15 ‘I am very very very so‘ 16 # 这里要注意,去除的是r、空格和y这三个字符,首端查找有空格去掉,尾端先去掉空格,再去掉字符y,再去掉两个字符r 17 18 >>> s1.strip(‘r yIamso‘) 19 ‘very very ve‘ 20 21 >>> s1.lstrip(‘r yIamso‘) 22 ‘very very very sorry ‘ 23 24 >>> s1.rstrip(‘r yIamso‘) 25 ‘ I am very very ve‘ 26 >>>
字符串查找
1 >>> s = "I am very very very sorry" 2 >>> s.find(‘very‘) 3 5 4 # 每一个字母每一个空格都算一个字符 5 # 且从0开始算起,I+空格+a+m+空格,正好第6个是very,因此返回5 6 >>> s.find(‘very‘,5) 7 5 8 # 表示是从第5个开始查 9 >>> s.find(‘very‘,6,13) 10 -1 11 # 表示是从第6个开始查,到第13个结束,查询不到返回-1的结果 12 >>> info = ‘abca‘ 13 >>> info.find(‘a‘) 14 0 15 # 从下标0开始,查找在字符串里第一个出现的子串,返回结果0 16 >>> info.find(‘a‘,1) 17 3 18 # 从下标1开始,查找在字符串里第一个出现的子串:返回结果3 19 >>> info.find(‘3‘) 20 -1 21 # 查找不到返回-1
1 >>> a = "I am very very very sorry" 2 >>> a.index(‘very‘) 3 5 4 >>> a.index(‘very‘,5) 5 5 6 >>> a.index(‘very‘,6,13) 7 Traceback (most recent call last): 8 File "<stdin>", line 1, in <module> 9 ValueError: substring not found 10 >>> a.rindex(‘very‘,10) 11 15 12 >>> a.rindex(‘very‘,10,15) 13 10 14 >>> a.rindex(‘very‘,-10,-1) 15 15 16 # 注意与find()比较
1 >>> a = "I am very very very sorry" 2 >>> a.rindex(‘very‘,10) 3 15 4 >>> a.rindex(‘very‘,10,15) 5 10 6 >>> a.rindex(‘very‘,-10,-1) 7 15 8 >>> a.count(‘very‘) 9 3 10 >>> a.count(‘very‘,5) 11 3 12 >>> a.count(‘very‘,10,14) 13 1 14 # 注意,count是统计个数的
字符串判断
1 >>> a = "I am very very very sorry" 2 >>> a.startswith(‘very‘) 3 False 4 >>> a.startswith(‘very‘,5) 5 True 6 >>> a.startswith(‘very‘,5,9) 7 True 8 >>> a.endswith(‘very‘,5,9) 9 True 10 >>> a.endswith(‘very‘,5) 11 False 12 >>> a.endswith(‘very‘,5,-1) 13 False 14 >>> a.endswith(‘very‘,5,100) 15 False 16 >>> a.endswith(‘sorry‘,5) 17 True 18 >>> a.endswith(‘sorry‘,5,-1) 19 False 20 >>> a.endswith(‘sorry‘,5,100) 21 True 22 # 注意左闭右开,5,9是从第5个字串开始到第8个字串,不含第9个
字符串格式化
标签:bcd 复杂 数据 stdin last 一起 三引号 效率 个数
原文地址:https://www.cnblogs.com/linfengs/p/11719773.html