Learn Python The Hard Way learning (5)-more variables and Printing

Source: Internet
Author: User

Now we enter more variables and print them. We usually use "" to refer to strings.

Strings are quite convenient. In practice, we will learn how to create strings containing variables. There is a special way to insert a variable into a string, which is equivalent to telling Python: "Hey, this is a formatted string. Put the variable here ."

Enter the following program:

1. # -- coding: UTF-8 --
2. my_name = 'zed A. shares'
3. my_age = 35 # no lie
4. my_height = 74 # inch
5. my_weight = 180 # lb
6. my_eyes = 'blue'
7. my_teeth = 'white'
8. my_hair = 'brown'
9.
10.
11. print "let's talk about % s." % my_name
12. print "He's % d inches tall." % my_height
13. print "He's % d pounds heavy." % my_weight
14. print "Actually that's not too heavy ."
15. print "He's got % s eyes and % s hair." % (my_eyes, my_hair)
16. print "His teeth are usually % s depending on the coffee." % my_teeth
17.
18.
19. # The following line is complex. Try to write it.
20. print "If I add % d, % d, and % d I get % d." % (
21. my_age, my_height, my_weight, my_age + my_height + my_weight)

Tip: If the encoding is incorrect, enter the first sentence.

Running result:
Root @ he-desktop :~ /Mystuff # 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.
Root @ he-desktop :~ /Mystuff #

Extra score exercises:
1. Remove my _ in front of all variable names.

2. Try other format characters. % r is very useful, like saying, "Everything can be printed."

3. Search for all python format characters.
% D format integer
% I formatted integer
% U format the unsigned integer (disused, not supported)
% O Format the unsigned octal number
% X format the unsigned hexadecimal number (lowercase letter)
% X format the unsigned hexadecimal number (uppercase letters)
% E format the floating point number using scientific notation
% E serves the same purpose as % e
% F format the floating point number. You can specify the precision after the decimal point. The default value is 6 decimal places. For example, %. 2f shows 2 decimal places.
% F is the same as % f
% G determines whether to use % f or % e Based on the value size.
% G is the same as % g
% C format characters and ASCII code;
% S formatted string www.2cto.com
% R large string

4. Use mathematical calculations to convert inches and LBS into centimeters and kilograms.
1 inch = 2.54 cm, 1 lb = 0.4536 kg
1. my_height_centimeter = my_height * 2.54
2. my_weight_kilo = my_weight * 0.4536
3. print "He's % d centimeters tall." % my_height_centimeter
4. print "He's % d kilos heavy." % my_weight_kilo
 

Author: lixiang0522

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.