Today look at learning%d,%s,%f and other usages. %d is a placeholder integer, and%s is a placeholder, and%f is a floating-point number. The following example of study is said to enter the name, age, job, salary. And give a calculation of how long it will take to retire at the age of 65. The focus is on using placeholders for print (msg). Additional spare is attached to the back.
#__author__: "Gabriel Jason"#date:2017-11-05name= Input ("Name:") Age= Int (Input ("Age :")) Job= Input ("Job:") Salary= Input ("Salary:")ifSalary.isdigit ():#Is it a number? Is there a non-number? Salary =Int (Salary)#Else:#print ("must input digit")#exit () #退出程序Print(name,name,age,job,salary)#%d digit integer;%f floating-point number, approximately equal to a decimal. msg =" "---------Info of%s-----Name:%s Age:%djob:%ssalary:%f You'll be retired in%s years---------end------" "% (name,name,age,job,salary,65-Age )Print(msg)
================================
Here is the dump material:
#####################
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)
Transfer from http://blog.csdn.net/huangfu77/article/details/54807835
"Python learning" today look at learning%d,%s,%f and other uses, the following example is said to enter the name, age, job, salary. And give a calculation of how long it will take to retire at the age of 65