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], ...
随机推荐
- spring cloud Hystix熔断机制--基本熔断配置和Fegin client熔断配置
所谓的熔断机制和日常生活中见到电路保险丝是非常相似的,当出现了问题之后,保险丝会自动烧断,以保护我们的电器, 那么如果换到了程序之中呢? 当现在服务的提供方出现了问题之后整个的程序将出现错误的信息显示 ...
- 20165206第4次实验《Android程序设计》实验报告
20165206第4次实验<Android程序设计>实验报告 一.实验报告封面 课程:Java程序设计 班级:1652班 姓名:韩啸 学号:20165206 指导教师:娄嘉鹏 实验日期:2 ...
- CSS预处理器—Sass、LESS和Stylus
http://www.w3cplus.com/css/css-preprocessor-sass-vs-less-stylus-2.html 一.什么是CSS预处器 CSS预处理器定义了一种新的语言, ...
- 【转】flannel网络的VXLAN及host-gw
http://www.fly63.com/article/detial/1738 VXLAN是Linux内核本身支持的一种网络虚拟化技术,是内核的一个模块,在内核态实现封装解封装,构建出覆盖网络,其实 ...
- SPOJ 1812 LCS2
题解: 和上一题差不多 每个点记录前面的到这个点的最大值 初值赋为len[i] 然后注意要用子节点更新父节点 代码: #include <bits/stdc++.h> #define ll ...
- BeautifulSoup下Unicode乱码解决
今天在用scrapy爬某个网站的数据,其中DOM解析我用的是BeautifulSoup,速度上没有XPath来得快,不过因为用了习惯了,所以一直用的bs,版本是bs4 不过在爬取过程中遇到了一些问题, ...
- Python_部分内置函数
内置函数:可以直接调用的函数 all():传入的列表,元组,等等,只要一个为假,就为假(fales)(所有的都为真才为真) # None, {}:空字典, []:空列表, 0:零,():空集合,“”: ...
- echarts Y轴的刻度 跟数据对应---tooltip-formatter
var xAxisData = ['2018-01', '2018-02', '2018-03', '2018-04', '2018-05', '2018-06', '2018-07', '2018- ...
- UOJ#219/BZOJ4650 [NOI2016]优秀的拆分 字符串 SA ST表
原文链接http://www.cnblogs.com/zhouzhendong/p/9025092.html 题目传送门 - UOJ#219 (推荐,题面清晰) 题目传送门 - BZOJ4650 题意 ...
- TreeSet集合的自然排序与比较器排序、Comparable接口的compareTo()方法
[自然排序] package com.hxl; public class Student implements Comparable<Student> { private String n ...