{article Learn Python The Hard Way}{text}{/article}

More Variables And Printing

When I google search I found this about %

Old string formatting

The % operator can also be used for string formatting. It interprets the left argument much like a sprintf()-style format string to be applied to the right argument, and returns the string resulting from this formatting operation.

String Formatting Operations

String and Unicode objects have one unique built-in operation: the % operator (modulo). This is also known as the string formatting or interpolation operator. Given format % values (where format is a string or Unicode object), % conversion specifications in format are replaced with zero or more elements of values. The effect is similar to the using sprintf() in the C language. If format is a Unicode object, or if any of the objects being converted using the %s conversion are Unicode objects, the result will also be a Unicode object.


ex5.py

my_name = 'Zed A. Shaw'
my_age = 35 # not a lie
my_height = 74 # inches
my_weight = 180 # lbs
my_eyes ='Blue'
my_teeth = 'White'
my_hair = 'Brown'


print "Let's talk about %s." % my_name
print "He's %d inches tall." % my_height
print "He's %d pounds heavy." % my_weight
print "Actually that's not too heavy."
print "He's got %s eyes and %s hair." % (my_eyes, my_hair)
print "His teeth are usually %s depending on the coffee." % my_teeth

#this line is tricky, try to get it exactly right
print "If I add %d, %d, and %d I get %d." % (my_age, my_height, my_weight, my_age + my_height + my_weight)

PS C:> python ex5.py
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.

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.

my_name = 'Zed A. Shaw'
my_age = 35 # not a lie
my_height = 74 # inches
my_weight = 180 # lbs
my_eyes ='Blue'
my_teeth = 'White'
my_hair = 'Brown'

name = my_name
age = my_age
height = my_height
weight = my_weight
eyes = my_eyes
teeth = my_teeth
hair = my_hair

print "Let's talk about %s." % name
print "He's %d inches tall." % height
print "He's %d pounds heavy." % weight
print "Actually that's not too heavy."
print "He's got %s eyes and %s hair." % (eyes, hair)
print "His teeth are usually %s depending on the coffee." % teeth

#this line is tricky, try to get it exactly right
print "If I add %d, %d, and %d I get %d." % (age, height, weight, age + height + weight)

PS C:> python ex5_1.py
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.

2: Try more format characters. %r is a very useful one. It's like saying "print this no matter what".

my_name = 'Zed A. Shaw'
my_age = 35 # not a lie
my_height = 74 # inches
my_weight = 180 # lbs
my_eyes ='Blue'
my_teeth = 'White'
my_hair = 'Brown'



print "Let's talk about %r." % my_name
print "He's %d inches tall." % my_height
print "He's %d pounds heavy." % my_weight
print "Actually that's not too heavy."
print "He's got %s eyes and %s hair." % (my_eyes, my_hair)
print "His teeth are usually %s depending on the coffee." % my_teeth

#this line is tricky, try to get it exactly right
print "If I add %r, %r, and %r I get %r." % (my_age, my_height, my_weight, my_age + my_height + my_weight)


PS C:\mystuff> python ex5q2.py

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.

3: Search online for all of the Python format characters.

Conversion

Meaning

'd'

Signed integer decimal.

'i'

Signed integer decimal.

'o'

Signed octal value.

'u'

Obsolete type – it is identical to 'd'.

'x'

Signed hexadecimal (lowercase).

'X'

Signed hexadecimal (uppercase).

'e'

Floating point exponential format (lowercase).

'E'

Floating point exponential format (uppercase).

'f'

Floating point decimal format.

'F'

Floating point decimal format.

'g'

Floating point format. Uses lowercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise.

'G'

Floating point format. Uses uppercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise.

'c'

Single character (accepts integer or single character string).

'r'

String (converts any Python object using repr()).

's'

String (converts any Python object using str()).

'%'

No argument is converted, results in a '%' character in the result.

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

inches = 1
centimeters = inches * 2.54 # 1 inch = 2.54 centimeter
print "%r inches equals %r centimeters." % (inches, centimeters)


pounds = 1
kilos = pounds * 0.453592 # 1 pounds = 0.453592 kilos
print "%r pounds equals %r kilos." % (pounds, kilos)

PS C:\mystuff> python ex5q4.py
1 inches equals 2.54 centimeters.
1 pounds equals 0.453592 kilos.




inches = 1
centimeters = inches * 2.54 # 1 inches equals 2.54 centimeters.
print "%r inches equals %r centimeters." % (inches, centimeters)


pounds = 1
kilos = pounds * 0.453592 # 1 pounds equals 0.453592 kilos.
print "%r pounds equals %r kilos." % (pounds, kilos)

inches = 12
centimeters = inches * 2.54
print "%r inches equals %r centimeters." % (inches, centimeters)

pounds = 100
kilos = pounds * 0.453592
print "%r pounds equals %r kilos." % (pounds, kilos)

PS C:\mystuff> python ex5q4.py
1 inches equals 2.54 centimeters.
1 pounds equals 0.453592 kilos.
12 inches equals 30.48 centimeters.
100 pounds equals 45.3592 kilos.

{source}
<!-- You can place html anywhere within the source tags -->

<style>

<link href="http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css" rel="stylesheet" type="text/css" />
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js" type="text/javascript"></script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shAutoloader.js" type="text/javascript"></script>

div {width:100%;}

.success {background-color:#E4E9EE;}

</style>


<div class="success" style="width:100%;height:250px;overflow:auto;border:3px solid black; margin:0px">

my_name = 'Zed A. Shaw' </br>
my_age = 35 # not a lie </br>
my_height = 74 # inches </br>
my_weight = 180 # lbs </br>
my_eyes ='Blue' </br>
my_teeth = 'White' </br>
my_hair = 'Brown' </br>
</br></br>
name = my_name </br>
age = my_age </br>
height = my_height </br>
weight = my_weight </br>
eyes = my_eyes </br>
teeth = my_teeth </br>
hair = my_hair </br>
</br></br>
print "Let's talk about %s." % name</br>
print "He's %d inches tall." % height</br>
print "He's %d pounds heavy." % weight</br>
print "Actually that's not too heavy."</br>
print "He's got %s eyes and %s hair." % (eyes, hair)</br>
print "His teeth are usually %s depending on the coffee." % teeth</br>

#this line is tricky, try to get it exactly right</br>
print "If I add %d, %d, and %d I get %d." % (age, height, weight, age + height + weight)</br>

</BR></BR></BR>

*** Python 2.7.6 (default, Nov 10 2013, 19:24:24) [MSC v.1500 64 bit (AMD64)] on win32. ***</BR>
>>> </BR>
Let's talk about Zed A. Shaw.</BR>
He's 74 inches tall.</BR>
He's 180 pounds heavy.
Actually that's not too heavy.</BR>
He's got Blue eyes and Brown hair.</BR>
His teeth are usually White depending on the coffee.</BR>
If I add 35, 74, and 180 I get 289.</BR>
>>> </BR>


</div>

<script language="javascript" type="text/javascript">
    // You can place JavaScript like this

</script>
<?php
    // You can place PHP like this

?>
{/source}