标签:
python 字符串
一、Python字符串转义字符
| 转义字符 | 描述 |
|---|---|
| \(在行尾时) | 续行符 |
| \\ | 反斜杠符号 |
| \‘ | 单引号 |
| \" | 双引号 |
| \a | 响铃 |
| \b | 退格(Backspace) |
| \e | 转义 |
| \000 | 空 |
| \n | 换行 |
| \v | 纵向制表符 |
| \t | 横向制表符 |
| \r | 回车 |
| \f | 换页 |
| \oyy | 八进制数,yy代表的字符,例如:\o12代表换行 |
| \xyy | 十六进制数,yy代表的字符,例如:\x0a代表换行 |
| \other | 其它的字符以普通格式输出 |
二、Python字符串格式化
| 符 号 | 描述 |
|---|---|
| %c | 格式化字符及其ASCII码 |
| %s | 格式化字符串 |
| %d | 格式化整数 |
| %u | 格式化无符号整型 |
| %o | 格式化无符号八进制数 |
| %x | 格式化无符号十六进制数 |
| %X | 格式化无符号十六进制数(大写) |
| %f | 格式化浮点数字,可指定小数点后的精度 |
| %e | 用科学计数法格式化浮点数 |
| %E | 作用同%e,用科学计数法格式化浮点数 |
| %g | %f和%e的简写 |
| %G | %f 和 %E 的简写 |
| %p | 用十六进制数格式化变量的地址 |
格式化操作符辅助指令:
| 符号 | 功能 |
|---|---|
| * | 定义宽度或者小数点精度 |
| - | 用做左对齐 |
| + | 在正数前面显示加号( + ) |
| <sp> | 在正数前面显示空格 |
| # | 在八进制数前面显示零(‘0‘),在十六进制前面显示‘0x‘或者‘0X‘(取决于用的是‘x‘还是‘X‘) |
| 0 | 显示的数字前面填充‘0‘而不是默认的空格 |
| % | ‘%%‘输出一个单一的‘%‘ |
| (var) | 映射变量(字典参数) |
| m.n. | m 是显示的最小总宽度,n 是小数点后的位数(如果可用的话) |
例子:
|
1
2
3
4
|
>>> print ("dec:%d oct:%o hex:%x float:%.2f" %(a, a, a, a))dec:123456 oct:361100 hex:1e240 float:123456.00>>> print ("test %s" %b)test hello world |
三、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‘ 输出 \n 和 print R‘\n‘输出 \n |
例子:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
>>> a = "hello">>> b = "world">>> a + b‘helloworld‘>>> a * 2‘hellohello‘>>> a[0]‘h‘>>> a[2:4]‘ll‘>>> if ‘o‘ in a:... print ("o in string a")... o in string a>>> if "b" not in a:... print ("b not in string a")... b not in string a |
四、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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
#字符串去除空格>>> str1 = " hello ">>> str2 = "world">>> print (str1 + str2) hello world>>> print (str1.lstrip() + str2)hello world>>> print (str1.rstrip() + str2) helloworld>>> print (str1.strip() + str2)helloworld#查找字符串的索引,找到返回索引值,找不到则报错>>> b.index("o")4>>> b.index("a")Traceback (most recent call last): File "<input>", line 1, in <module>ValueError: substring not found#字符串长度>>> str1 = " test ">>> len(str1)9>>> len(str1.strip())4#输出相同字符>>> a = "12345">>> b = "345">>> a and b‘345‘#将字符串中的字符大小写转换>>> str1 = "hellOWorLD">>> str1.lower()‘helloworld‘>>> str1.upper()‘HELLOWORLD‘>>> str1.swapcase()‘HELLowORld‘>>> str1.capitalize()‘Helloworld‘#复制指定字符串长度>>> str2 = str1[3:7]>>> print (str2)lOWo#字符串逆序输出>>> str1 = "123456789">>> str1[::-1]‘987654321‘#字符串转换为列表>>> str1 = "1 2 3 4 5 6 7 8 9">>> str1.split(" ")[‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘]#列表转换为字符串>>> list1 = [‘a‘, ‘b‘, ‘c‘, ‘d‘]>>> ‘‘.join(list1)‘abcd‘ |
标签:
原文地址:http://www.cnblogs.com/Kingway-Python/p/5815632.html