The "print" statement (and related material) -------------------------------------------- In class 2, we spent a lot of time using the "print" statement. I wanted to fill in the details on "print" in case people are confused or wondering. The basic form of print is print something where something is an item or expression of _any_ data type (most often a string, but potentially anything). Some data types don't make sense to print directly (we'll see examples when we get to object-oriented programming, where a single variable can represent a _huge_ collection of data and state information), but most that we've encountered can be printed out by the print statement in a straightforward way. So for example print "hello" # a string print 7.5 # a number print ["E", "F", "F"] # a list of strings print [2, 4, 6, 8] # a list of numbers print [[], "", [], "", ""] # a list of empty strings and empty lists print "That's funny." # a string again print len("Shikaripura") # the result of a function, in this case # the len function, whose results ("return # type") are numbers print raw_input() # the result of a function, in this case # the raw_input function, whose results # are strings print 3.14159*(r*r) # the result of a calculation, which will # be performed by Python and then printed # out (assuming that some number has been # assigned to the variable r) Many other examples are possible, and we'll see more examples as we learn more features of Python. The print statement also has a feature where it can print out _more than one item at once_ on a single line; to use this, you separate the items to be printed with commas: print "I", "am", "an", "electronic", "computer" print "I'm", "a", "Python", "interpreter!" print 3,1,4,1,5,9,2,6,5,3,5 print "I'm",22,"years","old." print "I'm", age, "years", "old." # if the variable age has a value You can mix types arbitrarily here (so you can print strings and numbers and lists and anything else you can think of on the same line). Note that the print statement will insert spaces between the items which are printed out this way! There is no way to prevent the spaces from being added; one space is added for each comma. Note that print "Hi","there,","how are you?" print "Hi", "there,", "how are you?" print "Hi" , "there," , "how are you?" all do exactly the same thing. (How come?) On the other hand, print "Hi","there,","how are you?" print "Hi","there, ","how are you?" are different. Each print statement outputs an entire complete line and then goes on to the next line for subsequent outputs. (For programmers: this means that print outputs a newline after whatever it prints.) If you want to print a blank line by itself, you can just say print and a line will be skipped. print is equivalent to print "" because it outputs nothing (the empty string) and then goes to the next line. This can be inconvenient if it's not what you intended; for example, print "I am" print age print "years old." results in output like I am 54 years old. instead of I am 54 years old. One of the ways to deal with this is to use string concatenation, which is accomplished in Python with the "+" operator: string1 + string2 equals a new string whose contents are the contents of string1 followed by the contents of string2. You can use this inside a print statement print "I'm " + "glad to see you." print "This" + " " + "is" + "fun." print "Me" + "ow!" or anywhere else in Python (because it's not specific to print statements, but is a general feature of the language). So for example, you could write something like statement = "I am" statement = statement + " " statement = statement + str(age) statement = statement + " " statement = statement + "years old." print statement to build up a string out of smaller pieces and then print it out. (Please be sure that you understand how this works; we'll be using code like this frequently!) Or weird_string = "ergro" prefix = "und" place = prefix + weird_string + prefix print "It's buried " + place We could also have said print "It's buried", place to get the same effect. We spent a fair amount of time in class observing that print "I have", 2, "eyes" works but print "I have " + 2 + "eyes" doesn't. Numbers can't be concatenated with strings. On the other hand, there is a function called str which turns a number into its (base 10) string representation. So str(2) is the same as "2". (More usefully, str(x) is the same as whatever number x equals, but represented as a string instead of a number.) What if you want to go the other way? We did a brief example along these lines. Remember that raw_input(), which reads a line of input from the user and returns that line as a string, _always returns a string_. (That's actually why it's called "raw".) So sometimes this is good: print "What is your name?" name = raw_input() name_length = len(name) print "Your name is " + name + " and has " + str(name_length) + " letters." But sometimes it's annoying: this_year = 2002 print "How old are you?" age = raw_input() print "You were probably born around " + str(this_year-age) Oops! That doesn't work! How come? That's right, we have to do this_year = 2002 print "How old are you?" age = int(raw_input()) print "You were probably born around " + str(this_year-age) or this_year = 2002 print "How old are you?" age = raw_input() print "You were probably born around " + str(this_year-int(age)) in order to be able to do arithmetic on the age (because it has to be a number type in order to do arithmetic on it). Does all this make sense? If not, please experiment and then ask me questions about it. One last thing about print: if you wanted to have several successive print statements give output all on one line, you can put a comma at the _end_ of the print statement to indicate that the next print statement should output on the same line. print 2, print 4, print 6, print 8, print 10 Note that you still get a space in between items (but now the items can be printed with subsequent statements). Of course, you can do other things in between: x = 2 print x, x = x * 2 print x, x = x * 2 print x, x = x * 2 print x, x = x * 2 print x, x = x * 2 print x, x = x * 2 print x, x = x * 2 print x, x = x * 2 print x This sure seems repetitive; when we're all done with sequence types, we'll talk about more concise ways to write programs which repeat the same action over and over (looping, or iteration).