python中print输出形式有哪些
更新:HHH   时间:2023-1-7


这篇文章将为大家详细讲解有关python中print输出形式有哪些,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

1、普通输出,Print函数接受零或多个参数,并将单个空格作为默认分隔符显示结果。为输出Python程序的值提供了简单的方法,实际参数sep的设置可以改变分隔符。另外,每次打印都默认以换行符结束。这种行为可以通过设置实际参数end来改变。

print('hello')
#结果:hello
print('hello','world')
#结果:hello world
print('hello','world',sep='***')#sep指定特定的分隔符
#结果:hello***world
print('hello','world',end='****')#end 设置结尾参数
#结果:hello world****

2、格式化输出,格式字符串可以包含一个或多个转换声明。转换字符告诉格式化运算符,什么类型的值会插入字符串的相应位置。可在%和格式化字符之间添加格式化修改符。

name="apple"
price=6
print("the %s costs %d cent" %(name,price))
#结果:the apple costs 6 cent
 
print("The %+10s costs %5.2f cents" % (name,price))#+代表右对齐,10代表占用10个字符
#结果:The      apple costs  6.00 cents
print("The %+10s costs %10.2f cents" % (name,price))#10.2代表占用10个字符,并保留2位小数。
#结果:The      apple costs       6.00 cents
itemdict = {"item":"banana","cost":24}#使用字典传递参数
print("The %(item)s costs %(cost)7.1f cents" % itemdict)
#结果:The banana costs    24.0 cents

关于“python中print输出形式有哪些”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

返回编程语言教程...