How to Print in Python

(4 votes, average 4.50 out of 5)

Printing in Python is as simple as typing print followed by the string you wish to print.  You do not need to import any libraries.

1
2
>>> print "Hello World"
Hello World

 

You can also print the value of an integer or list simply by print

1
2
3
4
5
6
>>> x = [1, 2, 3]
>>> print x
[1, 2, 3]
>>> y = 2
>>> print y
2

If, however, you want to print a string AND some integer (or list), you need to cast to string.

1
2
3
>>> x = 42
>>> print "The meaning of life is " + str(x)
The meaning of life is 42


Some notes about print:

Partner Links:
Last Updated on Saturday, 26 June 2010 10:53  
Related Articles

» Customizing File Association in Emacs (based on file extension)

Occassionally it can be useful to have emacs automatically use a particular syntax highlighter based on file extension.  Typically this is useful when you want to use non-stnadard file extensions in your own environment.  You can get it to work by adding the following to your .emacs file (typically located in your home directory):;; Use perl syntax highlighter for .foo files.(setq auto-mode-alist (cons '(".*\.foo" . cperl-mode) auto-mode-alist))The above code would use the cperl...

» Java - Reading and writing a properties file

A properties file in Java can be very useful when you start to notice that there are many locations in your code where you are either hard coding values, many constants, or using enums. Instead of including these values directly in your code, you can refactor them out to a properties file to separate responsibilities. Instead of potentially specifying values throughout many classes or programs, all values can be condensed into one or more properties files as need.An example of a properties file...

» Trouble Booting into Windows

If you cannot boot fully into Windows - either getting stuck at an error message or the computer keeps rebooting, first try starting Windows in Safe Mode by continously pressing the F8 key once the computer turns on until you see a menu which looks like this:Use the arrow keys on the keyboard to select the Safe Mode option and hit the Enter key to start Safe Mode. For additional information and techniques to try on this topic, please refer to the excellent guide posted on Lifehacker: