How to print without newline in Python?

Posted in /  

How to print without newline in Python?
vinaykhatri

Vinay Khatri
Last updated on October 31, 2024

    Every high-level programming language support printing the text or other data values on the console using some methods and statement. In Python, we use the print() method to display text or other data on the console window. By default, every print() method print the statement on a new line, but this behavior of the print statement can be changed by specifying the end parameter.

    When we program in Python, sometimes we want to print the different values on the same line. For example, we may want to print the user’s name and age on the same line using two different print statements. But how can we print or display two different print statements on a single line in Python? Here in this tutorial, we have mentioned different techniques on how to print statements in Python without a newline.

    Python print() method

    The Python print() method is used to display text data on the console window, and this method is often used to create console based application using Python.

    Python print() syntax

    print( *values, end)

    The print() method can accept two parameters the *values and the end. The *values signifies that the print statement can accept multiple comma-separated values. The end is an optional parameter and it specifies the end value for the printed values. By default, the value of the end parameter is '\n' which represents a new line.

    print() method Example without specifying the end parameter.

    val1= 12
    val2 = "Hello"
    val3= [1,2,3,4]
    val4 = lambda x : x+2
    print(val1)
    print(val2)
    print(val3)
    print(val4)

    Output

    12
    Hello
    [1, 2, 3, 4]
    <function <lambda> at 0x0382ADF0>

    In the above example, for all the print() methods we did not mention the end parameter, so by default the end parameter value becomes '\n' and every print statement printed on a new line. now let’s specify the end parameter to '\n' value and see what happen

    val1= 12
    val2 = "Hello"
    val3= [1,2,3,4]
    val4 = lambda x : x+2
    print(val1, end ='\n' )
    print(val2 ,end = '\n' )
    print(val3, end ='\n' )
    print(val4, end ='\n' )

    Output

    12
    Hello
    [1, 2, 3, 4]
    <function <lambda> at 0x03E0ADF0>

    Here you can see that after mentioning the end parameter to '\n' nothing changed in the format of the printed values, which specifies the end has a default value of '\n' .

    How to print without a newline in Python?

    By far we have discussed that the print() method accepts two parameters the *values and end, and by default the value of end is '\n'. If we specify the end parameter to black space like end = " " then the print statement value will end with the blank space, not newline '\n'. If the python print() statement will end with a blank space then the next print statement will start from the same line where the last statement was ended.

    print statement without printing a new line.

    val1= 12
    val2 = "Hello"
    val3= [1,2,3,4]
    val4 = lambda x : x+2
    print(val1, end =' ' )
    print(val2 ,end = ' ' )
    print(val3, end =' ' )
    print(val4, end =' ' )

    Output

    12 Hello [1, 2, 3, 4] <function <lambda> at 0x033AADF0>

    We can set the end parameter to any character value and every printed value will end with that character.

    Example

    let’s print different values and end them with a @ symbol.

    val1= 12
    val2 = "Hello"
    val3= [1,2,3,4]
    val4 = lambda x : x+2
    print(val1, end ='@ ' )
    print(val2 ,end = '@ ' )
    print(val3, end =' @' )
    print(val4, end =' @' )

    Output

    12@ Hello@ [1, 2, 3, 4] @<function <lambda> at 0x038DADF0> @

    Python print() *values parameter

    Using the single print() statement we can print more than one value on a single line. The *values parameter of the print() method can accept more than value. So instead of using the end parameter we should mention all the values in the print() statement and separate them with comma.

    Example

    val1= 12
    val2 = "Hello"
    val3= [1,2,3,4]
    val4 = lambda x : x+2
    
    print(val1, val2, val3, val4)

    Output

    12 Hello [1, 2, 3, 4] <function <lambda> at 0x03A4ADF0>

    Conclusion

    In this Python tutorial, we learned how to print values without printing a new line. The python print() method is used to print text and other data values on the window console, and by default, it prints every statement on a new line. But by specifying the end parameter to a blank space we can print two different print() statements in a single line without print a new line.

    People are also reading:

    Leave a Comment on this Post

    0 Comments