惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Martin Fowler
Martin Fowler
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
雷峰网
雷峰网
J
Java Code Geeks
G
Google Developers Blog
博客园 - 司徒正美
The GitHub Blog
The GitHub Blog
L
LangChain Blog
人人都是产品经理
人人都是产品经理
GbyAI
GbyAI
Vercel News
Vercel News
S
SegmentFault 最新的问题
Engineering at Meta
Engineering at Meta
H
Hackread – Cybersecurity News, Data Breaches, AI and More
云风的 BLOG
云风的 BLOG
F
Fortinet All Blogs
Y
Y Combinator Blog
博客园_首页
Last Week in AI
Last Week in AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
罗磊的独立博客
A
About on SuperTechFans
B
Blog
Microsoft Security Blog
Microsoft Security Blog

博客园 - 小采采

python之jieba库 Python 标准库笔记(1) — String模块 python爬虫---selenium库的用法 Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等) python字符串截取、查找、分割 jupyter notebook快捷键使用指南 python中防止字符串转义 使用腾讯电脑管家清理电脑后,上不了网了 Python正则表达式指南 python之format函数 python安装media报错 Python version 2.7 required, which was not found in the registry python第三方库之PyGraphics Python中第三方的用于解析HTML的库:BeautifulSoup 83款 网络爬虫开源软件 基于CRF工具的机器学习方法命名实体识别的过 几款开源分词工具比较 win7中安装mysql JDK安装与环境变量配置
Python之print()函数
小采采 · 2017-06-15 · via 博客园 - 小采采

1. 输出字符串

>>> str = 'Hello World' 
>>> print (str)
Hello World

2. 格式化输出整数

支持参数格式化

>>> str = "the len '%s' is %d" %('Hello World',len('Hello World'))
>>> print (strHello) the length of (Hello World) is 11

3. 格式化输出16进制,十进制,八进制整数

#%x --- hex 十六进制 #%d --- dec 十进制 #%o --- oct 八进制  
>>> nHex = 0xFF 
>>> print("nHex = %x,nDec = %d,nOct = %o" %(nHex,nHex,nHex))
nHex = ff,nDec = 255,nOct = 377

4.格式化输出浮点数(float)

import math
>>> print('PI=%f'%math.pi) PI=3.141593 >>> print ("PI = %10.3f" % math.pi)
PI =      3.142 >>> print ("PI = %-10.3f" % math.pi)
PI = 3.142      >>> print ("PI = %06d" % int(math.pi)) PI = 000003

5. 格式化输出浮点数(float) 

>>> precise = 3 >>> print ("%.3s " % ("python")) pyt
>>> precise = 4 >>> print ("%.*s" % (4,"python")) pyth
>>> print ("%10.3s " % ("python"))        pyt

6.输出列表(List)

输出列表

>>> lst = [1,2,3,4,'python'] 
>>> print (lst)
[1, 2, 3, 4, 'python']

输出字典

>>> d = {1:'A',2:'B',3:'C',4:'D'}

>>> print(d)

{1: 'A', 2: 'B', 3: 'C', 4: 'D'}

7. 自动换行

print 会自动在行末加上回车,如果不需回车,只需在print语句的结尾添加一个逗号”,“,就可以改变它的行为。

>>> for i in range(0,6):     
print (i,)
0 
1
2
3
4
5

8.以任何字符结尾

会自动在行末加上回车,如果不需回车和换行,以某个字符结尾,只需在print语句的结尾添加一个逗号”,“end="*",就可以改变它的结束字符。

>>> for i in range(0,6):    
            print (i,end=':')

0:1:2:3:4:5

9.与format()函数配合使用进行格式化输出

具体来说,print()函数用槽格式和format()方法将变量和字符串结合到一起输出。如:

>>>c1=10.24024

>>>print("转换后的温度为{:.2f}C".format(c1))

转换后的温度为10.24C