Python print() Method
The print()
method prints the given object to the console or to the text stream file.
print() syntax:
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
Parameters:
- objects: One or more objects to be printed, seperated by a space ' ' by default.
- Sep: (Optional) If multiple objects passed, they are separated by the specified separator. Default is ' '.
- end: (Optional) The last value to print. Default is '\n'.
- file: (Optional) Must be an object with
write(string)
method. Default is sys.stdout. - flush: (Optional) The stream is forcibly flushed if buffered. Default is False.
Return Value:
No return value.
The following example prints various objects.
print("Learning Python")
name = 'John'
print("My name is",name)
Learning Python
My name is John
We can pass various parameters to alter the output.
name = 'John'
print("My name is",name,sep='---',end = "\n\n\n\n")
print("I am 18 years old")
My name is---John
I am 18 years old
The following prints the object to the file.
printtofile = open('debug.txt', 'w')
print('printing to file', file = printtofile)
printtofile.close()