python里print怎么格式化列表 python中print format的用法_浮点数


学习任何一门编程语言都离不开print的用法,本文分成几个部分来介绍print的用法,让大家在看完之后,掌握其用法。

第一部分:官网解释
第二部分:格式符%的用法
第三部分:格式化字符串 f
第四部分:转义字符的用法
第五部分:补充(313)

文前的话:如果英语好的话,实际上编程学习真的是可以去看人家的官网,这是最快、最直接、最有效的方法。

一、官网解释

print(*objects,sep=' ',end='n',file=sys.stdout,flush=False)

先看官网给的解释。

参数说明

  • objects -- 复数,说明可以一次输出多个对象。输出多个对象时,需要用 , 分隔。
  • sep -- 用来间隔多个对象,默认值是一个空格。
  • end -- 用来设定以什么结尾。默认值是换行符 n,我们可以换成其他字符串。(所以几乎每个输出,都差不多都会换行)
  • file -- 要写入的文件对象。
  • flush -- 输出是否被缓存通常决定于 file,但如果 flush 关键字参数为 True,流会被强制刷新。

Print objects to the text stream file, separated by sep and followed by end. sep, end, file and flush, if present, must be given as keyword arguments(关键性参数).


print(9)
name="wan"
print(name)
a=123
print(a)


9
wan
123


>>> print("www","runoob","com",sep=".")  # 设置输出对象中的间隔符
www.runoob.com


每一次输出后,都是换行。因为默认的都是换行


>>>for i in range(0,6):
...     print(i) #换行
... 
0
1
2
3
4
5
>>> for i in range(0,6):
...     print(i, end=" ")#不换行,每个数据中间加个空格
... 
0 1 2 3 4 5


All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values. If no objects are given, print() will just write end.

print() 表示,不打印任何东西,只是一个空行

二、格式符%


print ("His name is %s"%("Aviad"))
print ("He is %d years old"%(25))
print ("His height is %.2f m"%(1.83234))


His name is Aviad
He is 25 years old
His height is 1.83 m

可以看到这个格式:“内容格式”%(内容),%s、%d、%f,分别表示内容的数值格式,为字符串、整数、浮点字符。前面只是一个例子,表示操作符的作用,真正输出肯定如下


print ("His name is Aviad)
print ("He is 25 years old)
print ("His height is 1.83m)


以乘法口诀的输出为例


print('%d*%d=%d'%(j,i,j*i),end="t")


格式为:%d%d=%d,公式整数的乘法表达式,

内容为:%(j,i,ji),三个整数分别是j i j*i。


#用字典来传递数据
print ("I'm %(c)s. I have %(l)d yuan." % {'c':'hungry','l':22})
#这里面就可先定义 list=[]的字典,然后 print中引用list
调试输出:
I'm hungry. I have 22 yuan.


%s 字符串 (采用str()的显示);%r 字符串 (采用repr()的显示)

%c 单个字符

%b 二进制整数;%d 十进制整数;%i 十进制整数;%o 八进制整数;%x 十六进制整数

%e 指数 (基底写为e);%E 指数 (基底写为E)

%f 浮点数;%F 浮点数(%.2f 2指保持浮点数小数点后两位)

%g 指数(e)或浮点数 (根据显示长度);%G 指数(E)或浮点数 (根据显示长度)

三、格式化字符串 f


lst=[1,'xiaoming',29.5,'123456644']
for _ in lst:
    print(f'{_}的类型为{type(_)}')


1的类型为<class 'int'>
xiaoming的类型为<class 'str'>
29.5的类型为<class 'float'>
123456644的类型为<class 'str'>

print(f'{_}的类型为{type(_)}')

格式为:print(f'{}{}')。

f,表示对字符串格式化,‘ ’中为相应的内容。

{ }里面为替换字段,文中的替换字段为变量‘_’,它是指列表lst中的内容。

四、转义字符()

n表示换行,t表示制表符,r表示回车,f表示换页


>>> print('I'm ok.') #字符串内部既包含'又包含"怎么办?可以用转义字符来标识
I'm ok.
>>> print('I'm learningnPython.')
I'm learning
Python.
>>> print('n')


>>> print('t')
       
>>> print(r't') #允许用r''表示''内部的字符串默认不转义
t


python里print怎么格式化列表 python中print format的用法_字符串_02


第五部分:补充

print+format的组合


print("{} {}".format("hello", "world")) # 不设置指定位置,按默认顺序
'hello world'
 
print("{0} {1}".format("hello", "world"))  # 设置指定位置
'hello world'
 
print("{1} {0} {1}".format("hello", "world") ) # 设置指定位置
'world hello world'

print('{} a word she can get what she {} for.'.format('With','came'))
print('{preposition} a word she can get what she {verb} for'.format(preposition = 'With',verb = 'came')) 
print('{0} a word she can get what she {1} for.'.format('With','came'))
With a word she can get what she came for.