python 的格式化输出 f-string
f-string格式化字符串。
从%s格式化到format格式化再到f-string格式化,格式化的方式越来越直观,
f-string的效率也较前两个高一些,使用起来也比前两个简单一些。
基本使用
f-string用大括{ }表示被替换字段,其中直接填入替换内容即可。
1 2 3 from datetime import date, time, datetimefrom chinese_calendar import is_workdayimport pandas as pd
1 2 3 4 x, y = 9 , 99 d = date.today() f'今天是:{d} ,"x"={x} ,"y"={y} '
Output: '今天是:2022-04-05,"x"=9,"y"=99'
表达式求值与函数调用
f-string的大括号{ }可以填入表达式或调用函数,Python会求出其结果并填入返回的字符串内。
f-string中使用lambda匿名函数:可以做复杂的数值计算,切记要将函数表达式和传参分别用小括号包含起来。
1 2 3 x = 9 y = 19 f'y/x整除:{y//9 } , y/x的余数:{y%x} , y/x取两位小数:{y/x:.2 f} , y/x取两位小数:{round (y/x,2 ) } , 数字y的科学计数:{y:e} '
Output:
'y/x整除:2, y/x的余数:1, y/x取两位小数:2.11, y/x取两位小数:2.11 , 数字y的科学计数:1.900000e+01'
1 2 3 4 x = 9 y = 19 f'{(lambda a,b: a**2 -b )(x,y):.2 f} ' f'{(lambda x :"今天要上班" if is_workday(x) else "今天休息" )(d) } '
Output:
'62.00'
'今天休息'
f-string中引号使用存在的问题
f-string大括号内使用的引号不能和大括号外的引号定界符引号冲突,需根据情况灵活切换使用单引号、双引号、单三引号、双三引号。
只要大括号内外的引号不同,就没有问题。
大括号中只能是单引号和 双引号 ,大括号外的引号定界符引号可以使用单引号、双引号、单三引号、双三引号。
大括号外的引号还可以使用\转义,但大括号内不能使用\转义
1 f'今天是:{date.today()} 周 {(lambda x :"今天要上班" if is_workday(x) else "今天是中国的节假日" )(d) } '
Output:
'今天是:2022-04-05 周 今天是中国的节假日'
1 f'这里用到转义符,仅仅能在大括号外:I\'m number "{x} "'
Output: ‘这里用到转义符,仅仅能在大括号外:I'm number “9”‘
数字格式化
正负号的使用
宽度与精度相关格式描述符:保留小数点位数
截断与填充的结合使用
针对date、datetime和time对象,进行年月日、时分秒等信息提取
1 2 3 4 5 6 x = 9 y = -99 f'正数前加“+”:{x :+} ,负数不改变:{y:+} ' f'正数无变化:{x:-} ,负数也无变化:{y:-} ' f'正数前增加空格:{x: } , 负数无变化:{y: } '
Output:
'正数前加“+”:+9,负数不改变:-99'
'正数无变化:9,负数也无变化:-99 '
'正数前增加空格: 9, 负数无变化:-99'
1 2 3 4 5 x = 99.1234 f'{x:g} , {x:.1 g} , {x:.2 g} , {x:.3 g} ' f'{x:f} , {x:.1 f} , {x:.2 f} , {x:.3 f} ' f'{x:e} , {x:.1 e} , {x:.2 e} , {x:.3 e} '
Output:
1 2 3 '99.1234, 1e+02, 99, 99.1' '99.123400, 99.1, 99.12, 99.123' '9.912340e+01, 9.9e+01, 9.91e+01, 9.912e+01'
1 2 3 4 5 6 7 x, y = 99.12345 , 'python' f'{x:20 } ' f'{y:20 } ' f'{x:020} '
Output:
1 2 3 ' 99.12345' 'python ' '00000000000099.12345'
1 2 3 4 5 6 7 8 x = 99.12345 y = 'python' f'{x:<20 } ' f'{y:^20 } ' f'{x:-^20 } ' f'{y:*>20 } '
Output:
1 2 3 4 '99.12345 ' ' python ' '------99.12345------' '**************python'
1 2 3 4 y = 'python' f'{y:.4 } ' f'{y:10.5 } '
Output:
1 2 3 4 5 x = 999.123 f'{x:<15.2 f} ' f'{x:~^15.2 f} '
Output:
1 2 '999.12 ' '~~~~999.12~~~~~'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 d = date.today() d1 = datetime.now() d, d1 f"{d:%y-%m-%d} ,{d:%Y-%m-%d} " f"{d:%Y} ,{d:%y} " f"{d:%m} {d:%d} " f"{d1:%H} ,{d1:%M} ,{d1:%S} " f'{d:%W} {d:%w} '
Output:
1 2 3 4 5 6 (datetime.date(2022, 4, 5), datetime.datetime(2022, 4, 5, 16, 4, 46, 131598)) '22-04-05,2022-04-05' '2022,22' '04 05' '16,04,46' '14 2'
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 from chinese_calendar import workdays, utils, get_workdays, get_holidays, get_holiday_detail, is_workdayd1 = datetime.now() wd = { '星期一' : 1 , '星期二' : 2 , '星期三' : 3 , '星期四' : 4 , '星期五' : 5 , '星期六' : 6 , '星期日' : 7 , } a = int (f'{d1:%w} ' ) if get_holiday_detail(d)[0 ]: b = "今天是:" + get_holiday_detail(d)[1 ] else : b = "" print (f'''此时此刻:{d1} ; {(lambda x: [k for k, v in wd.items() if v == x][0 ])(a)} ; {(lambda x: "今天要上班" if is_workday(x) else "今天休假" )(d1)} ; {b} 今年的第 {d1:%W} 周; 第 {(d-date(2021 , 12 , 31 )).days} 天; ''' )
Output:
1 2 3 4 5 6 此时此刻:2022-04-05 16:04:46.148597; 星期二; 今天休假; 今天是:Tomb-sweeping Day 今年的第 14 周; 第 95 天;
乘法表 1 2 3 4 for i in range (1 , 10 ): for j in range (1 , i + 1 ): print (f'{j} *{i} ={i*j} ' , end="\t " ) print ("\n" )
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
两个堆积数字的示例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 def print_num1 (x ): ls = [i for i in range (1 , x, 1 )] sum (ls) * 2 + x width = 4 * x + 10 ls1 = [f'{i:03} ' for i in range (sum (ls) * 2 + x)] m = 0 n = 1 for i in range (len (ls1)): for n in range (1 , x, 1 ): a = " " for j in ls1[m:m + n]: a += (" " + j) print (f'{a:^{width} }' ) m = m + n for n in range (x, 1 , -1 ): a = " " for j in ls1[m:m + n]: a += (" " + j) print (f'{a:^{width} }' ) m = m + n
000
001 002
003 004 005
006 007 008 009
010 011 012 013 014
015 016 017 018 019 020
021 022 023 024 025 026 027
028 029 030 031 032 033 034 035
036 037 038 039 040 041 042 043 044
045 046 047 048 049 050 051 052 053 054
055 056 057 058 059 060 061 062 063 064 065
066 067 068 069 070 071 072 073 074 075 076 077
078 079 080 081 082 083 084 085 086 087 088 089 090
091 092 093 094 095 096 097 098 099 100 101 102 103 104
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
379 380 381 382 383 384 385 386 387 388 389 390 391 392
393 394 395 396 397 398 399 400 401 402 403 404 405
406 407 408 409 410 411 412 413 414 415 416 417
418 419 420 421 422 423 424 425 426 427 428
429 430 431 432 433 434 435 436 437 438
439 440 441 442 443 444 445 446 447
448 449 450 451 452 453 454 455
456 457 458 459 460 461 462
463 464 465 466 467 468
469 470 471 472 473
474 475 476 477
478 479 480
481 482
483
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 def print_num2 (x ): if x % 2 == 0 : x = x + 1 ls = [i for i in range (1 , x, 2 )] sum (ls) * 2 + x width = 4 * x + 10 ls1 = [f'{i:03} ' for i in range (sum (ls) * 2 + x)] m = 0 n = 1 for i in range (len (ls1)): for n in range (1 , x, 2 ): a = " " for j in ls1[m:m + n]: a += (" " + j) print (f'{a:>{width} }' ) m = m + n for n in range (x, 1 , -2 ): a = " " for j in ls1[m:m + n]: a += (" " + j) print (f'{a:>{width} }' ) m = m + n
000
001 002 003
004 005 006 007 008
009 010 011 012 013 014 015
016 017 018 019 020 021 022 023 024
025 026 027 028 029 030 031 032 033 034 035
036 037 038 039 040 041 042 043 044 045 046 047 048
049 050 051 052 053 054 055 056 057 058 059 060 061 062 063
064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080
081 082 083 084 085 086 087 088 089 090 091 092 093 094 095
096 097 098 099 100 101 102 103 104 105 106 107 108
109 110 111 112 113 114 115 116 117 118 119
120 121 122 123 124 125 126 127 128
129 130 131 132 133 134 135
136 137 138 139 140
141 142 143
144