Introduction to Print Statement in Python
The print() function prints the given object to the standard output device (screen) or to the text stream file. Remember the time when you wrote your first program? Yes! I am talking about the “Hello World” program which is probably the first program which anyone learns in their life. That one line of the program has a print statement which we use daily in our programming without even knowing all the intricacies of it. The purpose of the print() statement is to print the given object to the standard output device or to the text stream file.
Syntax:
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
As we have seen the basic syntax of a print function,
Parameters of Print Statement in Python
let us discuss its parameters in details:
- objects: This signifies the object to be printed, * indicates that there can be more than one object.
- sep: This signifies the separation which would be between the different objects. By default, this value is ‘‘.
- end: The value for this parameter is printed at the last.
- file: It should be an object with a write(str) method. If this value is not mentioned, it prints objects on the standard output device i.e. screen.
- flush: The stream gets forcibly flushed if this value is True. By default, this value is False.
Examples of Print Statement in Python
Let us see some examples to fully understand print functionality.
1. Calling Print Function
To call the print function, we just need to write print followed by the parenthesis (). It tells Python that we are actually calling the function and not referring it by its name. Just calling print() would produce an invisible newline character. But using Python String literal ‘\n’ is a better option of printing a new line.
2. Newlines
Code:
print("Hello")
print("\n")
print("World")
Output:
- For Multiple Newlines:
Code:
print("Hello")
print(5*"\n")
print("World")
Output:
3. Sending Multiple Objects with Sep and End Parameters
Code:
print('Hello','World',sep=' ',end='!!!')
Output:
Here multiple objects are passed within the print function. The “sep” parameter has a value “ ” i.e space so there is space between the different objects and the end parameter has a value “!!!” so the string ends with “!!!”.
4. Using String Concatenation
Code:
string1 = "Hi "
string2 = "Hello "
string3 = "World"
string4 = "!!!"
print(string1+string2+string3+string4)
Output:
5. String Concatenation of Different Datatype
Code:
import datetime
now = datetime.datetime.now()
string1 = "Current time is "
print(type(now))
print(type(string1))
print(string1+now)
Output:
Here when we are concatenating objects of different data types then it is necessary to perform typecasting. Here the object now is of type datetime. Datetime and the object string is of type str hence the error. To fix this we need to perform type casting like following.
4.8 (7,888 ratings)
View Course
Code:
import datetime
now = datetime.datetime.now()
string1 = "Current time is "
print(type(now))
print(type(string1))
print(string1+str(now))
Output:
6. Using f-strings for String Concatenation
Code:
import datetime
now = datetime.datetime.now()
print(f'Current time is {now}')
Output:
f-strings helps us to avoid a common mistake of forgetting to typecast concatenated operands. It was introduced in Python version 3.6
7. Using Positional Arguments
Code:
import datetime
now = datetime.datetime.now()
print('Current time is',now)
Output:
Thus we see that using positional arguments and by the use of “,” we can concatenate objects without having to perform explicit typecasting or using f-strings.
8. Using File Parameter
Code:
srcFile = open('python.txt', 'w')
print(‘This file is about Python', file = sourceFile)
sourceFile.close()
Output:
Here the file parameter contains the file in which the text is to be written.
9. Printing None Constant
Code:
print(None)
Output:
The None constant despite being used to indicate an absence of a value, it will show up as None as the output as shown above rather than an empty string.
10. Printing User Custom Objects
Code:
class Person:
def __init__(self, name, age, gender):
self.name, self.age, self.gender = name, age, gender
jdoe = Person('Steve Smith', 35,'male')
print(jdoe)
Output:
Here we created a class named Person having attributes like name, age, and gender. We create an object of that class and we can print that object as mentioned above.
Recommended Articles
This is a guide to Print Statement in Python. Here we discuss the Introduction and a few examples of Print Statement in Python along with basic syntax and its parameters. You may also look at the following articles to learn more –