Python3 字符串

116 阅读6分钟

一 序

 本文继续Python入门学习,内容来自:www.runoob.com/python3/pyt…

字符串是 Python 中最常用的数据类型。我们可以使用引号( ' 或 " )来创建字符串。

var1 = 'Hello World!'

Python 访问字符串中的值

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

Python 访问子字符串,可以使用方括号 [] 来截取字符串。

在需要在字符中使用特殊字符时,python 用反斜杠 \ 转义字符。

Python字符串运算符

下表实例变量 a 值为字符串 "Hello",b 变量值为 "Python":

Python字符串运算符

下表实例变量 a 值为字符串 "Hello",b 变量值为 "Python":

操作符描述实例
+字符串连接a + b 输出结果: HelloPython
*重复输出字符串a*2 输出结果:HelloHello
[]通过索引获取字符串中字符a[1] 输出结果 e
[ : ]截取字符串中的一部分,遵循左闭右开原则,str[0:2] 是不包含第 3 个字符的。a[1:4] 输出结果 ell
in成员运算符 - 如果字符串中包含给定的字符返回 True'H' in a 输出结果 True
not in成员运算符 - 如果字符串中不包含给定的字符返回 True'M' not in a 输出结果 True
r/R原始字符串 - 原始字符串:所有的字符串都是直接按照字面的意思来使用,没有转义特殊或不能打印的字符。 原始字符串除在字符串的第一个引号前加上字母 r(可以大小写)以外,与普通字符串有着几乎完全相同的语法。```
print( r'\n' ) print( R'\n' )
| %      | 格式字符串                                                                                                  | 请看下一节内容。                              |

![](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/86eafc9603d54234979fe0f09e2f4cb3~tplv-k3u1fbpfcp-zoom-1.image)

## Python字符串格式化

Python 支持格式化字符串的输出 。尽管这样可能会用到非常复杂的表达式,但最基本的用法是将一个值插入到一个有字符串格式符 %s 的字符串中。

![](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/5569db1ba2f9498dbc941c9c1683cbfc~tplv-k3u1fbpfcp-zoom-1.image)

## Python三引号

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

![](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/1e49b53505754153b5921bb646f040d9~tplv-k3u1fbpfcp-zoom-1.image)

## f-string

f-string 是 python3.6 之后版本添加的,称之为字面量格式化字符串,是新的格式化字符串的语法。

![](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/eb950f8ea0f24f31ba7450bbd1558352~tplv-k3u1fbpfcp-zoom-1.image)

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

## Python 的字符串内建函数

Python 的字符串常用内建函数如下:

这里应该是重点的,比Java的String多了很多。

| 序号 | 方法及描述                                                                                                                                                                                                              |
| -- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| 1  | [capitalize()](https://www.runoob.com/python3/python3-string-capitalize.html) 将字符串的第一个字符转换为大写                                                                                                                      |
| 2  | [center(width, fillchar)](https://www.runoob.com/python3/python3-string-center.html) 返回一个指定的宽度 width 居中的字符串,fillchar 为填充的字符,默认为空格。                                                                                 |
| 3  | [count(str, beg= 0,end=len(string))](https://www.runoob.com/python3/python3-string-count.html) 返回 str 在 string 里面出现的次数,如果 beg 或者 end 指定则返回指定范围内 str 出现的次数                                                          |
| 4  | [bytes.decode(encoding="utf-8", errors="strict")](https://www.runoob.com/python3/python3-string-decode.html) Python3 中没有 decode 方法,但我们可以使用 bytes 对象的 decode() 方法来解码给定的 bytes 对象,这个 bytes 对象可以由 str.encode() 来编码返回。 |
| 5  | [encode(encoding='UTF-8',errors='strict')](https://www.runoob.com/python3/python3-string-encode.html) 以 encoding 指定的编码格式编码字符串,如果出错默认报一个ValueError 的异常,除非 errors 指定的是'ignore'或者'replace'                            |
| 6  | [endswith(suffix, beg=0, end=len(string))](https://www.runoob.com/python3/python3-string-endswith.html) 检查字符串是否以 obj 结束,如果beg 或者 end 指定则检查指定的范围内是否以 obj 结束,如果是,返回 True,否则返回 False.                                 |
| 7  | [expandtabs(tabsize=8)](https://www.runoob.com/python3/python3-string-expandtabs.html) 把字符串 string 中的 tab 符号转为空格,tab 符号默认的空格数是 8 。                                                                                 |
| 8  | [find(str, beg=0, end=len(string))](https://www.runoob.com/python3/python3-string-find.html) 检测 str 是否包含在字符串中,如果指定范围 beg 和 end ,则检查是否包含在指定范围内,如果包含返回开始的索引值,否则返回-1                                                  |
| 9  | [index(str, beg=0, end=len(string))](https://www.runoob.com/python3/python3-string-index.html) 跟find()方法一样,只不过如果str不在字符串中会报一个异常。                                                                                   |
| 10 | [isalnum()](https://www.runoob.com/python3/python3-string-isalnum.html) 如果字符串至少有一个字符并且所有字符都是字母或数字则返 回 True,否则返回 False                                                                                              |
| 11 | [isalpha()](https://www.runoob.com/python3/python3-string-isalpha.html) 如果字符串至少有一个字符并且所有字符都是字母或中文字则返回 True, 否则返回 False                                                                                             |
| 12 | [isdigit()](https://www.runoob.com/python3/python3-string-isdigit.html) 如果字符串只包含数字则返回 True 否则返回 False..                                                                                                            |
| 13 | [islower()](https://www.runoob.com/python3/python3-string-islower.html) 如果字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小写,则返回 True,否则返回 False                                                                              |
| 14 | [isnumeric()](https://www.runoob.com/python3/python3-string-isnumeric.html) 如果字符串中只包含数字字符,则返回 True,否则返回 False                                                                                                      |
| 15 | [isspace()](https://www.runoob.com/python3/python3-string-isspace.html) 如果字符串中只包含空白,则返回 True,否则返回 False.                                                                                                           |
| 16 | [istitle()](https://www.runoob.com/python3/python3-string-istitle.html) 如果字符串是标题化的(见 title())则返回 True,否则返回 False                                                                                                   |
| 17 | [isupper()](https://www.runoob.com/python3/python3-string-isupper.html) 如果字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是大写,则返回 True,否则返回 False                                                                              |
| 18 | [join(seq)](https://www.runoob.com/python3/python3-string-join.html) 以指定字符串作为分隔符,将 seq 中所有的元素(的字符串表示)合并为一个新的字符串                                                                                                    |
| 19 | [len(string)](https://www.runoob.com/python3/python3-string-len.html) 返回字符串长度                                                                                                                                      |
| 20 | [ljust(width\[, fillchar\])](https://www.runoob.com/python3/python3-string-ljust.html) 返回一个原字符串左对齐,并使用 fillchar 填充至长度 width 的新字符串,fillchar 默认为空格。                                                                  |
| 21 | [lower()](https://www.runoob.com/python3/python3-string-lower.html) 转换字符串中所有大写字符为小写.                                                                                                                               |
| 22 | [lstrip()](https://www.runoob.com/python3/python3-string-lstrip.html) 截掉字符串左边的空格或指定字符。                                                                                                                             |
| 23 | [maketrans()](https://www.runoob.com/python3/python3-string-maketrans.html) 创建字符映射的转换表,对于接受两个参数的最简单的调用方式,第一个参数是字符串,表示需要转换的字符,第二个参数也是字符串表示转换的目标。                                                                    |
| 24 | [max(str)](https://www.runoob.com/python3/python3-string-max.html) 返回字符串 str 中最大的字母。                                                                                                                               |
| 25 | [min(str)](https://www.runoob.com/python3/python3-string-min.html) 返回字符串 str 中最小的字母。                                                                                                                               |
| 26 | [replace(old, new \[, max\])](https://www.runoob.com/python3/python3-string-replace.html) 把 将字符串中的 old 替换成 new,如果 max 指定,则替换不超过 max 次。                                                                             |
| 27 | [rfind(str, beg=0,end=len(string))](https://www.runoob.com/python3/python3-string-rfind.html) 类似于 find()函数,不过是从右边开始查找.                                                                                             |
| 28 | [rindex( str, beg=0, end=len(string))](https://www.runoob.com/python3/python3-string-rindex.html) 类似于 index(),不过是从右边开始.                                                                                            |
| 29 | [rjust(width,\[, fillchar\])](https://www.runoob.com/python3/python3-string-rjust.html) 返回一个原字符串右对齐,并使用fillchar(默认空格)填充至长度 width 的新字符串                                                                             |
| 30 | [rstrip()](https://www.runoob.com/python3/python3-string-rstrip.html) 删除字符串字符串末尾的空格.                                                                                                                               |
| 31 | [split(str="", num=string.count(str))](https://www.runoob.com/python3/python3-string-split.html) 以 str 为分隔符截取字符串,如果 num 有指定值,则仅截取 num+1 个子字符串                                                                      |
| 32 | [splitlines(\[keepends\])](https://www.runoob.com/python3/python3-string-splitlines.html) 按照行('\r', '\r\n', \n')分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符。                                   |
| 33 | [startswith(substr, beg=0,end=len(string))](https://www.runoob.com/python3/python3-string-startswith.html) 检查字符串是否是以指定子字符串 substr 开头,是则返回 True,否则返回 False。如果beg 和 end 指定值,则在指定范围内检查。                               |
| 34 | [strip(\[chars\])](https://www.runoob.com/python3/python3-string-strip.html) 在字符串上执行 lstrip()和 rstrip()                                                                                                            |
| 35 | [swapcase()](https://www.runoob.com/python3/python3-string-swapcase.html) 将字符串中大写转换为小写,小写转换为大写                                                                                                                     |
| 36 | [title()](https://www.runoob.com/python3/python3-string-title.html) 返回"标题化"的字符串,就是说所有单词都是以大写开始,其余字母均为小写(见 istitle())                                                                                               |
| 37 | [translate(table, deletechars="")](https://www.runoob.com/python3/python3-string-translate.html) 根据 str 给出的表(包含 256 个字符)转换 string 的字符, 要过滤掉的字符放到 deletechars 参数中                                                   |
| 38 | [upper()](https://www.runoob.com/python3/python3-string-upper.html) 转换字符串中的小写字母为大写                                                                                                                                 |
| 39 | [zfill (width)](https://www.runoob.com/python3/python3-string-zfill.html) 返回长度为 width 的字符串,原字符串右对齐,前面填充0                                                                                                           |
| 40 | [isdecimal()](https://www.runoob.com/python3/python3-string-isdecimal.html) 检查字符串是否只包含十进制字符,如果是返回 true,否则返回 false。                                                                                                 |