字符串是 Python 中最常用的数据类型,是一个个字符组成的有序的序列,是字符的集合。
创建字符串很简单,可以使用引号(‘或"或""")来创建字符串,只要为变量分配一个值即可。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | s1 =‘string‘s2 ="string2"s3 =‘‘‘this‘s a "String"‘‘‘s4 =‘hello \n magedu.com‘print(s4)#hello# magedu.coms5 =r‘hello \n magedu.com‘print(s5)#hello \n magedu.com#字符串前面加r,表示的意思是禁止字符转义s6 =‘c:\windows\nt‘print(s6)#c:\windows#ts7 =R"c:\windows\nt"# 字符串前面加r,表示的意思是禁止字符转义print(s7)#c:\windows\nts8 =‘c:\windows\\nt‘print(s7)#c:\windows\nt# \ 转义字符,去除特殊字符的含义sql ="""select * from user where name=‘tom‘""" | 
| 1 2 3 4 5 6 7 | sql ="select * from user where name=‘tom‘"print(sql[4])# 字符串‘c‘sql[4] =‘o‘# TypeError: ‘str‘ object does not support item assignment# 错误的,因为字符串是不可变类型 | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | str1 ="python"forc instr1:    print(c)    print(type(c)) # 什么类型,当然还是字符串类型(str)"""p<class ‘str‘>y<class ‘str‘>t<class ‘str‘>h<class ‘str‘>o<class ‘str‘>n<class ‘str‘>""" | 
| 1 2 3 4 | str1 ="python"str1 =list(str1)print(str1)# [‘p‘, ‘y‘, ‘t‘, ‘h‘, ‘o‘, ‘n‘] | 
| 1 2 3 4 5 | str1 =‘www.baidu.com‘print(str1.replace(‘www‘, ‘sss‘))# ‘sss.baidu.com‘print(str1.replace(‘w‘, ‘s‘, 2))# ‘ssw.baidu.com‘  | 
| 1 2 3 4 5 6 | s ="\r \n\t hello python \n \t"print(s.strip())#"hello python"s ="i am very very very sorry"print(s.strip(‘iy‘))#" am very very very sorr" | 
| 1 2 3 | s ="i am very very very sorry"print(s.lstrip(‘iy‘))#" am very very very sorry" | 
| 1 2 3 | s ="i am very very very sorry"print(s.rstrip(‘iy‘))#"i am very very very sorr" | 
| 1 2 3 4 | s ="i am very very very sorry"print(s.find(‘very‘))  # 5print(s.find(‘very‘, 5))   # 从索引5开始查找  # 5print(s.find(‘very‘, 6, 13))  # 从索引6到13间查找 # -1 | 
| 1 2 3 4 5 | s ="i am very very very sorry"print(s.rfind(‘very‘, 10))print(s.rfind(‘very‘, 10, 15))print(s.rfind(‘very‘, -10, -1))  # 15print(s.rfind(‘very‘, -1, -10) ) # -1 | 
一般情况下查找字符串不适用index,返回异常不容易处理,会导致程序崩溃
| 1 2 3 4 5 | s ="i am very very very sorry"print(s.index(‘very‘)) # 5 索引从0开始print(s.index(‘very‘,5) )print(s.index(‘very‘,6,13)) | 
| 1 2 3 4 5 6 | s ="i am very very very sorry"print(s.rindex(‘very‘, 10))print(s.rindex(‘very‘, 10, 15))print(s.rindex(‘very‘, -10, -1))  # 15print(s.rindex(‘very‘, -1, -10))  # -1 | 
| 1 2 3 4 5 | s =‘I am very very very sorry‘print(s.count(‘very‘)) # 3print(s.count(‘very‘,5))  # 3print(s.count(‘very‘,5,14))  # 2 | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | s ="i am very very very sorry""""首先打印出每个字符的索引:方法1:num = 0for i in s:    print(num,"-",i,end="")    num +=1# 0 - i 1 -   2 - a 3 - m 4 -   5 - v 6 - e 7 - r 8 - y 9 -   10 - v 11 - e 12 - r 13 - y 14 -   15 - v 16 - e 17 - r 18 - y 19 -   20 - s 21 - o 22 - r 23 - r 24 - y 方法2:for i,j in enumerate(s):    print(i,"-",j,end=" ")# 0 - i 1 -   2 - a 3 - m 4 -   5 - v 6 - e 7 - r 8 - y 9 -   10 - v 11 - e 12 - r 13 - y 14 -   15 - v 16 - e 17 - r 18 - y 19 -   20 - s 21 - o 22 - r 23 - r 24 - y """print(s.startswith(‘very‘))# Falseprint(s.startswith(‘very‘, 5))# Trueprint(s.startswith(‘very‘, 5, 9))# Trueprint(s.endswith(‘very‘, 5, -1))# Falseprint(s.endswith(‘very‘, 5, 100))# True | 

| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | s1 ="i‘m \ta super student"print(s1.split())# ["i‘m", ‘a‘, ‘super‘, ‘student‘]# \t 在默认的情况下被当成分隔符(\t表示Tab键)print(s1.split(‘ ‘))# ["i‘m", ‘\ta‘, ‘super‘, ‘student‘]print(s1.split(‘ ‘, maxsplit=2))# ["i‘m", ‘\ta‘, ‘super student‘]print(s1.split(‘\t‘, maxsplit=2))# ["i‘m ", ‘a super student‘]   | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | s1 ="i‘m \ta super student"print(s1.rsplit())# ["i‘m", ‘a‘, ‘super‘, ‘student‘]# \t 在默认的情况下被当成分隔符print(s1.rsplit(‘super‘))# ["i‘m \ta ", ‘uper ‘, ‘tudent‘]print(s1.rsplit(‘ ‘))# ["i‘m", ‘\ta‘, ‘super‘, ‘student‘]print(s1.rsplit(‘ ‘, maxsplit=2)) #或 print(s1.rsplit(‘ ‘, 2))# ["i‘m", ‘\ta‘, ‘super student‘] maxsplit可以省却,直接写值print(s1.rsplit(‘\t‘, maxsplit=2))# ["i‘m ", ‘a super student‘] | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | str1 =‘ab c\n\nde fg\rkl\r\n‘print(str1.splitlines())#[‘ab c‘, ‘‘, ‘de fg‘, ‘kl‘]print(str1.splitlines(True))#[‘ab c\n‘, ‘\n‘, ‘de fg\r‘, ‘kl\r\n‘]s1 ="""i‘m a super student.you‘re a super teacher."""print(s1)#i‘m a super student.#you‘re a super teacher.print(s1.splitlines())#["i‘m a super student.", "you‘re a super teacher."]print(s1.splitlines(True))#["i‘m a super student.\n", "you‘re a super teacher."] | 
| 1 2 3 4 5 6 7 8 9 10 | s1 ="i‘m a super student"print(s1.partition(‘s‘))# ("i‘m a","s","uper student")print(s1.partition(‘ ‘))#("i‘m", ‘ ‘, ‘a super student‘)print(s1.partition(‘abc‘))# ("i‘m a super student",‘‘,‘‘) | 
| 1 2 3 4 | str1 ="I am supper student"print(str1.rpartition(" "))# (‘I am supper‘, ‘ ‘, ‘student‘) | 
| 1 2 3 | str1 ="i am a supper student"print(str1.upper())# I AM A SUPPER STUDENT | 
| 1 2 3 | str1 ="I AM A SUPPER STUDENT"print(str1.lower())# i am a supper student | 
| 1 2 3 4 5 6 7 8 9 10 11 | S1 ="Runoob EXAMPLE....WOW!!!"# 英文S2 ="ß"# 德语print(S1.lower())# runoob example....wow!!!print(S1.casefold())# runoob example....wow!!!print(S2.lower())# ßprint(S2.casefold())  # 德语的"ß"正确的小写是"ss"# ss | 
| 1 2 3 | str1 ="I Am A Supper Student"print(str1.swapcase())#i aM a sUPPER sTUDENT | 
| 1 2 3 4 5 6 | str1 ="i am supper student"print(str1.title())#I Am Supper Studentprint(str1.capitalize())#I am supper student | 
| 1 2 3 | s1 =‘abc‘print(s1.center(50,‘#‘))#‘#######################abc########################‘ | 
| 1 2 3 | s1 =‘abc‘print(s1.zfill(50))‘00000000000000000000000000000000000000000000000abc‘ | 
| 1 2 3 4 5 | s1 ="abc"print(s1.ljust(20))#"abc                 "print(s1.ljust(20,"*"))#abc***************** | 
| 1 2 3 4 5 | s1 =‘abc‘print(s1.rjust(20))#"                 abc"print(s1.rjust(50,"$"))#‘$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$abc‘ | 
原文:https://www.cnblogs.com/Jintaonet/p/11161105.html