Python print() 格式化输出

Python print{} 格式化输出

  • [1. `print()`](#1. print())
  • [2. 浮点数 (float)](#2. 浮点数 (float))
  • References

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,

相关推荐
JamSlade5 小时前
SSO登录验证设计要点细节(以微软 Microsoft SSO为例) 基于react python
python·react.js·microsoft
MediaTea5 小时前
Python 文件操作:JSON 格式
开发语言·windows·python·json
百锦再6 小时前
金仓数据库提出“三低一平”的迁移理念
开发语言·数据库·后端·python·rust·eclipse·pygame
野生工程师6 小时前
【Python爬虫基础-1】爬虫开发基础
开发语言·爬虫·python
力江6 小时前
攻克维吾尔语识别的技术实践(多语言智能识别系统)
人工智能·python·自然语言处理·语音识别·unicode·维吾尔语
诗句藏于尽头6 小时前
MediaPipe+OpenCV的python实现交互式贪吃蛇小游戏
人工智能·python·opencv
盼哥PyAI实验室6 小时前
Python 正则表达式实战 + 详解:从匹配QQ邮箱到掌握核心语法
python·mysql·正则表达式
木易 士心6 小时前
Android 开发核心技术深度解析
android·开发语言·python
nju_spy7 小时前
力扣每日一题(四)线段树 + 树状数组 + 差分
数据结构·python·算法·leetcode·面试·线段树·笔试
lzq6037 小时前
Python虚拟环境全指南:venv与conda对比与实践
开发语言·python·conda