In the first post on this Python Tutorial, we learnt about Python and the installation process. As we have already set up the dev environment to build and run Python application, let's begin learning how to print something on the output.
This post will guide you in speeding up writing Python scripts by mastering the usages of print() function effectively.
This is second part of the Python Tutorial series for absolute beginners.
At the end of the last post, we wrote a simple program to print "Hello Python" in the output using the Print() function. So, you have already learnt how to print in Python. But that's not the full knowledge.
This simple function can do much more thing than what you thought about. Unless you master the skills, you won't be able to utilize it while writing the Python scripts.
How to print simple string in Python?
When you want to print a simple string, you can pass it as an argument to the print method. You can also send multiple strings as a comma separated value and Python will concatenate them as a single string. Consider the following two examples:
>>> print("Hello Reader")
Hello Reader
>>> print("Hello", "Reader,", "\nWelcome to my blog!")
Hello Reader,
Welcome to my blog!
The first example takes one string as an argument to the print method and outputs it. The second example takes three string arguments and prints them all. To print a new line, we can use the '\n' character.
How to print a string multiple times in Python?
Sometime you want to print a character, a string or a blank line multiple times. Python makes it easy to repetitively print the same string 'n' times specified by you. You need to multiply the string 'n' times. Let's consider the following examples:
>>> print(10 * "a")
aaaaaaaaaa
>>> print(10 * " - ")
- - - - - - - - - -
>>> print("-", 10 * " * " , "-")
- * * * * * * * * * * -
>>> print("First line", 2 * "\n", "Fourth line")
First line
Fourth line
In the first example, it prints the character 'a' 10 times. In the second example, it prints the string " - " 10 times. In the third example, it first writes " - ", prints " * " 10 times, then prints " - " again. The fourth example first prints the string "First line", then prints blank line twice and then the string "Fourth line".
How to print blank lines in Python?
There are number of ways to print blank lines in Python. Check the following examples, where all the three cases the print method will output a blank line on the screen.:
>>> print()
>>> print("")
>>> print("\n")
How to print multiple strings in Python with a separator?
When you want to separate multiple strings passed to the print method with a separator, you can use the named argument "sep=separator_string". Consider the following example to understand it better:
>>> print("1", "2", "3", "4", "5", sep=" - ")
1 - 2 - 3 - 4 - 5
How to prevent line breaks in Python print statement?
By default the print method adds a line break at the end, which is similar to Console.WriteLine method in C#. But in Python, you can ask the print method to end with a different string. You need to pass the named argument "end=end_string" to the method. Consider the following examples:
>>> print("Kunal"); print("Chowdhury");
Kunal
Chowdhury
>>> print("username", end="@"); print("domain", end=".com");
username@domain.com
The first example demonstrates the default functionality where each call to the print method ends with a new line. The second example takes the named argument end and thus you will see the entire output in a single line, appending the string that you passed to end. In case you don't want to add a string, you can set blank space to it.
How to print a collection in Python?
If you have a collection, be it a set, list, array or tuple, you can easily print it to console. Just pass the collection variable to the print method, and Python will print it to the output in proper format. Let's consider the following three examples to understand it better:
>>> set = {"Kunal", "Rajat", "Amitabha"}
>>> print(set)
{'Amitabha', 'Rajat', 'Kunal'}
>>> list = ["Kunal", "Rajat", "Amitabha"]
>>> print(list)
['Amitabha', 'Rajat', 'Kunal']
>>> tuple = ("Kunal", "Rajat", "Amitabha")
>>> print(tuple)
('Amitabha', 'Rajat', 'Kunal')
Here, the first example is a set, the second one is a list/array and the third one is a tuple. When you print them, it prints in respective format.