Python3 字符串
Python字符串运算符
+ | 字符串连接 | a + b 输出结果: HelloPython |
* | 重复输出字符串 | a*2 输出结果:HelloHello |
[] | 通过索引获取字符串中字符 | a[1] 输出结果 e |
[ : ] | 截取字符串中的一部分,取到截止点的前一个 | a[1:4] 输出结果 ell |
in | 成员运算符 - 如果字符串中包含给定的字符返回 True | H in a 输出结果 1 |
not in | 成员运算符 - 如果字符串中不包含给定的字符返回 True | M not in a 输出结果 1 |
r/R | 原始字符串 - 原始字符串:所有的字符串都是直接按照字面的意思来使用,没有转义特殊或不能打印的字符。 原始字符串除在字符串的第一个引号前加上字母"r"(可以大小写)以外,与普通字符串有着几乎完全相同的语法。 | print r'\n' prints \n 和 print R'\n' prints \n |
% | 格式字符串 | 请看下一节内容。 |
[ : : ] | 以跳固定步数的方式,截取字符串中的一部分 | a[0:6:2] 输出结果:Hlo |
Python 的字符串内建函数
1 | capitalize() |
2 | center(width, fillchar) |
3 | count(str, beg= 0,end=len(string)) |
4 | bytes.decode(encoding="utf-8", errors="strict") |
5 | encode(encoding='UTF-8',errors='strict') |
6 | endswith(suffix, beg=0, end=len(string)) |
7 | expandtabs(tabsize=8) |
8 | find(str, beg=0 end=len(string)) |
9 | index(str, beg=0, end=len(string)) |
10 | isalnum() |
11 | isalpha() |
12 | isdigit() |
13 | islower() |
14 | isnumeric() |
15 | isspace() |
16 | istitle() |
17 | isupper() |
18 | join(seq) |
19 | len(string) |
20 | ljust(width[, fillchar]) |
21 | lower() |
22 | lstrip() |
23 | maketrans() |
24 | max(str) |
25 | min(str) |
26 | replace(old, new [, max]) |
27 | rfind(str, beg=0,end=len(string)) |
28 | rindex( str, beg=0, end=len(string)) |
29 | rjust(width,[, fillchar]) |
30 | rstrip() |
31 | split(str="", num=string.count(str)) |
32 | splitlines([keepends]) |
33 | startswith(str, beg=0,end=len(string)) |
34 | strip([chars]) |
35 | swapcase() |
36 | title() |
37 | translate(table, deletechars="") |
38 | upper() |
39 | zfill (width) |
40 | isdecimal() |