Python print函数用法,print 格式化输出
原文地址:http://blog.csdn.net/zanfeng/article/details/52164124
使用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) ...
- [转载]Python print函数用法,print 格式化输出
使用print输出各型的 字符串 整数 浮点数 出度及精度控制 strHello = 'Hello Python' print strHello #输出结果:Hello Python #直接出字符串 ...
- 【313】python 中 print 函数用法总结
参考:python 中 print 函数用法总结 参考:Python print() 函数(菜鸟教程) 参考:Python 3 print 函数用法总结 目录: 字符串和数值类型 变量 格式化输出 p ...
- python骚操作---Print函数用法
---恢复内容开始--- python骚操作---Print函数用法 在 Python 中,print 可以打印所有变量数据,包括自定义类型. 在 3.x 中是个内置函数,并且拥有更丰富的功能. 参数 ...
- 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回调函数用法实例详解
本文实例讲述了Python回调函数用法.分享给大家供大家参考.具体分析如下: 一.百度百科上对回调函数的解释: 回调函数就是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函 ...
- 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], ...
随机推荐
- Centos7上搭建ftp服务器
ftp服务器搭建 1.安装好centos系统,配好yum仓库 其中vsftpd源在这下载 http://rpmfind.net/linux/rpm2html/search.php?query=vsft ...
- The.Glory.of.Innovation 创新之路2科学基石
犹太民族很早就确立了他们的生存法则:资源.土地,以及一切有形的东西都会消失,一个人最重要的财富是自己的头脑.是知识.是创造. 有些选择是被动的,有些选择是主动的,一旦决心要把技术变成自己的,独立的 ...
- Java生成生成密码类
import java.util.Date; import java.util.Random; public class PasswordUtil { public final static Stri ...
- SQL Server索引的执行计划
如何知道索引有问题,最直接的方法就是查看执行计划.通过执行计划,可以回答表上的索引是否被使用的问题. (1)包含索引:避免书签查找 常见的索引方面的性能问题就是书签查找,书签查找分为RID查找和键值查 ...
- Leetcode刷题第001天
一.合并两个有序链表 [题目]206. 反转链表 /** * Definition for singly-linked list. * struct ListNode { * int val; * L ...
- WPF在XAML中实现持续动画的暂停、恢复、停止
1.动画通过EventTrigger监听按钮的FrameworkElement.Loaded事件,但控件载入时就进行动画, 持续动画通过<BeginStoryboard Name="y ...
- hihocoder 前两题思路
1800 : 玩具设计师 二维前缀和的写法有很多,最常见的是s[x-1][y]+s[x][y-1]-s[x-1][y-1]+a[x][y]; 涉及二维矩阵求和,联想前缀和,求>=指定面积的最大耐 ...
- java传值和传引用区别
1. 在java中所有的参数都是传值的,引用符号&的传递是C++中才有的:2. 在java传参中,基本类型(byte--short--int--long--float--double--boo ...
- CentOS7安装MySQL并设置远程登录
在CentOS中默认安装有MariaDB,这个是MySQL的分支,但为了需要,还是要在系统中安装MySQL,而且安装完成之后可以直接覆盖掉MariaDB. 1 下载并安装MySQL官方的 Yum Re ...
- Note for "Some Remarks on Writing Mathematical Proofs"
John M. Lee is a famous mathematician, who bears the reputation of writing the classical book " ...