What does end = in python?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    By default Python‘s print() function ends with a newline. A programmer with C/C++ background may wonder how to print without a newline. Python’s print() function comes with a parameter called ‘end‘. By default, the value of this parameter is ‘\n’, i.e. the new line character. 

    Example 1:

    Here, we can end a print statement with any character/string using this parameter. 

    Python3

    print("Welcome to", end = ' ')

    print("GeeksforGeeks", end= ' ')

    Output:

    Welcome to GeeksforGeeks

    Example 2: 

    One more program to demonstrate the working of the end parameter. 

    Python3

    print("Python", end='@')

    print("GeeksforGeeks")

    Output:

    Python@GeeksforGeeks

    Example 3:

    The print() function uses the sep parameter to separate the arguments and ends after the last argument.

    Python3

    print('G','F', sep='', end='')

    print('G')

    print('09','12','2016', sep='-', end='\n')

    print('Red','Green','Blue', sep=',', end='@')

    print('geeksforgeeks')

    Output

    GFG
    09-12-2016
    Red,Green,Blue@geeksforgeeks

    So, I'm struggling trying to understand this kinda simple exercise

    def a(n):
        for i in range(n):
            for j in range(n):
                if i == 0 or i == n-1 or j == 0 or j == n-1:
                    print('*',end='')
                else:
                    print(' ',end='')
            print()
    

    which prints an empty square. I tought I could use the code

                print("*", ''*(n-2),"*")
    

    to print the units in between the upper and the lower side of the square but they won't be aligned to the upper/lower side ones, which doesn't happen if you run the first code... so... could this be because of end='' or print() (would you be so kind and tell me what do they mean?)?

    asked Dec 4, 2013 at 10:13

    3

    Check the reference page of print. By default there is a newline character appended to the item being printed (end='\n'), and end='' is used to make it printed on the same line.

    And print() prints an empty newline, which is necessary to keep on printing on the next line.

    EDITED: added an example.
    Actually you could also use this:

    def a(n):
        print('*' * n)
        for i in range(n - 2):
            print('*' + ' ' * (n - 2) + '*')
        if n > 1:
            print('*' * n) 
    

    answered Dec 4, 2013 at 10:20

    starrifystarrify

    13.9k4 gold badges33 silver badges49 bronze badges

    2

    In Python 3.x, the end=' ' is used to place a space after the displayed string instead of a newline.

    please refer this for a further explanation.

    answered Dec 4, 2013 at 10:25

    What does end = in python?

    Nilani AlgiriyageNilani Algiriyage

    29.6k31 gold badges82 silver badges119 bronze badges

    1

    spam = ['apples', 'bananas', 'tofu', 'cats']
        i = 0
        for i in range(len (spam)):
            if i == len(spam) -1:
                print ('and', spam[i])
            elif i == len (spam) -2:
                print (spam [i], end=' ')
            else:
                print (spam [i], end=', ')
    

    So I'm new to this whole coding thing, but I came up with this code. It's probably not as sophisticated as the other stuff, but it does the job.

    spam = ['apples', 'bananas', 'tofu', 'cats']    
    def fruits():
        i = 0
        while i != len(spam):
            if len(spam) != i :
                print ('and', spam[i])
                i += 1   
     fruits()  
    

    try this!

    answered Sep 27, 2017 at 14:52

    print() uses some separator when it has more than one parameter. In your code you have 3 ("" is first, ''(n-2) - second, "*" -third). If you don't want to use separator between them add sep='' as key-word parameter.

    print("*", ' '*(n-2), "*", sep='') 
    

    answered Dec 4, 2013 at 10:25

    Andrey ShokhinAndrey Shokhin

    10.3k1 gold badge17 silver badges15 bronze badges

    use this to understand

    for i in range(0,52):
        print(5*"fiof" ,end=" ")
    

    just put different things here in end and also use with sep

    What does end = in python?

    Suraj Rao

    29.1k11 gold badges95 silver badges100 bronze badges

    answered Sep 21, 2021 at 10:19

    What does end = in python?

    print('\n'.join('*{}*'.format((' ' if 0

    answered Dec 4, 2013 at 10:32

    volcanovolcano

    3,53020 silver badges28 bronze badges

    1

    What does end () do in Python?

    Python print end keyword The end key of print function will set the string that needs to be appended when printing is done. By default the end key is set by newline character. So after finishing printing all the variables, a newline character is appended.

    What does end \r do in Python?

    Put the \r at the beginning or end of your printed string, e.g. '\rthis string will start at the beginning of the line' . or 'the next string will start at the beginning of the line\r' . Conceptually, \r moves the cursor to the beginning of the line and then keeps outputting characters as normal.

    What does end == mean in Python?

    In Python 3. x, the end=' ' is used to place a space after the displayed string instead of a newline.

    What is the purpose of end?

    Ans. 4 The purpose of end is to append a string at the end of a print(). Appending a string to another string can also be accomplished by the “+” operator which is also known as the concatenation operator.