How to print out information is one of the first things you learn as a beginner programmer. This article goes over what you need to know about printing in Python, and we'll look at plenty of code examples along the way. Let's get started! Printing is most likely the first thing you'll learn when you embark on your Python learning journey. It is somewhat of a tradition to write a "Hello World"
program as your first lines of code. And you do that by using the Printing is mostly used for displaying information to the console, whether it's showing a certain message or computational result. But it's also used for debugging purposes. In order to print something to the console in Python 2, all you had to do was use the
This was called a print statement. In Python 3 the print statement was replaced by the If you don't add the set of opening and closing parentheses that follow So in Python 2, the print keyword was a statement, whereas in Python 3 print is a function. The full syntax of the This is what
Let's break it down:
The How to Print Objects in PythonEven if you don't pass in any arguments to This will just output a blank line to the console in such a case. This is best illustrated when using the Python REPL (Read Eval Print Loop). To start a new session, after installing Python on your machine type
It's similar to hitting they How to Print Strings in PythonYou print strings by passing
the string literal as an argument to The output will be the string literal, without the quotes.
If you have a set string or phrase you want to print, you can store it in a variable and pass the variable name as the argument to
It's a best practice to give meaningful names to variables, according to the content stored inside them. This will make code more readable for yourself and anyone else you're working with. As shown in the You can do this by separating each argument with a comma.
There are two arguments: "Hello" and "there!". In the examples below, the arguments are a string literal and a variable.
You can also do this using concatenation with the addition operator:
With concatenation, you have to account for the spaces otherwise you'll end end up with the following:
Keep in mind that you cannot add a string literal with a number. This would result in an error:
As the error suggests, you can only concatenate (add) a string to a string. This means that if you want to include a number, you'd have to convert it to its string equivalent by typecasting.
A modern way to print objects is by using
Strings are not the only objects that can be passed as arguments. In fact, Examples of how to print the rest of Python's built-in data types
How to Change the Way Objects Are Separated in the print() FunctionAs you've seen in the syntax for the By default objects are separated by a single space.
To disable that, you explicitly change the value of For example, objects can be separated by dashes:
Or you could even remove the space by adding an empty string instead:
How to Remove the Default Newline in print()As you saw ealier in the syntax breakdown
of the By default, after each print call, a new line is created. If you call print two times separately, one after the other, you'll see that the second call is displayed on a newline immediately after the first call.
To disable that, you can explicitly change the value of
Now both are on the same line. You can even change it to a full stop:
Anything that isn't the default value will supress the newline that gets created. How to Direct the print() Output to a File in PythonYou'll mostly want to print the output to the standard output, or the command line standard output. There may be times, however, when you'll want to direct that output to an existing file. Say you have a text file and want to add some text using the To open and write to a file in Python, you call the With this mode, each time you run your code the contents of the file will be deleted and replaced by any new text you add. If you don't want to lose any content, you could use the Inside the
ConclusionThanks for reading and making it to the end! I hope you found this tutorial helpful. You now know the basics of how to use the If you are interested in learning more about the Python programming language, check out freeCodeCamp's Python certification to get you started. It covers all the fundamental programming concepts and gradually progresses to more advanced topics. In the end, you'll also build five projects to solidify your learning. Happy coding! Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started |