Reputation:
Why does it make difference when I use one upload only one of these three statements:
print("*", end=" ")
print("* ")
print("*\n")
I am getting the pyramid as shown in https://www.geeksforgeeks.org/programs-printing-pyramid-patterns-python/, only when i use "end" in print("*", end=" ")
.
The code is:
# Function to demonstrate printing pattern
def pypart(n):
# outer loop to handle number of rows
# n in this case
for i in range(0, n):
# inner loop to handle number of columns
# values changing acc. to outer loop
for j in range(0, i + 1):
# printing stars
print("*", end=" ")
# ending line after each row
print("\r")
# Driver Code
pypart(3)
Why does we need this line: print("\r")
?
I didn't get it from comment above that line.
Upvotes: 0
Views: 1049
Reputation: 1
1) print("*", end=" ") means:print a star and a space at the end of the line
2) print("* ") means:print a star with a gap
3) print("*\n") means:print a star & move the cursor to the new line
Upvotes: 0
Reputation:
print("*", end=" ")
means: print a star and a space at the endprint("* ")
means: print a star, a space and a newline at the end
(default)print("*\n")
means: print a star, a newline and another
newline at the end (default)More information: Python end parameter in print
.
Upvotes: 1