python中print的用法_Python2与Python3中print用法总结
Python2中的print用法
在Python2 中 print 是一种输出语句
strHello = 'Hello Python'
print strHello
# Hello Python
1.格式化输出整数
strHello = "the length of (%s) is %d" %('Hello Wordld', len('Hello World'))
print strHello
# the length of (Hello Wordld) is 11
2.格式化输出16进制整数
# 格式 描述
# %% 百分号标记
# %c 字符及其ASCII码
# %s 字符串
# %d 有符号整数(十进制)
# %u 无符号整数(十进制)
# %o 无符号整数(八进制)
# %x 无符号整数(十六进制)
# %X 无符号整数(十六进制大写字符)
# %e 浮点数字(科学计数法)
# %E 浮点数字(科学计数法,用E代替e)
# %f 浮点数字(用小数点符号)
# %g 浮点数字(根据值的大小采用%e或%f)
# %G 浮点数字(类似于%g)
# %p 指针(用十六进制打印值的内存地址)
# %n 存储输出字符的数量放进参数列表的下一个变量中
nHex = 0x20
print 'nHex = %x, nDec = %d, nOct = %o' %(nHex, nHex, nHex)
# nHex = 20, nDec = 32, nOct = 40
输出二进制的话,可以使用python函数bin()
# Python 2.7.10 (default, Feb 7 2017, 00:08:15)
# [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
# Type "help", "copyright", "credits" or "license" for more information.
# >>> bin(789)
# '0b1100010101'
# >>>
3.格式化输出浮点数(float)
%字符:标记转换说明符的开始
最小字段宽度:转换后的字符串至少应该具有该值指定的宽度。如果是*,则宽度会从值元组中读出
转换标志:-表示左对齐;+表示在转换值之前要加上正负号;''(空白字符)表示正数之前保留空格;0表示转换值若位数不够则用0填充
点(.)后跟精度值:如果转换的是实数,精度值就表示出现在小数点后的位数。如果转换的是字符串,那么该数字就表示最大字段宽度。如果是*,那么精度将从元组中读出
import math
#default
print 'PI = %f' % math.pi
# PI = 3.141593
# width = 10, precise = 3, align = left
print 'PI = %10.3fxxx' % math.pi
# PI = 3.142xxx
# width = 10, precise = 3, align = right
print 'PI = %-10.3fxxx' % math.pi
# PI = 3.142 xxx
# 前面填充字符串
print 'PI = %06d' % int(math.pi)
# PI = 000003
4.格式化输出字符串(string)
# precise = 3
print '%.3s' % ('jcodeer')
# jco
# precise = 4
print '%.*s' % (4,'jcodeer')
# jcod
# width = 10, precise = 3
print 'xx%10.3s' % ('jcodeer')
# xx jco
5.输出列表(list)
l = [1, 2, 3, 'jcodeer']
print l
# [1, 2, 3, 'jcodeer']
6.输出字典(dictionary)
d = {1: 'A',2: 'B',3: 'C',4: 'D'}
print d
# {1: 'A', 2: 'B', 3: 'C', 4: 'D'}
7.python print 自动换行
# print会在行末加上回车,如果不需要,只需在print语句结尾添加一个逗号','
for i in range(0,5):
print i,
# 0 1 2 3 4
或者直接使用下面的函数进行输出:
import sys
sys.stdout.write("输出的字符串")
8.万能的 %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."
)
# 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.'
9.矩阵输出
import numpy as np
a = np.array([[1,2],[3,4]])
b = np.array([[5,6],[7,8]])
print a
# [[1 2]
# [3 4]]
print b
# [[5 6]
# [7 8]]
print a, b
# [[1 2]
# [3 4]] [[5 6]
# [7 8]]
Python3中的print用法
在Python3 中print 是一个函数,通过格式化函数format()来控制输出格式
1. 通过位置标号
# {0}表示第一个元素, {1}表示第二个元素, {2}表示第三个元素,以此类推。。。
a = 'Ace'
b = 'hello'
print("{1}, my name is {0}".format(a, b))
# hello, my name is Ace
2. 通过关键词参数
name = "Ace"
age = 26
print("{myname}'s age is {myage}".format(myname=name, myage=age))
# Ace's age is 26
3. 通过属性和下标
person = ["Ace", 26]
print("{0[0]}'s age is {0[1]}".format(person))
# Ace's age is 26
print("{people[0]}'s age is {people[1]}".format(people=person))
# Ace's age is 26
字典字符串不需要加引号
person = {'Ace': 26}
print("{myname}'s age is {people[Ace]}".format(myname=name,people=person))
# Ace's age is 26
4. 格式化限定符
{0:0.3f} {1:3d} 在序号后面加上格式符就可以了,不用加%
5.填充与对齐
^,<,>分别代表居住,左对齐,右对齐,后面带宽度
a = 123.456789
haha = 'haha!!!'
print("{0:0.3f}, *{1:<14}*".format(a, haha))
print("{0:0.3f}, *{1:>14}*".format(a, haha))
print("{0:0.3f}, *{1:^14}*".format(a, haha))
print("{0:0.3f}, *{1:}*".format(a, haha))
# 123.457, *haha!!! *
# 123.457, * haha!!!*
# 123.457, * haha!!! *
# 123.457, *haha!!!*
python中print的用法_Python2与Python3中print用法总结相关推荐
- print在python2和python3的区别_Python2和Python3中print的不同点
在Python2和Python3中都提供print()方法来打印信息,但两个版本间的print稍微有差异 主要体现在以下几个方面: 1.python3中print是一个内置函数,有多个参数,而pyth ...
- python怎么横着输出_对python3中, print横向输出的方法详解
对python3中, print横向输出的方法详解 Python 2 : print打印的时候,如果结尾有逗号,打出来时候不会换行.但是在python3里面就不行了. Python3: 3.0的pri ...
- python3除法运算_Python2和Python3中除法操作/的不同
X/Y 在3.0版本之前的Python中 >>>1/2 0 即一个整数(无小数部分的数)被另外一个整数除,计算结果的小数部分被截除了,只留下了整数部分 有时候,这个功能比较有用,譬如 ...
- python多个strip_python 格式化多个Python3中strip()、lstrip()、rstrip()用法详解
Python中有三个去除头尾字符.空白符的函数,它们依次为: strip: 用来去除头尾字符.空白符(包括n.r.t.' ',即:换行.回车.制表符.空格) lstrip:用来去除开头字符.空白符(包 ...
- python双除_python2和python3 中的除法的区别
1. python2中有/和//的除法区别: 其中/的用法: >>>1/2 0 即一个整数(无小数部分的数)被另外一个整数除,计算结果的小数部分被截除了,只留下了整数部分 有时候,这 ...
- python垃圾回收 循环引用_在做 Python 循环引用垃圾回收实验中的一个小问题, Python3 的 print 是线程安全的吗?...
以前都听说 Python 循环引用会导致引用数无法清零,所以不能垃圾回收,会内存泄漏,需要删除引用关系或者用 gc.collect()才能进行正常垃圾回收. 但做了下实验,好像还是会自动回收循环引用的 ...
- python格式化字符串语法_详解Python3 中的字符串格式化语法
一.旧式的字符串格式化 % 操作符 参考以下示例: >>> name = "Eric" >>> "Hello, %s." % ...
- python打开并读取csv文件_!python3中使用使用read_csv( )读取csv文件,文件路径中含有中文,无法读取怎么处理?...
python3如何根据csv文件的列的内容,自动建数据库表 你好,csv格式的和excel格式是差不多的, 下面是读取excel的一些函数,希望帮到你: # -*- coding: cp936 -*- ...
- python2和python3的默认编码_Python2和Python3中的字符串编码问题解决
原博文 2018-01-02 11:38 − Python2和Python3在字符串编码上是有明显的区别. 在Python2中,字符串无法完全地支持国际字符集和Unicode编码.为了解决这种限制,P ...
最新文章
- 安装和配置以下合同 14.1管理
- 处理南通一客户:Fortigate 310B Firmware丢失
- HDFS【2.5.1】系列1:HDFS的核心数据结构---元数据
- C语言 | 用51单片机实现公历与农历星期的转换(完整源代码)
- HTML里Dom onload和jQuery document ready这两个事件的区别
- 七代处理器装win7_为啥新电脑没法装windows7系统?解决方法总会有的
- MySQL数据库的远程连接配置
- VS2015 vs2017 密钥
- sql 2000 数据库置疑
- matlab 平滑曲线连接_MATLAB画图
- 泪目!这篇博士论文致谢走红:感谢博一与我结婚的妻子...
- MyDiskTest硬盘检测绿色版
- html多个背景音乐自动播放,多个背景音乐同时自动播放?
- 计算机视觉笔记及资料整理(含图像分割、目标检测)
- 云服务器只能显示控制台吗,云服务器控制台使用方法
- 删除oracle所有表外键,Oracle中查询、禁用、起用、删除表外键
- python爬虫招聘网站(智联)
- win7各种插件的下载与使用
- 云米携手coKiing发布高端AI变频空调,提前卡位5G智能家居市场
- 良心安利服装类3d打印模型素材网站
热门文章
- 《深入理解Java虚拟机》笔记6——高效并发
- 《深入理解Java虚拟机》笔记5——类加载机制与字节码执行引擎
- 【C#学习】delegate(委托) 和 event(事件)
- JAVA程序员看C#的精华与糟粕
- Android Studio 单刷《第一行代码》系列 02 —— 日志工具 LogCat
- EasyMock 使用方法与原理剖析
- 论文Very Deep Convolutional Networks for Large-Scale Image Recognition
- 深度学习Deep learning From Image to Sequence
- 对话机器学习大神Yoshua Bengio(上)
- Ubuntu启用休眠hibernate