The Print Function
In Python 3 and higher, print is a function rather than a keyword.
out: hello world!
foo = 1
bar = 'bar'
baz = 3.14
out: 1 print(bar)
out: bar print(baz)
You can also pass a number of parameters to print:
print(foo, bar, baz)
out: 1 bar 3.14
Another way to print multiple parameters is by using a +
print(str(foo) + " " + bar + " " + str(baz))
out: 1 bar 3.14
What you should be careful about when using + to print multiple parameters, though, is that the type of the parameters should be the same. Trying to print the above example without the cast to string first would result in an error, because it would try to add the number 1 to the string “bar” and add that to the number 3.14.
type:int str float print(foo + bar + baz)
will result in an error
This is because the content of print will be evaluated first:
print(4 + 5)
print("4" + "5")
print( + )
out: [4, 5]
Otherwise, using a + can be very helpful for a user to read output of variables In the example below the output is very easy to read!
The script below demonstrates this
telling python to include a function to create random numbers randnum = random.randint(0, 12)
make a random number between 0 and 12 and assign it to a variable print(“The randomly generated number was – ” + str(randnum))
You can prevent the print function from automatically printing a newline by using the end parameter:
print("this has no newline at the end of it… ", end="")
out: this has no newline at the end of it… see?
If you want to write to a file, you can pass it as the parameter file:
with open('my_file.txt', 'w+') as my_file:
print("this goes to the file!", file=my_file)
this goes to the file!
You can do more than just print text. print also has several parameters to help you.
Argument sep: place a string between arguments.
Do you need to print a list of words separated by a comma or some other string?
print('apples','bananas', 'cherries', sep=', ') apple, bananas, cherries
print('apple','banana', 'cherries', sep=', ') apple, banana, cherries
Argument end: use something other than a newline at the end
Without the end argument, all print() functions write a line and then go to the beginning of the next line. You can change it to do nothing (use an empty string of ”), or double spacing between paragraphs by using two newlines.
Argument file: send output to someplace other than sys.stdout.
Now you can send your text to either stdout, a file, or StringIO and not care which you are given. If it quacks like a file, it works like a file.
def sendit(out, values, sep=' ', end='\n'): …print(values, sep=sep, end=end, file=out)
sendit(sys.stdout, 'apples', 'bananas', 'cherries', sep='\t')
with open("delete-me.txt", "w+") as f:
… sendit(f, 'apples', 'bananas', 'cherries', sep=' ', end='\n')
GoalKicker.com – Python® Notes for Professionals 371
with open("delete-me.txt", "rt") as f:
apples bananas cherries
There is a fourth parameter flush which will forcibly flush the stream.
Must Read Python Interview Questions
200+ Python Tutorials With Coding Examples
Other Python Tutorials
- What is Python?
- Python Advantages
- Python For Beginners
- Python For Machine Learning
- Machine Learning For Beginners
- 130+ Python Projects With Source Code On GitHub