[转载]Python print函数用法,print 格式化输出
使用print输出各型的
- 字符串
- 整数
- 浮点数
- 出度及精度控制
strHello = 'Hello Python'
print strHello
#输出结果:Hello Python
#直接出字符串
1.格式化输出整数
python print也支持参数格式化,与C言的printf似,
strHello = "the length of (%s) is %d" %('Hello World',len('Hello World'))
print strHello
#输出果:the length of (Hello World) is 11
2.格式化输出16制整数
nHex = 0x20
#%x --- hex 十六进制
#%d --- dec 十进制
#%d --- oct 八进制
print "nHex = %x,nDec = %d,nOct = %o" %(nHex,nHex,nHex)
#输出结果:nHex = 20,nDec = 32,nOct = 40
#使用整数的各个制打印同一个数
如果需要输出二进制的话,可以使用python函数 bin()
Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> bin(789)
'0b1100010101'
>>>
3.格式化输出浮点数(float)
import math
#default
print "PI = %f" % math.pi
#width = 10,precise = 3,align = left
print "PI = %10.3f" % math.pi
#width = 10,precise = 3,align = rigth
print "PI = %-10.3f" % math.pi
#前面填充字符
print "PI = %06d" % int(math.pi)
#输出结果
#PI = 3.141593
#PI = 3.142
#PI = 3.142
#PI = 000003
#浮点数的格式化,精度、度和
4.格式化输出字符串(string)
#precise = 3
print "%.3s " % ("jcodeer")
#precise = 4
print "%.*s" % (4,"jcodeer")
#width = 10,precise = 3
print "%10.3s" % ("jcodeer")
#输出结果:
#jco
#jcod
# jco
#同于字符串也存在精度、度和。
5.输出列表(list)
l = [1,2,3,4,'jcodeer']
print l
#输出结果:[1, 2, 3, 4, 'jcodeer']
#于list直接打印即可
'''6.出字典(dictionary)'''
d = {1:'A',2:'B',3:'C',4:'D'}
print d
#输出结果:{1: 'A', 2: 'B', 3: 'C', 4: 'D'}
#同python也是支持dictionary出的
6.python print自动换行
print 会自动在行末加上回车,如果不需回车,只需在print语句的结尾添加一个逗号”,“,就可以改变它的行为。
for i in range(0,5):
print i,
或直接使用下面的函数进行输出:
sys.stdout.write("输出的字串")
7. 万能的 %r
有个同事问我python里面print ”%r” 是什么用途,被问倒了。
用了这么些年的python,还没用过print %r。
网上查了一下,发现%r是一个万能的格式付,它会将后面给的参数原样打印出来,带有类型信息。
python print %r 案例
formatter = "%r %r %r %r"
print formatter % (1, 2, 3, 4)
print formatter % ("one", "two", "three", "four")
print formatter % (True, False, False, True)
print formatter % (formatter, formatter, formatter, formatter)
print formatter % (
"I had this thing.",
"That you could type up right.",
"But it didn't sing.",
"So I said goodnight."
)
输出结果:
$ python ex8.py
1 2 3 4
'one' 'two' 'three' 'four'
True False False True
'%r %r %r %r' '%r %r %r %r' '%r %r %r %r' '%r %r %r %r'
'I had this thing.' 'That you could type up right.' "But it didn't sing." 'So I said goodnight.'
$
[转载]Python print函数用法,print 格式化输出的更多相关文章
- Python 3 print 函数用法总结
Python 3 print 函数用法总结 1. 输出字符串和数字 print("runoob") # 输出字符串 runoob print(100) ...
- 【313】python 中 print 函数用法总结
参考:python 中 print 函数用法总结 参考:Python print() 函数(菜鸟教程) 参考:Python 3 print 函数用法总结 目录: 字符串和数值类型 变量 格式化输出 p ...
- python骚操作---Print函数用法
---恢复内容开始--- python骚操作---Print函数用法 在 Python 中,print 可以打印所有变量数据,包括自定义类型. 在 3.x 中是个内置函数,并且拥有更丰富的功能. 参数 ...
- Python回调函数用法实例详解
本文实例讲述了Python回调函数用法.分享给大家供大家参考.具体分析如下: 一.百度百科上对回调函数的解释: 回调函数就是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函 ...
- python之函数用法__getitem__()
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法__getitem__() #http://www.cnblogs.com/hongf ...
- python之函数用法__str__()
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法__str__() #http://www.cnblogs.com/hongfei/p ...
- Python回调函数用法实例
Python回调函数用法实例 作者:no.body链接:https://www.zhihu.com/question/19801131/answer/27459821 什么是回调函数? 我们绕点远路来 ...
- python基础之 while 逻辑运算符 格式化输出等
1.while循环 while 条件: 循环体 while 条件: 循环体 else: 循环体 重点: 当条件为真的时候,就进入循环体,从上到下依次执行,执行完最后一条语句时,while并不是直接退出 ...
- python之函数用法setdefault()
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法setdefault() #D.get(k,d) #说明:k在D中,则返回 D[K], ...
随机推荐
- numpy模块之创建矩阵、矩阵运算
本文参考给妹子讲python https://zhuanlan.zhihu.com/p/34673397 NumPy是Numerical Python的简写,是高性能科学计算和数据分析的基础包,他是 ...
- Kubernetes List-Watch
list-watch,作为k8s系统中统一的异步消息传递方式,对系统的性能.数据一致性起到关键性的作用. list-watch操作需要做这么几件事: 由组件向apiserver而不是etcd发起wat ...
- K8s ipvs mode kube-proxy
IPVS vs. IPTABLES IPVS模式在Kubernetes 1.8中被引入,在1.9中进入beta测试. IPTABLES模式在1.1版本中被添加进来,在1.2开始就变成了默认的操作模式. ...
- MVC6 OWin Microsoft Identity 自定义验证
1. Startup.cs中修改默认的验证设置 //app.UseIdentity(); app.UseCookieAuthentication(options => { //options.A ...
- linux安装Zabbix监控
源码包3.4.0下载 https://nchc.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/3.4.0/zabbix-3.4 ...
- <转>xshell的快捷键
xshell中现有的快捷键 快捷方式键 说明 Alt + N 与文件菜单的新建相同 Alt + O 与文件菜单的打开相同 Alt + C 与文件菜单的断开相同 Alt + Enter 切换到全屏模式 ...
- netty上手
关于netty的基础NIO,请参见:NIO原理及实例 下面介绍Netty的上手使用: 首先为项目添加jar依赖: <dependency> <groupId>io.netty& ...
- Nginad Server安装
前言 Nginad是一个基于php的开源项目,它既可以作为静态配置的Ad Server,也可以作为动态的RTB Exchange使用.代码结构比较直接明了,挺适合用作学习的.本文如果有理解错误的地方, ...
- 80X86寄存器详解<转载>
引子 打算写几篇稍近底层或者说是基础的博文,浅要介绍或者说是回顾一些基础知识, 自然,还是得从最基础的开始,那就从汇编语言开刀吧, 从汇编语言开刀的话,我们必须还先要了解一些其他东西, 像 CPU ...
- Java -- JDBC mysql读写大数据,文本 和 二进制文件
1. 往mysql中读写字符文本 public class Demo1 { /* 创建数据库 create database LOBTest; use LOBTest; create table te ...