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

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()

 

posted @ 2018-01-26 09:50  dear_diary  阅读(2894)  评论(0编辑  收藏  举报