In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "how to use format of Python", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use format of Python" article.
A detailed explanation of format. Basic usage
Format terminology description
As the format string function of Python, fotmat mainly identifies the replacement field through the curly braces {} in the string, thus completing the formatting of the string.
Print ("my name is {}, and I am {} years old this year." Format ("Xiaomi", 18) # my name is Xiaomi, and I am 18 years old. The number of curly braces determines the number of parameters. But the number of curly braces can be less than the parameter. Print ("I like {} and {}" format ("Table Tennis", "Badminton", "knock Code") # I like table tennis and badminton. "" if there are more curly braces than parameters, an error will be reported. ""
two。 Pass in position parameters through numeric parameters
Note the following when passing in parameters
The number must be an integer greater than 0
Replacement fields with numbers can be repeated
A simple field name in numeric form is equivalent to treating the field as a sequence. Take values one by one in the form of index
# pass the parameter print ("name {0}, residence {1}") through the numeric index. Format ("Orange Liuxiang", "Fruit Village") # replace 1 field with a number can repeat "pythonprint (" I love {0}). \ nHe loves {1}. \ n {0} Love {1} ".format" ("Grey Wolf", "Red Wolf") "I love Grey Wolf he loves Red Wolf Grey Wolf Love Red Wolf" the simple field name in the digital form is equivalent to treating the field as a sequence. Select values one by one in the form of index "print" ("Xiao Ming likes {1}, {2} and {0}" .foramt ("SpongeBob", "Robot Cat", "Sea Thief King", "Naruto", "Dragon Ball")) # Xiaoming likes robotic cats, SpongeBob, and SpongeBob
3. Pass by keyword
Print ("I am {age} years old, I am reading {college}" .format (age=18 "," college= "university") # I am 18 years old, I am 20 years old # keywords can be placed freely print ("I am {age} years old, I am reading {college}" .format ("college=" University ", age=18"))
4. Mixed use of keywords and numbers
Note the following
Numeric and key fields can be mixed with passing parameters
The keyword argument must come after the positional parameter.
When mixed, the number can be omitted
Omitting field name {} cannot be used with a numeric field name
# mixed use of the passing parameter print ("I want to be {0}, he wants to be {1}, there is only one {truth} in this world" .format ("King of Sea Thief", "Fire Shadow", truth= "Truth") # I want to be King of Sea Thief, he wants to be Fire Shadow, there is only one truth in this world # numbers can also be omitted print ("I want to be {}, he wants to be {}") There is only one {truth} ".format (" King of the Sea Thief "," Fire Shadow "," truth= "" Truth ") # if the Guan Jian word comes before the position parameter, then''SyntaxError: unexpected indent! [insert picture description here] (https://img-blog.csdnimg.cn/20210321105132614.png#pic_center)'''
5. Use tuples and dictionaries to pass parameters
Format can use tuples and dictionaries to pass parameters, and the two can be mixed.
When used in a variety of mixed uses. The position parameter is in front of the keyword parameter, and the tuple is in front of the dictionary.
A = [Naruto "," Huoying "," Shibata "] print (" I am {}, I am the man who wants to be {} ".format (* a))"I am Naruto, I am the man who wants to be Huoying"print (" I am {1}, I am the man of {2} ".format (* a)) # use dictionary parameters v = {" name ":" Sun WuKong "," skill ":" tortoise Qigong "} print (" I am {name} " My trick is {skill} ".format (* * v)" I'm Sun WuKong My trick is to use both tuple and dictionary to pass parameter name= ["Carrot", "Jie Wang Quan"] names= {"nickname": "Sun Jun", "skill": "Yuan Qi bullet"} print ("I am {0}, my trick is {skill}" .format (* name,**names)) print ("I am {nickname}, my trick is {1}" .format (* name,**names)) # also use position parameters, tuple Keyword parameters, dictionary parameters. # Note that the position parameter should be preceded by the key number parameter a = ["name"] dic= {"name": "Super Saiyan"} print ("I am {0}, I am also {0}, because I am a soldier of justice, so I become {name}" .format ("Carlotte", * a justice fighter) "I am Carlotte, I am Sun WuKong, but what cannot be changed is that I am a soldier of justice." two。 Sublimation explains the use of 2.1 compound field names
Format uses both a number and a variable name, which is a compound field
Compound field names support two operators
-[] square brackets
-. Dot sign
2.2.Using class Person: def _ _ int__ (self,name,addr): self.name=name self.addr=addrp=Person ("Sun WuKong", "Baozi Mountain") # dot usage. Pass position parameters. Print ("I am {0.name}, home is {0.addr}" .format (p)) # when there is only one field You can omit the number print ("I am {.name}}" .format (p)) # try passing the properties of the file object f=open ("out.txt", "w") print ("file name:" {.name} .format (f)) # pass the keyword print ("I am {p.name}, home is {p.addr}" .format (pamphp)) print ("I am {girl.name}) Home in {girl.addr} ".format (girl=p))" I'm Sun WuKong Home is in Baozi Mountain. I'm Sun WuKong. I live in Baozi Mountain. "" 2.4Use square brackets mylist= ["Chen Daoming", "www.chendaoming.cc"] print ("website name: {0 [0], address {0 [1]}}" .format (my_list)) 2.5 align strings
The width at the back of the play
< 左对齐 后面带宽度 右对齐 后面带宽度 : 后面带填充的字符,只能是一个字符,不指定则是默认用空格填充 print("{:>5} ".format (1) # width is 5, right aligns print (": > 5 ".format (10)) print (": > 5 ".format (1000)) print (": > 5 ".format (100))" output is 1 10 100 100 ""
# positive sign indicates positive number print ("{: + 2f}" .format (3.14)) # + 3.140000print ("{:-2f}" .format (- 1)) #-1.00000 "print without decimal (" {: .0f} ".format (3.23123131)) #" print separated by comma ("{: } ".format (100000)) # 10000 percent means a percentage higher than print (" {: .2%} ".format (0.25)) # 25% is about the article" how to use Python's format " I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.