1. Use placeholders to reserve locations for real values and specify the format
Print ("I ' m%s.") I ' m%d year old "% (' AAA ', 18))
We can also use dictionaries to pass real values. As follows:
Common format characters
%s: string
%d: decimal integer
%b: Binary integer
%x: Hexadecimal integer
%e: Index base is E
%E: Base is E
%f: Floating point
2. Use Format ()
Reference http://www.cnblogs.com/hongten/archive/2013/07/27/hongten_python_format.html
Use the ' {} ' placeholder
Print (' I\ ' m {},{} '. Format (' Hongten ', ' Welcome to my space! ')
You can also use ' {0} ', a placeholder in the form of ' {1} '
Print (' {0},i\ ' m {1},my e-mail is {2} '. Format (' Hello ', ' hongten ', ' [email protected] ')
You can change the position of the placeholder
Print (' {1},i\ ' m {0},my e-mail is {2} '. Format (' Hongten ', ' Hello ', ' [email protected] ')
Placeholder using the form ' {name} '
Print (' Hi,{name},{message} '. Format (name = ' Tom ', message = ' How old is You? '))
Mixed with ' {0} ', ' {name} ' form
Print (' {0},i\ ' m {1},{message} '. Format (' Hello ', ' hongten ', message = ' This is a test message! ')
Python full stack _python string formatting