'How can I print on a single line inside a nested for loops?
I'm new to Python programming, and this is my first time posting here. I'm trying to make a Python program where I print
Numbers: 0, 1, 2, 3, 4, to the console all on one line with this code.
for i in range(0,1)
print("Numbers: ")
for j in range(0,5)
print(j, end=",")
print()
But I always get
Numbers:
0, 1, 2, 3, 4,
What should I do so the output is like this
Numbers: 0, 1, 2, 3, 4,
Solution 1:[1]
You have to add the "end" on your first print(), like this:
for i in range(0,1)
print("Numbers: ", end=": ")
for j in range(0,5)
print(j, end=",")
print()
Solution 2:[2]
for i in range(0,1):
print("Numbers: ",end='')
#print(*objects, sep=' ', end='\n')
The print() function prints the given object and start pointing to new line , because default value for "end" is '\n' (new line)
for j in range(0,5):
print(j, end=",")
print()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Pedro Muniz |
| Solution 2 | zeeshan12396 |
