python2和python3的区别 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!!!*
python2和python3的区别 print-Python2与Python3中print用法总结相关推荐
- python中print输出格式汇总_python中print输出格式有哪些
python中print输出格式有:1.可用加号连接[print("I " + "love " + "you")]:2.逗号连接会自动在连接 ...
- python2和python3的不同点_Python2和Python3的区别,新手学习Python应该如何选择
Python 2在2020年元旦将正式停止官方支持,同时也有越来越多的 python 库 不再支持 python 2. 所以小编建议新手刚开始学习Python使用Python3版本 所以我们就来看看 ...
- python3.6字典有序_为什么Python3.6字典变得有序了?
原博文 2019-12-25 16:09 − 其实 在你看了笔者的文章之前,或许想过这样一个问题, 为什么列表是有顺序的呢?而字典不是? 来看一下在内存中是怎样存储的就知道了: 列表的存储是顺序存储, ...
- python中print是什么意思_python中print什么意思
python中print什么意思,多个,是一个,对象,语法,默认值 python中print什么意思 易采站长站,站长之家为您整理了python中print什么意思的相关内容. python中prin ...
- python语言中print函数的作用_python中print()方法有什么
python中print()方法有什么 发布时间:2020-11-11 13:50:08 来源:亿速云 阅读:91 作者:小新 这篇文章给大家分享的是有关python中print()方法有什么的内容. ...
- print在python2和python3的区别_Python2和Python3中print的不同点
在Python2和Python3中都提供print()方法来打印信息,但两个版本间的print稍微有差异 主要体现在以下几个方面: 1.python3中print是一个内置函数,有多个参数,而pyth ...
- python2 与 python3的区别总结
python2 与 python3的区别总结 几乎所有的Python 2程序都需要一些修改才能正常地运行在Python 3的环境下.为了简化这个转换过程,Python 3自带了一个叫做2to3的实用脚 ...
- python2好还是python3好-总结对比Python2和Python3之间的区别
首先要说的是,Python的版本,目前主要分为两大类: Python 2.x的版本的,被称为Python2:是目前用的最广泛的,比如Python 2.7.3. Python 3.x的版本的,被称为Py ...
- Python2与Python3的区别:
前言:目前python两个版本Python2与Python3同时存在,并且这两个版本都在维护更新,但是Python2应该在2020年就停止维护更新了,那么到底选择Python2还是Python3,这个 ...
最新文章
- linux centos7 设置开机 进入命令行 不进入图形界面
- LeetCode算法题2:求字符串b在字符串a中的起始下标
- 汇编语言--CPU对外设的控制
- 找到一个不错的ASP.net电子图书下载网站
- c语言fgets函数的用法
- 条款12:复制对象时勿忘其每一个部分
- YAML,另一种标记语言?不止是标记语言!
- memcache面试
- Python爬虫实战之解密HTML
- Spring Boot从Controller层进行单元测试
- jQuery ajax post put 请求
- 论文笔记_S2D.06-2018-BMVC-用于实时语义分割的轻量级精细网络RefineNet
- 让Apache Shiro保护你的应用[转]
- Delphi10.4.1开发Linux应用视频重播
- 中国渔船数量不断下降,海洋渔船下降速度较慢「图」
- python打包时出现RecursionError: maximum recursion depth exceeded的解决方法
- 重新认识Git——抽丝剥茧说Git
- 【excel vba】拆分表格
- Allegro PCB -通孔焊盘制作 及Flash制作
- uboot的移植——移植uboot官方的uboot到x210开发板
热门文章
- activiti流程信号捕获事件触发signalEventReceived
- 中科方德桌面操作系统_兆芯CPU与中科方德新版桌面/服务器操作系统完成互认证...
- python切片迭代_Python高级特性 切片 迭代解析
- 在html中标记bdo,HTML_HTML非常用标签 optgroup、sub、sup和bdo示例代码,optgroup 用在select 标记中 可以 - phpStudy...
- 问答 | 为什么两轮差速机器人转向运动是圆周运动
- 变量案例弹出用户名(JS)
- 盒子横向排列-初识浮动Float(HTML、CSS)
- Unity3D--枚举+协程控制定点移动
- visual怎么设置默认运行_神马?游戏和软件不能运行?来3DM一下吧!
- Shell脚本学习-阶段二十八-shell练习二