使用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 #使用整数的各个制打印同一个数

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.'
$

 

转载于:https://www.cnblogs.com/zoe15/p/4419712.html

Python之print 格式化输出相关推荐

  1. python中print格式化输出%g_python怎么格式化输出

    详细内容 使用%格式化输出: 整数输出: %o -- oct 八进制 %d -- dec 十进制 %x -- hex 十六进制>>> print('%o' % 20) 24 > ...

  2. python的print格式化输出的format()方法和%两种方法

    目录 一.format用法 二.%用法 一.format用法 相对基本格式化输出采用'%'的方法,format()功能更强大,该函数把字符串当成一个模板,通过传入的参数进行格式化,并且使用大括号'{} ...

  3. python的print格式化输出,以及使用format来控制。

    20210305 time.strftime("%Y%m%d%H%M%S", time.localtime()) 时间格式化 20210206 https://www.runoob ...

  4. python 整数输出 d f_pythn print格式化输出---------%s 和 % d 都是什么意思?

    pythn print格式化输出. %r 用来做 debug 比较好,因为它会显示变量的原始数据(raw data),而其它的符 号则是用来向用户显示输出的. 1. 打印字符串 print (&quo ...

  5. python print()方法基本用法,print()格式化输出

    文章目录 print()语法 参数 输出类型 print()格式化输出 python格式化符号 综合实例 print()语法 print(*objects, sep=' ', end='\n', fi ...

  6. 第五章:稍息!立正!——print格式化输出,痴月熊学python

    痴月熊学Python 文章目录 痴月熊学Python 往期文章 前言 一.f-String格式化 二.占位符 三.format格式化 总结 系列文章 往期文章 第一章:Python-新人报道 第二章: ...

  7. python基础_格式化输出(%用法和format用法)(转载)

    python基础_格式化输出(%用法和format用法) 目录 %用法 format用法 %用法 1.整数的输出 %o -- oct 八进制 %d -- dec 十进制 %x -- hex 十六进制 ...

  8. Python 入门之格式化输出

    Python 入门之格式化输出 1.格式化 (1)%为占位 (2)%s - 站字符串的位置(数字.字符串都能够进行填充) 学习python中有什么不懂的地方,小编这里推荐加小编的python学习群:8 ...

  9. Python Base 字符串格式化输出

    Python的字符串格式化输出 概述 summary = '''Python用一个tuple(其实可以不写tuple括号也可以)将多个值传递给模板,每个值对应一个格式符. ''' example = ...

最新文章

  1. shell 脚本不能执行多条?何解
  2. CARTA:Gartner的持续自适应风险与信任评估战略方法简介
  3. 德语语言文学考研c1,2015-2016同济大学德语语言文学初试考研经验(下)
  4. php7 setcookie无效_PHP setcookie() 函数 | 菜鸟教程
  5. mysql命令查询语句
  6. 华为鸿蒙全能家居,能兑现多少?华为智慧屏十年不过时,用鸿蒙理念做智能家居...
  7. smp架构与numa架构_NUMA架构和Java
  8. android+udp传输大小,Android UDP数据包如何接收可变大小的数据包
  9. python 示例_带有示例的Python date timetuple()方法
  10. 【Luogu】P3343地震后的幻想乡(对积分概率进行DP)
  11. email邮件中 内嵌iframe_Python+Selenium执行结果,封装函数,用Python自动发送SMTP邮件...
  12. mysql数据库根目录恢复_MySQL中数据导入恢复的简单教程
  13. HDU3746 Cyclic Nacklace KMP求循环节
  14. 用计算机进行实时自动采集,《大学计算机基础》基础部分练习题_附件1
  15. 西门子触摸屏数据历史数据记录_西门子触摸屏参数跟数据简单说明
  16. 解决在湖北政务服务网注册武汉公积金账户时无法点击下一步的问题
  17. 基于机器学习的“能源之星”得分预测的完整演练
  18. 达梦数据库一些基础的SQI语句
  19. 微信小程序退出按钮回退到登录页面
  20. 超实用,一口气学会 Centos/Docker/Nginx/Node/Jenkins 等基础操作

热门文章

  1. 批量删除redis键
  2. securecrt连接GNS3步骤
  3. android.content.ActivityNotFoundException: No Activity found to handle Intent 的错误
  4. MapReduce框架中map、reduce方法的运行机制
  5. Mysql常用的命令
  6. Flutter Web:Shadow Root问题
  7. 记一次CPU飙升的问题分析解决思路(转)
  8. 超美的文件夹图标,右键秒改,实用方便适合文件夹分类
  9. CSUOJ-1980 不堪重负的数(区间dp)
  10. dbms_job涉及到的知识点