How to Use Python Print Function
Python print function is used to display the output on the screen. In this article, we will learn how to use python print function using different examples.
If you are writing any program or code, and once you are done with it, then you want to display your output on the screen. In python, we have a print function that prints the specified message on the screen or any other standard device. The displayed message can be a string or object (the object, if it is not a string, it will be converted into a string before being displayed on the screen).
Syntax
print(object(s), sep = seprator, end = end, file = file, flush = flush)
Parameters
- objects: The value you want to get printed. It may be more than one.
- sep:
- It is an optional value that is used to separate one object from the other.
- Default Value: โ โ โ a space
- end:
- It is also an optional value that is used to specify what to print at the end.
- Default Value: โ โ
- file:
- An optional value that must be an object with a write method.
- Default Value: sys.stdout โ system standard output
- sys.stdout is used to print the objects on the screen.
- flush:
- It is an optional value that takes Boolean parameters indicating whether the output is flushed (True) or buffered (False)
- Flush means whether the print call will immediately take effect.
- Default Value: flush = False
Return Value
The print function has no return value, i.e.
Return Type: None
Till now, we have gained enough theoretical knowledge of print function in python. Now itโs time to learn how to use python print().
So, letโs start with printing โHello World!!, We are Learning Python.โ
#print 'Hello World!!, We are learning Python'
print('Hello World!!, We are learning Python')
Output
How print() works with string literals?
String literals contain textual data in the form of characters enclosed within quotes. These characters are enclosed in double quotation marks (โ โ).
Example 1: Print the value stored in a variable
# Define the Variable
company_name = "InfoEdge"
#printprint(company_name)
Output
As we mentioned in syntax, print command can take more than one objects. Multiple objects in print() can be stored by using commas in between different objects.
Example 2: Pass two objects as different arguments in print().
# print Hello World!! and I'm working in InfoEdge are passed as two different arguents
print("Hello World!!", "I'm working in InfoEdge")
Output
Programming Online Courses and Certification | Python Online Courses and Certifications |
Data Science Online Courses and Certifications | Machine Learning Online Courses and Certifications |
Example 3: Pass string literals and variables in print().
# pass string literals and variable in print()#string literal: I'm working in#variable: InfoEdge
# Define the Variable
company_name = "InfoEdge"
#print
print("I'm working in", company_name)
Output
We can get the above output using concatenation. Letโs see the below example.
Example 4: Python Concatenation
# Python Concat
# Define the Variable
company_name = "InfoEdge"
#print
print("I'm working in" + company_name)
Output
Must Read: Getting Started with Python String
Must Read: String Formatting in Python
Recursion Function in Python | count() Function in Python |
len() Function in Python | float() Function in Python |
range() Function in Python | lambda() Function in Python |
If you see example 2, where we have passed two objects as a different argument, in print(), then we get the below:
Here, in the output both the argument (Hello World!! and Iโm working in InfoEdge) are not separated. So, how can we separate them.
Letโs see the example below.
How to separate parameter in python print() function?
Example 5: Separate both the arguments of example 2 by a comma.
# use sep parameter in print() function
print("Hello World!!", "I'm working in InfoEdge", sep = ' , ')
Output
How to use end parameter in print()?
Example 6:
# use end parameter in print()
print("Hello World", end = '!!', sep = ' ')
Output
Example 7:
# use end parameter in print()a = "Vikram"b = "Shubham"print(a, end = ' \n\n\n ')print(b)
Output
Difference Between print() and return() in Python
- print() is a function, that we call. It shows the human user a string representing what is going on inside the computer.
- print statement is used when you want to show the value to the user.
- return() is a keyword, that stops the execution of the current function, sending a value out to where the function was called.
- return() changes the flow of the program, while print() doesnโt.
In simple terms, printing means showing a value in the console (or to the users), whereas return means giving back a value from a function.
Letโs have an example, where we will use both return() and print.
def square(x): return x*x
def g(y): return y + 3
def h(y): return square(y) + 3
print(g(h(2)))
Output
Conclusion
In this article, we have discussed how to use print() function in python, with the help of examples.
Hope, you will like the article.
Keep Learning!!
Keep Sharing!!
Top Trending Article
Top Online Python Compiler | How to Check if a Python String is Palindrome | Feature Selection Technique | Conditional Statement in Python | How to Find Armstrong Number in Python | Data Types in Python | How to Find Second Occurrence of Sub-String in Python String | For Loop in Python |Prime Number | Inheritance in Python | Validating Password using Python Regex | Python List |Market Basket Analysis in Python | Python Dictionary | Python While Loop | Python Split Function | Rock Paper Scissor Game in Python | Python String | How to Generate Random Number in Python | Python Program to Check Leap Year | Slicing in Python
Interview Questions
Data Science Interview Questions | Machine Learning Interview Questions | Statistics Interview Question | Coding Interview Questions | SQL Interview Questions | SQL Query Interview Questions | Data Engineering Interview Questions | Data Structure Interview Questions | Database Interview Questions | Data Modeling Interview Questions | Deep Learning Interview Questions |