print() Tips for Python Developers
Welcome! In this article, you will learn helpful tips and tricks that you may not know about the print() function in Python.
Permalink๐น Tip #1: How to Print Without a New Line
In Python, when we call the print()
function multiple times to print multiple values in the terminal or the shell, like in this example:
print("Hello")
print("World")
We will usually see something like this in the output:
Hello
World
Each value is printed on a different line.
This is because the creators of Python decided that, by default, calls to the print()
function should end with a new line character (\n
).
You can see this in the documentation. The default value of the end
parameter is \n
, like you can see here:
print(\objects, sep=' ', end='\n', file=None, flush=False*)
But if you would like to print these values on the same line, you can replace the default new line character by passing a keyword argument to print()
:
print("Hello", end="")
print("World")
Now the output is:
HelloWorld
Why? Because now the end
character is an empty string ""
(the value that we passed for end
).
Similarly, if you would like to separate them with a space, then you would write a string with a space instead:
print("Hello", end=" ")
print("World")
And this would be the output:
Hello World
Permalink๐ธ Tip #2: How to End Print with a Custom Character
Great. Now let's see how you can end each line with a custom character.
The same parameter (end
) can be used to customize the character that is printed at the end of every string. For example, if we would like to print a dash after the print statement, we would write this code:
print("Hello", end="-")
print("World")
And this would be the output:
Hello-World
You can customize this to fit your needs and you can use any character (or sequence of characters) that would be valid in a Python string.
๐ก Tip: If you need to include spaces before or after the character(s), just add them to the string, like this: end=" - "
.
This can be very helpful if you need to print multiple strings using a for
loop:
colors = ["blue", "green", "red", "black"]
for color in colors:
print(color, end=" ")
The output would be:
blue green red black
๐ก Tip: You need to handle what happens with the last character in the output because the end
character will also be added to the end of the sequence. In this case, a space will also be added to the end of the string. One option would be to trim the space with a built-in method, like the .rstrip()
method.
Permalink๐น Tip #3: How to Separate Arguments with a Custom Character
Awesome. print()
can also take multiple arguments and these values will be printed in the output. By default, they will be separated by a space.
Let's see how you can customize the character that separates these arguments.
This is an example with two arguments:
print("Hello", "World")
The output will be:
Hello World
Notice the space. It was added in between the strings by default.
But.... we can change this! ๐
We just need to customize the sep
parameter, like this:
print("Hello", "World", sep=", ")
The output will be:
Hello, World
That is awesome, right? You can pass multiple arguments to print()
and customize how they are separated (or joined, depending on how you think of it ๐).
This will also work with more than two arguments:
print("I", "am", "learning", "Python", sep="-")
The output is:
I-am-learning-Python
๐ก Tip: for this example, we used the "-"
character to separate the arguments, but you can use any character or sequence of characters that you need for a particular goal.
Permalink๐ธ Tip #4: How to Write to a File with print()
And for our last tip, we have a super helpful feature: did you know that you can write a string to a file using print()
? Let's see how you can do this.
You just need to pass an argument for the file
parameter. This value must be a file object. This is an example:
with open("my-file.txt", "w") as my_file:
print("Hello, World", file=my_file)
After running this program, the content of the my-file.txt
file will be:
Hello, World
๐ก Tip: In Python, we usually use a with
statement to work with files because they are safer from bugs. When we use with
, we can only use the file object within the with
statement where it was opened and it is closed automatically when the statement is completed.
The Python documentation also mentions that:
Since printed arguments are converted to text strings,
print()
cannot be used with binary mode file objects. For these, usefile.write(...)
instead.
Permalink๐น Summary
The
print()
function is very powerful and there are many different ways to customize its output.We can customize how the line will end with the
end
parameter.We can also customize the character(s) that will be used to separate the arguments with the
sep
parameter.We can also write a string to a file using
print()
with thefile
parameter.The values of these parameters must be passed as keyword arguments (like this:
param=value
).
๐ป Thank you for reading my article. I hope you liked it and found it helpful.
If you would like to find more coding tutorials, follow me on YouTube (Coding with Estefania) and Twitter (EstefaniaCassN).
Have an awesome day. ๐
Subscribe to our newsletter
Read articles from Coding with Estefania directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Article Series
Python
Python String Indexing - How to Get Individual Characters
Welcome! ๐ In this article, you will learn how to get an individual character from a Python string.โฆ
print() Tips for Python Developers
๐น Tip #1: How to Print Without a New Line In Python, when we call the print() function multiple timโฆ
How to Create a Python Virtual Environment on Windows and macOS
Hi! ๐ If you need to work with Python packages, you should create a virtual environment first. Let'โฆ