using print() function in python

problem

You’re learning Python and you wish to see how to use the print() function so you can print/ show some data in the program’s output (console).

EXAMPLE 1

Let’s see some examples of print()

print_examples.py
				
					print('Hello') # or "Hello" will give the same output
				
			
running

Just type the above function in your Python editor, in my case I use the terminal as follows:

EXAMPLE 2

print_examples.py
				
					print(100) #prints the given number
				
			

EXAMPLE 3

print_examples.py
				
					print('Hello', 100) #prints 2 parameters
				
			

EXAMPLE 4

print_examples.py
				
					numbers = [1,6,199,-3]
print(100,'Hello', numbers) # prints the 3 given parameteres
				
			

When we put multiple parameters to the print() and separate them by comma, the output will have an empty space character between each parameter as shown above. Also the parameters can be of any type like this example it prints an integer then a string and then a list.

EXAMPLE 5

print_examples.py
				
					    message = "XXXX-XXXX-XXXX"
    print(message)
    print(message)
				
			

So when we have muptiple print() statements, it’s printing the one after the other separated by a new line. But what if we want to change that and separate the 2 print outputs by a tab?

EXAMPLE 6

print_examples.py
				
					    message = "XXXX-XXXX-XXXX"
    print(message, end="\t") #prints a tab at the end
    print(message) #prints a new line ('\n') by default
				
			

As you can see above, by specifying the end=”\t” parameter we change how it behaves. Inside end any value can be added and used to finish the print statement.

conclusion

In this post we saw examples of using the built-in print() statement of Python. Also, how the print() ends with a new line by default and how we can change that by supplying the end parameter and a value.

Share it!

Facebook
Twitter
LinkedIn
Reddit
Picture of Ellion

Ellion

Professional IT consultant, writer, programmer enthusiast interested in all sorts of coding.
Eats all cookies 🍪

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Strictly Necessary Cookies

Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.

Google Analytics Cookies

This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.

Keeping this cookie enabled helps us to improve our website.

HotJar Cookies

We use Hotjar in order to better understand our users’ needs and to optimize this service and experience. Hotjar is a technology service that helps us better understand our users’ experience (e.g. how much time they spend on which pages, which links they choose to click, what users do and don’t like, etc.) and this enables us to build and maintain our service with user feedback. Hotjar uses cookies and other technologies to collect data on our users’ behavior and their devices. This includes a device's IP address (processed during your session and stored in a de-identified form), device screen size, device type (unique device identifiers), browser information, geographic location (country only), and the preferred language used to display our website. Hotjar stores this information on our behalf in a pseudonymized user profile. Hotjar is contractually forbidden to sell any of the data collected on our behalf.

For further details, please see the ‘about Hotjar’ section of Hotjar’s support site.