python string模块ascii_Python String模块详解

1 >>> '{0}, {1}, {2}'.format('a', 'b', 'c')2

3 'a, b, c'

4

5 >>> '{}, {}, {}'.format('a', 'b', 'c') #2.7+ only

6

7 'a, b, c'

8

9 >>> '{2}, {1}, {0}'.format('a', 'b', 'c')10

11 'c, b, a'

12

13 >>> '{2}, {1}, {0}'.format(*'abc') #unpacking argument sequence

14

15 'c, b, a'

16

17 >>> '{0}{1}{0}'.format('abra', 'cad') #arguments' indices can be repeated

18

19 'abracadabra'

20

21 >>> 'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W')22

23 'Coordinates: 37.24N, -115.81W'

24

25 >>> coord = {'latitude': '37.24N', 'longitude': '-115.81W'}26

27 >>> 'Coordinates: {latitude}, {longitude}'.format(**coord)28

29 'Coordinates: 37.24N, -115.81W'

30

31 >>> c = 3-5j

32

33 >>> ('The complex number {0} is formed from the real part {0.real}'

34

35 ... 'and the imaginary part {0.imag}.').format(c)36

37 'The complex number (3-5j) is formed from the real part 3.0 and the imaginary part -5.0.'

38

39 >>> classPoint(object):40

41 ... def __init__(self, x, y):42

43 ... self.x, self.y =x, y44

45 ... def __str__(self):46

47 ... return 'Point({self.x}, {self.y})'.format(self=self)48

49 ...50

51 >>> str(Point(4, 2))52

53 'Point(4, 2)

54

55 >>> coord = (3, 5)56

57 >>> 'X: {0[0]}; Y: {0[1]}'.format(coord)58

59 'X: 3; Y: 5'

60

61 >>> "repr() shows quotes: {!r}; str() doesn't: {!s}".format('test1', 'test2')62

63 "repr() shows quotes: 'test1'; str() doesn't: test2"

64

65 >>> '{:<30}'.format('left aligned')66

67 'left aligned'

68

69 >>> '{:>30}'.format('right aligned')70

71 'right aligned'

72

73 >>> '{:^30}'.format('centered')74

75 'centered'

76

77 >>> '{:*^30}'.format('centered') #use '*' as a fill char

78

79 '***********centered***********'

80

81 >>> '{:+f}; {:+f}'.format(3.14, -3.14) #show it always

82

83 '+3.140000; -3.140000'

84

85 >>> '{: f}; {: f}'.format(3.14, -3.14) #show a space for positive numbers

86

87 '3.140000; -3.140000'

88

89 >>> '{:-f}; {:-f}'.format(3.14, -3.14) #show only the minus -- same as '{:f}; {:f}'

90

91 '3.140000; -3.140000'

92

93 >>> #format also supports binary numbers

94

95 >>> "int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}".format(42)96

97 'int: 42; hex: 2a; oct: 52; bin: 101010'

98

99 >>> #with 0x, 0o, or 0b as prefix:

100

101 >>> "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}".format(42)102

103 'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010'

104

105 >>> '{:,}'.format(1234567890)106

107 '1,234,567,890'

108

109 >>> points = 19.5

110

111 >>> total = 22

112

113 >>> 'Correct answers: {:.2%}.'.format(points/total)114

115 'Correct answers: 88.64%'

116

117 >>> importdatetime118

119 >>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)120

121 >>> '{:%Y-%m-%d %H:%M:%S}'.format(d)122

123 '2010-07-04 12:15:58'

124

125 >>> for align, text in zip('', ['left', 'center', 'right']):126

127 ... '{0:{fill}{align}16}'.format(text, fill=align, align=align)128

129 ...130

131 'left<<<<<<<<<<<

132

133 '^^^^^center^^^^^'

134

135 '>>>>>>>>>>>right'

136

137 >>>

138

139 >>> octets = [192, 168, 0, 1]140

141 >>> '{:02X}{:02X}{:02X}{:02X}'.format(*octets)142

143 'C0A80001'

144

145 >>> int(_, 16)146

147 3232235521

148

149 >>>

150

151 >>> width = 5

152

153 >>> for num in range(5,12):154

155 ... for base in 'dXob':156

157 ... print '{0:{width}{base}}'.format(num, base=base, width=width),158

159 ... print

160

161 ...162

163 5 5 5 101

164

165 6 6 6 110

166

167 7 7 7 111

168

169 8 8 10 1000

170

171 9 9 11 1001

172

173 10 A 12 1010

174

175 11 B 13 1011

176

177 >>> from string importTemplate178

179 >>> s = Template('$who likes $what')180

181 >>> s.substitute(who='tim', what='kung pao')182

183 'tim likes kung pao'

184

185 >>> d = dict(who='tim')186

187 >>> Template('Give $who $100').substitute(d)188

189 Traceback (most recent call last):190

191 [...]192

193 ValueError: Invalid placeholder in string: line 1, col 10

194

195 >>> Template('$who likes $what').substitute(d)196

197 Traceback (most recent call last):198

199 [...]200

201 KeyError: 'what'

202

203 >>> Template('$who likes $what').safe_substitute(d)204

205 'tim likes $what'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值