Python-字符串(string)

字符串是Python中最常用的数据类型。可以用''""来创建字符串。

1.创建字符串

1
2
string1 = 'Hello world.'
string2 = "I'am bad."

2.访问字符串中的值

可以通过索引或是切片的方式来访问字符串。

Python不支持单字符类型,单字符在Python中也是作为一个字符串使用。

1
2
3
4
5
6
7
8
string = "Son of the storm"

a = string[1]
b = string[2:3]
c = string[3:]
d = string[-1]

print(a,b,c,d,sep="\n")

输出如下:

1
2
3
4
o
n
of the storm
m

3.判断字符串中是否包含指定的字符

使用innot in

1
2
3
4
5
string = "Queen of the Andals, the Rhoynars and the First Men"
if "Queen" in string:#区分大小写
print("Queen is in the string.")
if "queen" not in string:
print("queen is not in the string.")

输出如下:

1
2
Queen is in the string.
queen is not in the string.

4.字符串拼接

使用+

1
2
3
string1 = "Queen/Lord of the Seven Kingdoms"
string2 = "Protector of the Realm"
print(string1+" , "+string2)

输出如下:

1
Queen/Lord of the Seven Kingdoms , Protector of the Realm

5.重复字符串。

使用*

1
2
string = "Khaleesi of the Great Grass Sea."
print(string*2)

输出如下:

1
Khaleesi of the Great Grass Sea.Khaleesi of the Great Grass Sea.

6.原始字符串

使用r或是R

保持字符串原意输出,不转义。

1
2
3
4
5
string1 = "I'm OK.\nThank you.\n..."
string2 = r"I'm OK.\nThank you.\n..."
string3 = R"I'm OK.\nThank you.\n..."

print(string1,string2,string3,sep="\n")

输出如下:

1
2
3
4
5
I'm OK.
Thank you.
...
I'm OK.\nThank you.\n...
I'm OK.\nThank you.\n...

7.转义字符

需要在字符串中添加特殊字符时,需要添加/转义字符。

转义字符 描述
\ 在行尾时,续行符。
\\ 反斜杠符号
\' 单引号
\" 双引号
\a 响铃符
\b 退格
\000 空格
\n 换行
\v 纵向制表符
\t 横向制表符
\r 回车
\f 换页
\oyy 八进制数,yy 代表的字符,例如:\o12 代表换行,其中 o 是字母,不是数字 0。
\xyy 十六进制数,yy代表的字符,例如:\x0a代表换行
\other 其它的字符以普通格式输出

8.三引号(多行字符串)

1)使用\

1
2
3
4
5
string = "Queen of the Andals, the Rhoynars and the First Men.\n" \
"Queen/Lord of the Seven Kingdoms.\n" \
"Protector of the Realm."

print(string)

输出如下:

1
2
3
Queen of the Andals, the Rhoynars and the First Men.
Queen/Lord of the Seven Kingdoms.
Protector of the Realm.

2)使用''''''或是""""""

python三引号允许一个字符串跨多行,字符串中可以包含换行符、制表符以及其他特殊字符。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
string = """
Queen of the Andals, the Rhoynars and the First Men.\nQueen/Lord of the Seven Kingdoms.
\tProtector of the Realm.
Khaleesi of the Great Grass Sea.
Breaker of Shackles/Chains.
Queen of Meereen.
Princess of Dragonstone.
Unburnt.
Mother of Dragons.
Mhysa.
Mother.
Silver Queen.
Silver Lady.
Dragon Queen.
"""

print(string)

输出如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Queen of the Andals, the Rhoynars and the First Men.
Queen/Lord of the Seven Kingdoms.
Protector of the Realm.
Khaleesi of the Great Grass Sea.
Breaker of Shackles/Chains.
Queen of Meereen.
Princess of Dragonstone.
Unburnt.
Mother of Dragons.
Mhysa.
Mother.
Silver Queen.
Silver Lady.
Dragon Queen.

9.Unicode字符串

Python3中,所有的字符串都是Unicode的字符串。

10.格式转化

capitalize()函数:将字符串第一个字符转化为大写。

center(width, fillchar)函数: 返回一个指定的宽度 width 居中的字符串,fillchar 为填充的字符,默认为空格。

ljust(width,fillchar)函数:返回一个原字符串左对齐,并使用 fillchar 填充至长度 width 的新字符串,fillchar 默认为空格。

rjust(width,fillchar)函数: 返回一个原字符串右对齐,并使用fillchar(默认空格)填充至长度 width 的新字符串。

expandtabs(tabsize=8)函数: 把字符串 string 中的 tab 符号转为空格,tab 符号默认的空格数是 8 。

lower()函数:转换字符串中所有大写字符为小写。

upper()函数:转换字符串中的小写字母为大写。

lstrip()函数:截掉字符串左边的空格或指定字符。

rstrip()函数:删除字符串字符串末尾的空格。

strip([chars\])函数:在字符串上执行 lstrip()和 rstrip()。

swapcase()函数: 将字符串中大写转换为小写,小写转换为大写。

title()函数: 返回”标题化”的字符串,就是说所有单词都是以大写开始,其余字母均为小写。

zfill (width)函数:返回长度为 width 的字符串,原字符串右对齐,前面填充0。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
string = "discovery requires experimentation。"
string1 = string.capitalize()
string2 = string.center(50,'-')
string3 = string.ljust(50,'-')
string4 = string.rjust(50,'-')
string5 = "\thello\tworld".expandtabs()
string6 = "DON'T MIND.".lower()
string7 = "don't mind.".upper()
string8 = "----don't mind----".lstrip("-")
string9 = "----don't mind----".rstrip("-")
string10 = "----don't mind----".strip("-")
string11 = "Don't MIND".swapcase()
string12 = string.title()
string13 = string.zfill(50)

print(string1,string2,string3,string4,string5,string6,string7,string8,string9,string10,string11,string12,string13,sep="\n")

输出如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
Discovery requires experimentation。
-------discovery requires experimentation。--------
discovery requires experimentation。---------------
---------------discovery requires experimentation。
hello world
don't mind.
DON'T MIND.
don't mind----
----don't mind
don't mind
dON'T mind
Discovery Requires Experimentation。
000000000000000discovery requires experimentation。

11.格式判断

startswith(substr, beg=0,end=len(string))函数: 检查字符串是否是以指定子字符串 substr 开头,是则返回 True,否则返回 False。如果beg 和 end 指定值,则在指定范围内检查。

endswith(suffix, beg=0, end=len(string))函数: 检查字符串是否以 obj 结束,如果beg 或者 end 指定则检查指定的范围内是否以 obj 结束,如果是,返回 True,否则返回 False。

isalnum()函数: 如果字符串至少有一个字符并且所有字符都是字母或数字则返 回 True,否则返回 False。

isalpha()函数: 如果字符串至少有一个字符并且所有字符都是字母则返回 True, 否则返回 False。

isdigit()函数: 如果字符串只包含数字则返回 True 否则返回 False。

islower()函数: 如果字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小写,则返回 True,否则返回 False 。

isnumeric()函数: 如果字符串中只包含数字字符,则返回 True,否则返回 False。

isspace()函数: 如果字符串中只包含空白,则返回 True,否则返回 False。

istitle()函数: 如果字符串是标题化的(每个单词首字母大写),则返回 True,否则返回 False。

isupper()函数: 如果字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是大写,则返回 True,否则返回 False。

isdecimal()函数:检查字符串是否只包含十进制字符,如果是返回 true,否则返回 false。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
string = "Linux is not Unix"
res1 = string.startswith("Linux")
res2 = string.endswith("Unix")
res3 = "linux1111".isalnum()
res4 = "linux".isalpha()
res5 = "12345677".isdigit()
res6 = "hello world".islower()
res7 = "1234345".isnumeric()
res8 = " ".isspace()
res9 = "This Is My Blogs.".istitle()
res10 = "THIS IS MY BLOGS.".isupper()
res11 = "11223322".isdecimal()

print(res1,res2,res3,res4,res5,res6,res7,res8,res9,res10,res11,sep="\n")

输出如下:

1
2
3
4
5
6
7
8
9
10
11
True
True
True
True
True
True
True
True
True
True
True

12.查找

find(str, beg=0, end=len(string))函数: 检测 str 是否包含在字符串中,如果指定范围 beg 和 end ,则检查是否包含在指定范围内,如果包含返回开始的索引值,否则返回-1 。

index(str, beg=0, end=len(string))函数: 跟find()方法一样,但str不在字符串中会报一个异常.

rfind(str, beg=0,end=len(string))函数:类似于 find()函数,不过是从右边开始查找。

rindex( str, beg=0, end=len(string))函数:类似于 index(),不过是从右边开始。

1
2
3
4
5
6
7
8
string = "Miracles happen every day.Miracles happen every day."
res1 = string.find("happen")
res2 = string.find("hello")
res3 = string.index("every")
res4 = string.rfind("happen")
res5 = string.rindex("every")

print(res1,res2,res3,res4,res5,sep="\n")

输出如下:

1
2
3
4
5
9
-1
16
35
42

13.统计

count(str, beg= 0,end=len(string))函数: 返回 str 在 string 里面出现的次数,如果 beg 或者 end 指定则返回指定范围内 str 出现的次数。

len(string)函数:返回字符串长度。

max(str)函数:返回字符串 str 中最大的字母。

min(str)函数:返回字符串 str 中最小的字母。

1
2
3
4
5
6
7
string = "life was like a box a chocolates, never know what you're gonna get."
num = string.count("l")
cnt = len(string)
charMax = max("ABDKHFckdhjfh")
charMin = min("ABDKHFckdhjfh")

print(num,cnt,charMax,charMin,sep="\n")

输出如下:

1
2
3
4
3
67
k
A

14.编码、解码

encode(encoding='UTF-8',errors='strict'): 以 encoding 指定的编码格式编码字符串。

bytes.decode(encoding="utf-8", errors="strict"): Python3 中没有 decode 方法,但可以使用 bytes 对象的 decode() 方法来解码给定的 bytes 对象 。

1
2
3
4
5
6
7
8
string = "我认识你。"

b_string1 = string.encode()
b_string2 = string.encode("GBK")
string1 = b_string1.decode()
string2 = b_string2.decode("GBK")

print(b_string1,b_string2,string1,string2,sep="\n")

输出如下:

1
2
3
4
b'\xe6\x88\x91\xe8\xae\xa4\xe8\xaf\x86\xe4\xbd\xa0\xe3\x80\x82'
b'\xce\xd2\xc8\xcf\xca\xb6\xc4\xe3\xa1\xa3'
我认识你。
我认识你。

15.序列转字符串

join(seq)函数:以指定字符串作为分隔符,将 seq 中所有的元素(的字符串表示)合并为一个新的字符串。

1
2
3
4
5
seq = ["I","like","you"]
string1 = ",".join(seq)
string2 = "|".join(seq)

print(string1,string2,sep="\n")

输出如下:

1
2
I,like,you
I|like|you

16.子串替换

replace(old, new [, max\])函数:把 将字符串中的 str1 替换成 str2,如果 max 指定,则替换不超过 max 次。

1
2
3
string = "she is right, you are not right."
string1 = string.replace("right","bad")
print(string1)

输出如下:

1
she is bad, you are not bad.

17.字符串分割

split(str="", num=string.count(str))函数:num=string.count(str)) 以 str 为分隔符截取字符串,如果 num 有指定值,则仅截取 num+1 个子字符串。

splitlines([keepends\])函数: 按照行(‘\r’, ‘\r\n’, \n’)分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符。

1
2
3
4
5
6
7
string = "I Love you so mush"
seq1 = string.split(" ")
string = "I Love you so mush\n\rYes me to!\nHahah"
seq2 = string.splitlines()
seq3 = string.splitlines(True)

print(seq1,seq2,seq3,sep="\n")

输出如下:

1
2
3
['I', 'Love', 'you', 'so', 'mush']
['I Love you so mush', '', 'Yes me to!', 'Hahah']
['I Love you so mush\n', '\r', 'Yes me to!\n', 'Hahah']

参考链接: https://www.runoob.com/python3/python3-string.html