标签:
1: >>> mystr="Test string"
2: >>> mystr[0] = ‘t‘
3: Traceback (most recent call last):
4: File "<pyshell#1>", line 1, in <module>
5: mystr[0] = ‘t‘
6: TypeError: ‘str‘ object does not support item assignment
7: >>>
1: #一般格式化
2: >>> myformat = "Hello, my name is %s %s"
3: >>> name = (‘Bill‘,‘Gunn‘)
4: >>> print (myformat % name)
5: Hello, my name is Bill Gunn
6: >>>
7:
8: #用列表格式化
9: >>> myformat = ‘Hello, my name is %s‘
10: >>> name=[‘Bill‘, ‘Gunn‘]
11: >>> print(myformat % name)
12: Hello, my name is [‘Bill‘, ‘Gunn‘]
13:
14: #打印浮点数
15: >>> import math
16: >>> print ("PI = %.5f" % pi)
17: PI = 3.14159
18:
19: #打印百分号
20: >>> print("%.2f%%"% 22.3)
21: 22.30%
22: >>>
1: #从string模块中导入Template
2: >>> from string import Template
3: #创建模板
4: >>> myformat = Template("My name is $name")
5: #替换变量并打印
6: >>> print(myformat.substitute(name="Bill Gunn"))
7: My name is Bill Gunn
8: >>>
9:
10: #输出美元符号的方法,在模板里输入两个$
11: >>> mytemplate = Template("The price is $$$price")
12: >>> mytemplate.substitute(price=100)
13: ‘The price is $100‘
14: >>>
15:
16: #如果参数与后面的字符串相连,需要用大括号将其括起来
17: >>> from string import Template
18: >>> mytemplate = Template("It‘s ${x}tastic!")
19: >>> mytemplate.substitute(x=‘slum‘)
20: "It‘s slumtastic!"
21: >>>
22:
23: #使用字典替换参数
24: >>> mytemplate = Template("My $property is $value")
25: >>> name = {}
26: >>> name["property"] = "name"
27: >>> name["value"] = "Bill Gunn"
28: >>> mytemplate.substitute(name)
29: ‘My name is Bill Gunn‘
30: >>>
31:
1: >>> data = tuple(list("123"))
2: >>> data
3: (‘1‘, ‘2‘, ‘3‘)
4: #格式化字符串中只有一个转义说明符,而元组中有三个元素,转换会报错
5: >>> print ("data is %s" % data)
6: Traceback (most recent call last):
7: File "<pyshell#18>", line 1, in <module>
8: print ("data is %s" % data)
9: TypeError: not all arguments converted during string formatting
10: #显示元组中的全部元素
11: >>> print ("data is %s %s %s" % data)
12: data is 1 2 3
13: >>>
14:
转义说明符 | 含义 |
---|---|
d,i | 带符号的十进制整数 |
o | 不带符号的八进制 |
u | 不带符号的十进制 |
x | 不带符号的十六进制(小写) |
X | 不带符号的十六进制(大写) |
e | 科学计数法的浮点数(小写) |
E | 科学计数法的浮点数(大写) |
f,F | 十进制浮点数 |
g | 如果指数大于-4或者小于精度值则和e相同,否则和f相同 |
G | 如果指数大于-4或者小于精度值则和E相同,否则和F相同 |
C | 单字符(接受整数或者单字符字符串) |
r | 字符串(使用repr转换任意Python对象) |
s | 字符串(使用str转换任意Python对象) |
1: #十进制整数
2: >>> print ("The price is $%d" % 12)
3: The price is $12
4:
5: #十六进制整数
6: >>> print ("Hex %x" % 12)
7: Hex c
8:
9: #八进制整数
10: >>> print ("Oct %o" % 12)
11: Oct 14
12: >>>
13:
1: #限制宽度
2: >>> "%10f" % math.pi
3: ‘ 3.141593‘
4:
5: #限制小数位数
6: >>> "%5.2f" % math.pi
7: ‘ 3.14‘
8:
9: #用星号限制宽度和精度,下例中,宽度为10,精度为5
10: >>> ‘%*.*s‘ % (10, 5, ‘adfasdfadsfasdfasdfasdf‘)
11: ‘ adfas‘
12: >>>
13:
1: #空白补0
2: >>> print ("%010f" % math.pi)
3: 003.141593
4:
5: #左对齐
6: >>> "%-10.2f" % math.pi
7: ‘3.14 ‘
8:
9: #空白右对齐
10: >>> print("% 5d\n% 5d" % (123, 12))
11: 123
12: 12
13:
14: #显示正负符号
15: >>> print ("%+5d\n%+5d" % (123, -123))
16: +123
17: -123
18: >>>
19:
1: >>> string.ascii_letters
2: ‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ‘
3: >>> letters = string.ascii_letters
4: >>> letters.find(‘AB‘)
5: 26
6: >>> letters.find(‘X‘,30,-1)
7: 49
8: >>> letters.find("AB",26)
9: 26
10:
1: >>> data = list(‘123456‘)
2: >>> data
3: [‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘]
4: >>> "AB".join(data)
5: ‘1AB2AB3AB4AB5AB6‘
6: >>>
7:
1: >>> mystr="ABCD"
2: >>> mystr.lower()
3: ‘abcd‘
4: >>> mystr
5: ‘ABCD‘
6: >>>
7:
1: >>> mystr = "My name is Geng Qi"
2: >>> mystr.replace("Geng Qi", "Bill Gunn")
3: ‘My name is Bill Gunn‘
4: >>>
1: #以加号为分割符
2: >>> mystr = "1+2+3+4+5+6"
3: >>> mystr.split(‘+‘)
4: [‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘]
5:
6: #不提供分割符时,以空白符为分割符
7: >>> mystr = "This is a test string"
8: >>> mystr.split()
9: [‘This‘, ‘is‘, ‘a‘, ‘test‘, ‘string‘]
10: >>>
11:
1: >>> mystr = " asdfad adfasf asdf "
2: >>> mystr
3: ‘ \tasdfad adfasf asdf \t\t‘
4: #去除空白符
5: >>> mystr.strip()
6: ‘asdfad adfasf asdf‘
7:
8: #去除指定字符
9: >>> mystr.strip(‘\t‘)
10: ‘ \tasdfad adfasf‘ asdf ‘
11: >>>
12:
1: >>> table = str.maketrans(‘cs‘, ‘kz‘)
2: >>> table
3: {115: 122, 99: 107}
4: >>> "Please don‘t knock at my door!".translate(table)
5: "Pleaze don‘t knokk at my door!"
标签:
原文地址:http://www.cnblogs.com/xinguichun/p/5703762.html