Print in Python
Prints the values to a stream, or to sys.stdout by default.
Syntax is :
Help on built-in function print in module built-ins:
print(…)
print(value, …, sep=’ ‘, end=’\n’, file=sys.stdout)
Examples
I will show a various example to demonstrate the usage of print statement.
- Simple example
print("hello world from Python")
2. Example of Print using variables
sum is a variable declared with a value of 200$. It can be passed into print and the output gets printed as Total = 200$
sum = "200$"
print("Total = ",sum)
#output: Total = 200$
3. Example of print with separator and end
I have added a variable called sum and print is called by passing this variable and a separator is used before the variable is printed. The default end is a new line but here we do not go next line but instead stay on the same line. If there is another print statement then it will be printed on the same line.
sum = 200
print("Total = ",sum, sep="$", end ='')
#output: Total = $200
4. Example for Print using file IO
inFile = open('helloPrint.txt', 'w')
print('I fully understand how print works now.Yay!!!', file = inFile)
inFile.close()
If helloPrint.txt is not present it is created by the file open() call and appends the text mentioned to the file. file close() is used to close the filehandle.
Very nice 👍🏻
Thanks. Happy learning.