Skip to content
Home » How to Use the end Parameter in the print Function

How to Use the end Parameter in the print Function

Spread the love

Today we’ll be talking about the print function, and to be precise, we’ll see how to print strings without the newline character at the end, which is possible if you explicitly use the end parameter in the print function.

When you use the print function, a newline is appended to the string:

titles = ['Sponge Girl',
         'Headless Statues',
         'Bill the Catfish',
         'Bang']

for title in titles:
    print(title)

The output:

Sponge Girl
Headless Statues
Bill the Catfish
Bang

So, each string is on a separate line. This is because the print function takes an optional end parameter, which defaults to the newline character. But you can change it to anything else.

The end Parameter

So, as mentioned above the default value assigned to the end parameter is ‘\n’, so the newline character. However, if you explicitly set it to a different string, this string will be used instead of the newline character.

Here are some examples:

1) Put a white space at the end of the string:

titles = ['Sponge Girl',
         'Headless Statues',
         'Bill the Catfish',
         'Bang']

for title in titles:
    print(title, end = " ")  # whitespace added at the end

The output is:

Sponge Girl Headless Statues Bill the Catfish Bang

2) Put an asterisk surrounded by white spaces at the end of the string:

titles = ['Sponge Girl',
         'Headless Statues',
         'Bill the Catfish',
         'Bang']

for title in titles:
    print(title, end = " * ")  

The output is:

Sponge Girl * Headless Statues * Bill the Catfish * Bang *

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

3) Put a tab at the end of the string:

titles = ['Sponge Girl',
         'Headless Statues',
         'Bill the Catfish',
         'Bang']

for title in titles:
    print(title, end = "\t")  # a tab added

The output is:

Sponge Girl     Headless Statues        Bill the Catfish        Bang     

4) Put an arbitrary sequence of characters plus a newline character at the end of the string:

titles = ['Sponge Girl',
         'Headless Statues',
         'Bill the Catfish',
         'Bang']

for title in titles:
    print(title, end = "###\n")  # hash + newline

The output is:

Sponge Girl###
Headless Statues###
Bill the Catfish###
Bang###

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

5) Don’t put anything at the end of the string:

titles = ['Sponge Girl',
         'Headless Statues',
         'Bill the Catfish',
         'Bang']

for title in titles:
    print(title, end = "")  # nothing

The output is:

Sponge GirlHeadless StatuesBill the CatfishBang

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.

6) You can even use a formatted string as the end parameter:

titles = ['Sponge Girl',
         'Headless Statues',
         'Bill the Catfish',
         'Bang']

for title in titles:
    print(title, end = f" -> ({titles.index(title)}) ")  

The output is:

Sponge Girl -> (0) Headless Statues -> (1) Bill the Catfish -> (2) Bang -> (3)

You can experiment yourself, but usually the parameter is set to something simple and we use different ways of formatting text.

Here’s the video version:


Spread the love

Leave a Reply