python培训之print如何不换行

发布时间:2022-09-02 09:45:59 人气:156 作者:多测师

  大家应该知道python中print之后是默认换行的,

  那如何我们不想换行,且不想讲输出内容用一个print函数输出时,就需要改变print默认换行的属性,

  方法如下:

  print('contents', end='!@#$%^&*')

  end就表示print将如何结束,默认为end="\n"(换行)

  栗子:

  print("祝各位身体健康")

  print("!")

  print("祝各位身体健康", end=' ')

  print("!")

python培训之print如何不换行

  python3.0 的print 函数有如下的形式:

  print([object,...][,seq=' '][,end='\n'][,file=sys.stdout])

  我们在使用print()函数时,并不希望输出结束后自动换行,因此,我们可以按照下面的方法来做

  1.print()指定结束符

  print('hello',end='')

  print('world')

  #result:helloworld

  当print()函数,指定end参数为空字符后,print()函数就不再主动添加换行符了。并且,hello和world之间也不存在任何空格。

  a = 'first line'

  b = 'second line'

  c = 'third line'

  print(a,end='\n\n')

  print(b)

  print(c,end='!')

  我们可以利用指定结束符的方法,灵活控制换行行数和结尾字符。

  以上内容为大家介绍了python培训之print如何不换行,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注多测师。https://www.e70w.com/xwzx/



返回列表
在线客服
联系方式

热线电话

17727591462

上班时间

周一到周五

二维码
线