user9941223
user9941223

Reputation:

Why does it make difference when I use "end" or "\n" or " " (space) statement?

Why does it make difference when I use one upload only one of these three statements:

  1. print("*", end=" ")
  2. print("* ")
  3. print("*\n") Collage Pic of Outputs, because i can upload only one image

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

Answers (2)

Mayank.MYK
Mayank.MYK

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

user9145571
user9145571

Reputation:

  1. print("*", end=" ") means: print a star and a space at the end
  2. print("* ") means: print a star, a space and a newline at the end (default)
  3. print("*\n") means: print a star, a newline and another newline at the end (default)

More information: Python end parameter in print.

Upvotes: 1

Related Questions