Variable and Printing in Python

Variable and Printing in Python

Now we’ll do even more typing of variables and printing them out. This time we’ll use something called a “format string.” Every time you put ” (double- quotes) around a piece of text, you have been making a string. A string is how you make something that your program might give to a human. You print them, save them to fi les, send them to web servers, all sorts of things.

Strings are really handy, so in this exercise you will learn how to make strings that have variables embedded in them. You embed variables inside a string by using specialized format sequences and then putting the variables at the end with a special syntax that tells Python, “Hey, this is a format string, put these variables in there.”

1 my_name = 'Zed A. Shaw'
2 my_age = 35# not a lie
3 my_height = 74# inches
4 my_weight = 180# lbs
5 my_eyes = 'Blue'
6 my_teeth = 'White'
7 my_hair = 'Brown'
8
9  print "Let's talk about %s." % my_name
10 print "He's %d inches tall." % my_height
11 print "He's %d pounds heavy." % my_weight
12 print "Actually that's not too heavy."
13 print "He's got %s eyes and %s hair." % (my_eyes, my_hair)
14 print "His teeth are usually %s depending on the coffee." % my_teeth
15
16# this line is tricky,
try to get it exactly right
17 print "If I add %d, %d, and %d I get %d." % (
18 my_age, my_height, my_weight, my_age + my_height + my_weight)

Output

 Let's talk about Zed A. Shaw.
 He's 74 inches tall.
 He's 180 pounds heavy.
 Actually that's not too heavy.
 He's got Blue eyes and Brown hair.
 His teeth are usually White depending on the coffee.
 If I add 35, 74, and 180 I get 289.

Example

1 print "Mary had a little lamb."
2 print "Its fleece was white as %s." % 'snow'
3 print "And everywhere that Mary went."
4 print "." * 10 # what'd that do?
5
6 end1 = "C"
7 end2 = "h"
8 end3 = "e"
9 end4 = "e"
10 end5 = "s"
11 end6 = "e"
12 end7 = "B"
13 end8 = "u"
14 end9 = "r"
15 end10 = "g"
16 end11 = "e"
17 end12 = "r"
18
19 # watch that comma at the end. try removing it to see what happens
20 print end1 + end2 + end3 + end4 + end5 + end6,
21 print end7 + end8 + end9 + end10 + end11 + end12

Output

Mary had a little lamb.
 Its fleece was white as snow.
 And everywhere that Mary went.
 ..........
 Cheese Burger

Study Drills

1. Change all the variables so there isn’t the my_ in front. Make sure you change the name everywhere, not just where you used = to set them.

2. Try more format characters. %r is a very useful one. It’s like saying “print this no matter what.”

3. Search online for all the Python format characters.

4. Try to write some variables that convert the inches and pounds to centimeters and kilos. Do not just type in the measurements. Work out the math in Python.

Common Student Questions

Can I make a variable like this: 1 = ‘Zed Shaw’?

No, the 1 is not a valid variable name. They need to start with a character, so a1 would work, but 1 will not.

What does %s, %r, and %d do again?

You’ll learn more about this as you continue, but they are “formatters.” They tell Python to take the variable on the right and put it in to replace the %s with its value.

How can I round a floating point number?

You can use the round() function like this: round(1.7333).

Why does this not make sense to me?

Try making the numbers in this script your measurements. It’s weird, but talking about yourself will make it seem more real.