Exercise 5: More variables and printing | Stupid way to learn Python

Source: Internet
Author: User

First, brief

"Format string"-each time you use "or" to refer to some text, you create a string. A string is the way the program presents information to people.

Second, the Code

1 #!usr/bin/env python2 #-*-coding:utf-8-*-3 4 #Author:xixihuang5 #Date:2016/08/31 09:52 AM6 #Desc: Exercise 5: More variables and printing7 #type more variables and print them out. This time we will use something called "format string". 8 #every time you use "To reference some text, you create a string." A string is the way the program presents information to people. 9 Ten # One # A # -  -My_name ='Zed A. Shaw' theMy_age = 35#Not a Lie -My_height = 74#inches -My_weight = 180#lbs -My_eyes ='Blue' +My_teeth =' White' -My_hair ='Brown' +  A Print "Let's talk about %s."%My_name at 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 is usually%s depending on the coffee."%My_teeth -  in #This is tricky, try-to-get it exactly right - Print "If I add%d,%d, and%d I get%d."% ( toMy_age, My_height, my_weight, my_age + my_height + my_weight)

Third, the output results

$ python ex5.pylet ' s talk about Zed A. Shaw. He'sinches tall. He'spounds heavy. Actually that's nottoo heavy. He's got Blue eyes and Brown hair.  289. $

Iv. Extra points Exercises

1. Modify all the variable names to remove the "my_" in front of them. Make sure you get rid of every place, not just where you've been assigned the value of ' = '.

Replace all shortcut keys: Ctrl + R

2. Try to use more formatting characters. For example, %r is a very useful one, and it means "print out whatever it is".

Formatting character Descriptions

%d signed integer (decimal);

%s calls the STR function to print the string, and the STR function returns the original string;

%r calls the Rper function to print the string, and the string returned by the REPR function is added as an escape sequence, which is the form of a direct writing string;

3. Search all the Python formatted characters on the Web.

The string formatting operator (%) applies only to string types, very similar to the string formatting of the printf () function in C, and even uses the same symbols, with percent sign (%), and supports all printf () format operations.

The syntax is as follows:
Format_String% String_to_convert
Where: format_string is the format tag string, in the form of "%cdoe", String_to_convert is the string to be formatted, if it is more than two, you need to enclose in parentheses.

Python String Formatting symbols:

Format description
Percent percentile sign #就是输出一个%
%c character and its ASCII code
%s string
%d signed integer (decimal)
%u unsigned integer (decimal)
%o unsigned integer (octal)
%x unsigned integer (hexadecimal)
%x unsigned integer (16 uppercase characters)
%e floating-point numbers (scientific notation)
%e floating point number (scientific notation, E instead of e)
%f floating point number (with decimal point symbol)
%g floating-point numbers (%e or%f depending on the size of the value)
%g floating-point number (similar to%g)
%p pointer (memory address with hexadecimal print value)
%n the number of stored output characters into the next variable in the parameter list

The% format character can also be used in dictionaries, and the output can be formatted by using% (name) to refer to the elements in the dictionary.

The minus sign indicates that the number should be left-aligned, and "0" tells Python to fill the number with a leading 0, plus the number always displays its positive and negative (+,-) symbol, even if the number is positive.


You can specify the minimum field width, for example: "%5d"% 2. A period character can also be used to specify additional precision, such as "%.3d"% 3.

e.g.
# example: Numeric formatting
nyear = 2018
Nmonth = 8
Nday = 18
# formatted date%02d number to two-bit integer vacancy fill 0
print '%04d-%02d-%02d '% (nyear,nmonth,nday)
>> 2018-08-18# Output Results

Fvalue = 8.123
print '%06.2f '%fvalue# reserved 2-bit decimal floating-point with a width of 6
>> 008.12# Output

print '%d '%10# output decimal
>> 10
print '%o '%10# output octal
>> 12
print '%02x '%10# output two-bit hex, letter lowercase blank complement 0
>> 0a
print '%04x '%10# output four-bit hex, letter capital Vacancy 0
>> 000A
print '%.2e '%1.2888# output floating-point type with scientific notation reserved 2 decimal places
>> 1.29e+00

Formatting operator Auxiliary directives
Symbolic effect
* Define width or decimal point accuracy
-Used for left alignment
+ Show plus sign (+) in front of positive number
<sp> show spaces in front of positive numbers
# 0 (' 0 ') is displayed in front of the octal number, preceded by ' 0x ' or ' 0X ' in hexadecimal (depending on
Using ' x ' or ' x ')
0 The number shown is preceded by ' 0 ' instead of the default space
% ' percent ' output a single '% '
(VAR) mapping variable (dictionary parameter)
M.N m is the smallest total width displayed, and n is the number of digits after the decimal point (if available)

4. Try using variables to convert inches and pounds into centimeters and kilograms. Do not type the answer directly. Use Python's computational capabilities to do this.

Use Python's computational capabilities to do this.

1 inch (in) = 2.54 cm (cm)

1 lb (lb) = 0.45359237 kg (kg)

1My_height = 74#inches2Transfer_height = 2.54 * My_height#cm3 4My_weight = 180#lbs5Transfer_weight = 0.45359237 * my_weight#kg6 7 Print "He ' s%d innchess tall,%d cm."%(My_height, Transfer_height)8 Print "He ' s%d pounds heavy,%d kg."% (My_weight, transfer_weight)

V. Supplementary

Exercise 5: More variables and printing | Stupid way to learn Python

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.