Variables And Printing in Python

By: Zed A. Shaw Emailed: 1765 times Printed: 2513 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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 files, 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.” As usual, just type this in even if you do not understand it and make it exactly the same.

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)

What You Should See

$ 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.
$


python Home | All python Tutorials | Latest python Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Zed A. Shaw
Formatted printing in Python
Printing in Python
Strings And Text in Python
Variables And Printing in Python
Variables And Names in Python
Numbers And Math in Python
Comments And Pound Characters in Python
Your First Program in Python
Installing gedit for python programming in Linux
Installing gedit for python programming in Mac OS X

More Tutorials in python
What is the need for Python language?
How to compile python script and create .pyc file?
How to find the current module name in python
How to force rereading of a changed module in python
UnboundLocalError in python
call by reference in python
Callable objects in python
Ternary operator in python
Hexadecimal and Octal integers in python
Convert string to number in python
Convert number to string in python
Perl's chomp() equivalent for removing trailing newlines from strings in python
Convert between tuples and lists in python
Iterate over a sequence in reverse order in python
Remove duplicates from a list in python

More Latest News
Most Viewed Articles (in python )
Variables And Printing in Python
Strings And Text in Python
Using Unicode Literals in Python
Unknown command: 'migrate'
Convert number to string in python
Perl's chomp() equivalent for removing trailing newlines from strings in python
Garbage Collection in Python
Installing gedit for python programming in Windows
Installing gedit for python programming in Linux
Your First Program in Python
Comments And Pound Characters in Python
Numbers And Math in Python
Variables And Names in Python
Printing in Python
Formatted printing in Python
Most Emailed Articles (in python)
Convert number to string in python
How to force rereading of a changed module in python
Hexadecimal and Octal integers in python
Formatted printing in Python
Ternary operator in python
Convert string to number in python
Remove duplicates from a list in python
Your First Program in Python
Numbers And Math in Python
Variables And Names in Python
error: â_mysql_ConnectionObjectâ has no member named
call by reference in python
Perl's chomp() equivalent for removing trailing newlines from strings in python
Iterate over a sequence in reverse order in python
Schwartzian Transform in python