码迷,mamicode.com
首页 > 编程语言 > 详细

Python 让两个print()函数的输出打印在一行内

时间:2018-01-26 10:59:28      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:blog   内容   span   height   class   乘法口诀   bubuko   orm   利用   

1.两个连续的print()函数为什么在输出时内容会分行显示?

解:print()中有两个默认参数 sep 和 end,其中sep是代替分隔符,end是代替末尾的换行符,默认使用‘,’代替空格,且默认末尾加上换行符,end函数用来定义一行输出的末尾。

1 coffee_cup = coffee  
2 print("I love my", coffee_cup, "!",sep="*")  
3 """ 
4 输出结果是: 
5 I love my*coffee*! 
6 """

 

2.如何让两个print()函数打印在一行内

 解:可以利用end参数,将默认的换行符改为空格或是空白即可

1 print(hello, end = " ")  
2 print(world, end = "*")  
3 print(!)  
4 """ 
5 输出结果是: 
6 hello world*! 
7 """ 

 

 

3.示例:打印9*9 乘法表:

#输出 9*9 乘法口诀表。

for i in range(1,10):
    for j in range(1,i+1):
        print("{}*{}={}".format(i, j, i * j) , end=" ")
    print()

技术分享图片

 

Python 让两个print()函数的输出打印在一行内

标签:blog   内容   span   height   class   乘法口诀   bubuko   orm   利用   

原文地址:https://www.cnblogs.com/bahcelor/p/8358163.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!