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