In this paper, the Str.format () usage of Python development is analyzed. Share to everyone for your reference, as follows:
Format the output of a string, we can see in many places, such as: C + + has seen
Here's a look at the string Format function Str.format () in Python:
#使用str. Format () function # use ' {} ' placeholder print (' I\ ' m {},{} '. Format (' Hongten ', ' Welcome to my space! ') Print (' # ') #也可以使用 ' {0} ', ' {1} ' placeholder print (' {0},i\ ' m {1},my e-mail is {2} '. Format (' Hello ', ' hongten ', ' Hongtenzone@foxmail.com ') #可以改变占位符的位置print (' {1},i\ ' m {0},my e-mail is {2} '. Format (' Hongten ', ' Hello ', ' Hongtenzone@foxmail.com ') print (' # ') #使用 placeholder print (' Hi,{name},{message} ' in the form of ' {name} '. Format (name = ' Tom ', message = ' How old is? ') Print (' # ') #混合使用 ' {0} ', ' {name} ' form print (' {0},i\ ' m {1},{message} '. Format (' Hello ', ' hongten ', message = ' This is a test message! ')) Print (' # ') #下面进行格式控制import mathprint (' The value of PI is approximately {}. '). Format (Math.PI)) print (' The value of pi is approximately {!r} '. Format (Math.PI)) print (' The value of pi is approximately {0:.3f} '. Format (MATH.PI)) Table = {' Sjoerd ': 4127, ' Jack ': 4098, ' dcab ': 7678}for name, phone in Table.items (): Print (' {0:10} ==> ; {1:10d} '. Format (name, phone)) Table = {' Sjoerd ': 4127, ' Jack ': 4098, ' Dcab ': 8637678}print (' jack: {0[jack]:d}; SjOerd: {0[sjoerd]:d}; ' Dcab: {0[dcab]:d} '. Format (table)
Operating effect:
Python 3.3.2 (V3.3.2:d047928ae3f6, May, 00:03:43) [MSC v.1600 + bit (Intel)] on Win32type "copyright", "credits" or "license ()" For more information.>>> ================================ RESTART =========================== =====>>> i ' m hongten,welcome to my space!####################################### #Hello, I ' m Hongten,my e-mail Is Hongtenzone@foxmail.comhello,i ' m hongten,my e-mail is hongtenzone@foxmail.com################################### # # # # # #Hi, tom,how old was you?####################################### #Hello, I ' m hongten,this is a test message!######### ############################## #The value of pi is approximately 3.141592653589793.The value of pi is approximately 3.14159 2653589793.The value of PI is approximately 3.142.Jack ==> 4098Sjoerd ==> 4127Dcab ==>
I hope this article is helpful for Python program design.