阅读量:0
Python print{} 格式化输出
1. print()
传递给函数的值称为参数。
引号没有打印在屏幕上,它们只是表示字符串的起止,不是字符串的一部分。可以用这个函数在屏幕上打印出空行,只要调用 print()
就可以了,括号内没有任何东西。
Microsoft Windows [版本 6.1.7601] 版权所有 (c) 2009 Microsoft Corporation。保留所有权利。 C:\Users\foreverstrong>python Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> print("Hello World!") Hello World! >>> exit() C:\Users\foreverstrong>
Microsoft Windows [版本 6.1.7601] 版权所有 (c) 2009 Microsoft Corporation。保留所有权利。 C:\Users\foreverstrong>python Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> width = 5 >>> width 5 >>> symbol1 = '*' >>> symbol1 '*' >>> print(symbol1 * width) ***** >>> symbol2 = 'x' >>> symbol2 'x' >>> print(symbol2 * width) xxxxx >>> symbol3 = '0' >>> symbol3 '0' >>> print(symbol3 + (' ' * (width - 2)) + symbol3) 0 0 >>> print(symbol2 + (' ' * (width + 2)) + symbol2) x x >>> print(symbol1 + (' ' * (width + 0)) + symbol1) * * >>> exit() C:\Users\foreverstrong>
2. 浮点数 (float)
#!/usr/bin/env python3 # -*- coding:utf-8 -*- import math # default print("PI = %f" % math.pi) # width = 10, precision = 3, alignment = right print("PI = %10.3f" % math.pi) # width = 10, precision = 3, alignment = left print("PI = %-10.3f" % math.pi) # precision = 4 print("PI = %.4f" % math.pi) # precision = 5 print("PI = %.5f" % math.pi) # width = 8 print("PI = %08d" % int(math.pi))
/usr/bin/python3.5 /home/strong/pyqt5_workspace/task_master.py PI = 3.141593 PI = 3.142 PI = 3.142 PI = 3.1416 PI = 3.14159 PI = 00000003 Process finished with exit code 0
References
[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/