[转载]Python print函数用法,print 格式化输出

时间:2022-01-08 20:17:51

使用print输出各型的

  1. 字符串
  2. 整数
  3. 浮点数
  4. 出度及精度控制
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 格式化输出的更多相关文章

  1. Python 3 print 函数用法总结

    Python 3 print 函数用法总结 1. 输出字符串和数字 print("runoob")    # 输出字符串 runoob print(100)            ...

  2. 【313】python 中 print 函数用法总结

    参考:python 中 print 函数用法总结 参考:Python print() 函数(菜鸟教程) 参考:Python 3 print 函数用法总结 目录: 字符串和数值类型 变量 格式化输出 p ...

  3. python骚操作---Print函数用法

    ---恢复内容开始--- python骚操作---Print函数用法 在 Python 中,print 可以打印所有变量数据,包括自定义类型. 在 3.x 中是个内置函数,并且拥有更丰富的功能. 参数 ...

  4. Python回调函数用法实例详解

    本文实例讲述了Python回调函数用法.分享给大家供大家参考.具体分析如下: 一.百度百科上对回调函数的解释: 回调函数就是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函 ...

  5. python之函数用法__getitem__()

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法__getitem__() #http://www.cnblogs.com/hongf ...

  6. python之函数用法__str__()

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法__str__() #http://www.cnblogs.com/hongfei/p ...

  7. Python回调函数用法实例

    Python回调函数用法实例 作者:no.body链接:https://www.zhihu.com/question/19801131/answer/27459821 什么是回调函数? 我们绕点远路来 ...

  8. python基础之 while 逻辑运算符 格式化输出等

    1.while循环 while 条件: 循环体 while 条件: 循环体 else: 循环体 重点: 当条件为真的时候,就进入循环体,从上到下依次执行,执行完最后一条语句时,while并不是直接退出 ...

  9. python之函数用法setdefault()

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法setdefault() #D.get(k,d) #说明:k在D中,则返回 D[K], ...

随机推荐

  1. 数据库 数据库SQL语句二

    单行函数 --操作数据对象 --接受参数返回一个结果 --只对一行进行变换 --每行返回一个结果 --可以转换数据类型 --可以嵌套 --参数可以是一列或一个值 字符函数 SQL> select ...

  2. Puppet简易入门

    一.查看官方提供的下载源 https://docs.puppet.com/guides/puppetlabs_package_repositories.html 二. 选择对应系统的下载源 因为本机是 ...

  3. BZOJ 2761: [JLOI2011]不重复数字 水题

    2761: [JLOI2011]不重复数字 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2100  Solved: 809 题目连接 http:// ...

  4. 如何在CentOS 7上安装EPEL源

    EPEL 是什么? EPEL (Extra Packages for Enterprise Linux,企业版Linux的额外软件包) 是Fedora小组维护的一个软件仓库项目,为RHEL/CentO ...

  5. 【转】Tomcat中server.xml配置图

    http://www.cnblogs.com/ywl925/archive/2013/02/28/2936926.html Tomcat Server的结构图 该文件描述了如何启动Tomcat Ser ...

  6. 第一节: dingo/API 最新版 V2.0 之安装讲解(连载)

    我发现关于dingo/API V2.0的资料少之又少,应该也是发布时间不久的原因.下面,我就来给大家讲解(翻译)下官方的英文文档,如果有说的不对的地方,请指正.先附上,官网wiki地址https:// ...

  7. grep正则表达式搜索

    grep -n -e "INT32 *AdaptorPrmOp" --include "*.c"  -r ./ 搜索函数的定义 中间有n个空格

  8. java 获取class文件所在路径

      java 获取class文件所在路径 CreateTime--2017年11月14日15:14:32 Author:Marydon 1.获取class文件所在路径(磁盘路径.绝对路径) // 获取 ...

  9. bzoj 3611(洛谷 4103) [Heoi2014]大工程——虚树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3611 https://www.luogu.org/problemnew/show/P4103 ...

  10. gradle下的第一个SpringMVC应用

    新建gradle project 缺少了很多文件夹和文件,我们自己补充,补充完的目录如下: HelloController: package controller; import javax.serv ...