一般来说,从C/C++切换到Python的人都想知道如何打印两个或多个变量或语句,而不用进入Python的新行。因为python print()函数默认以换行符结尾。Python有一个预定义的格式,如果使用print(一个_变量),那么它将 自动转到下一行。
null
例如:
Python3
print ( "geeks" ) print ( "geeksforgeeks" ) |
这将导致:
geeksgeeksforgeeks
但有时我们可能不想进入下一行,而是想在同一行上打印。那么我们能做什么呢?
例如:
Input : print("geeks") print("geeksforgeeks")Output : geeks geeksforgeeksInput : a = [1, 2, 3, 4]Output : 1 2 3 4
这里讨论的解决方案完全依赖于您使用的python版本。
在Python 2中不使用换行符打印。十、
python
# Python 2 code for printing # on the same line printing # geeks and geeksforgeeks # in the same line print ( "geeks" ), print ( "geeksforgeeks" ) # array a = [ 1 , 2 , 3 , 4 ] # printing a element in same # line for i in range ( 4 ): print (a[i]), |
输出:
geeks geeksforgeeks1 2 3 4
在Python 3中不使用换行符打印。十、
Python3
# Python 3 code for printing # on the same line printing # geeks and geeksforgeeks # in the same line print ( "geeks" , end = " " ) print ( "geeksforgeeks" ) # array a = [ 1 , 2 , 3 , 4 ] # printing a element in same # line for i in range ( 4 ): print (a[i], end = " " ) |
输出:
geeks geeksforgeeks1 2 3 4
在Python 3中不使用换行符打印。不使用for循环的x
Python3
# Print without newline in Python 3.x without using for loop l = [ 1 , 2 , 3 , 4 , 5 , 6 ] # using * symbol prints the list # elements in a single line print ( * l) #This code is contributed by anuragsingh1022 |
输出:
1 2 3 4 5 6
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END