Article directory
2.2 Composite field names
A field name that uses both forms of a number and a variable name is a composite field name.
2.2.1 Using . dot notation
Pass positional arguments
Replacement field form: {数字.属性名}
numbers can be omitted when there is only one replacement field
class Person(object):
def __init__(self,name,age,gender):
self.name = name
self.age = age
self.gender = gender
p = Person('zhangsan',18,'female')
print('姓名是{0.name},年龄是{0.age},性别是{0.gender}'.format(p))
print('姓名是{.name}'.format(p)) # 只有一个替换字段时,可以省略数字
2.2. Use [] brackets
Pass positional parameters
as a list Pass positional parameters as a tuple
Pass positional parameters as a dictionary
- Bracket usage: passing positional parameters as a list
infos = ['阿星', 9527]
food = ['霸王花', '爆米花']
print('我叫{0[0]},警号{0[1]},爱吃{1[0]}。'.format(
infos, food))
"""
我叫阿星,警号9527,爱吃霸王花。
"""
- Bracket usage: passing positional arguments as tuples
food = ('僵尸', '脑子')
print('我叫{0[0]},年龄{1},爱吃{0[1]}。'.format(
food, 66))
"""
我叫僵尸,年龄66,爱吃脑子。
"""
- Bracket usage: passing positional parameters with a dictionary
dic = dict(name='阿星', pid=9527)
print('我是{[name]}!'.format(dic))
# 多个替换字段,不能省略数字
print('我是{0[name]},警号{0[pid]}。'.format(dic))
"""
我是阿星!
我是阿星,警号9527。
"""
3. Convert fields
There are three values for the conversion field conversion field, which must be added before !
:
s
: call on the parameter before passing the parameterstr()
r
: call on the parameter before passing the parameterrepr()
a
: call on the parameter before passing the parameterascii()
ascii()
A function is like repr()
a function, returning a string that can represent an object. But for non-ASCII characters, escape them with \x
, \u
or \U
.
# 转换字段
print('I am {!s}!'.format('Bruce Lee 李小龙'))
print('I am {!r}!'.format('Bruce Lee 李小龙'))
print('I am {!a}!'.format('Bruce Lee 李小龙'))
"""
I am Bruce Lee 李小龙!
I am 'Bruce Lee 李小龙'!
I am 'Bruce Lee \u674e\u5c0f\u9f99'!
"""
4. Format specifiers
The use of format specifiers is too complicated, and there are not many actual usage scenarios, so we will not discuss it for now.
Advanced case
[Python] Python realizes the word guessing game-challenge your intelligence and luck!
[python] Python tkinter library implements GUI program for weight unit converter
[python] Use Selenium to get (2023 Blog Star) entries
Use Tencent Cloud Cloud studio to realize scheduling Baidu AI to realize text recognition
[Xiaobai must see] Python word cloud generator detailed analysis and code implementation
[Xiaobai must see] Python crawls an example of NBA player data
[Must-see for Xiaobai] Sample code for crawling and saving Himalayan audio using Python
[Xiaobai must see] Python crawler data processing and visualization
[Must-see for Xiaobai] Python crawler program to easily obtain hero skin pictures of King of Glory
[Must-see for Xiaobai] Use Python to generate a personalized list Word document
Geospatial data processing and visualization using geopandas and shapely (.shp)
Use selenium to crawl Maoyan movie list data
Detailed explanation of the principle and implementation of image enhancement algorithm Retinex
Getting Started Guide to Crawlers (8): Write weather data crawler programs for visual analysis
Getting started with crawlers (4): The best way to crawl dynamic web pages using Selenium and API
Getting Started Guide to Crawlers (3): Python network requests and common anti-crawler strategies
Getting started with crawlers (2): How to use regular expressions for data extraction and processing
Getting started with reptiles (1): Learn the basics and skills of reptiles
Python object-oriented programming basics and sample code
MySQL database operation guide: learn how to use Python to add, delete, modify and query operations
Python file operation guide: encoding, reading, writing and exception handling
Selenium Automation Toolset - Complete Guide and Tutorials
Python web crawler basics advanced to actual combat tutorial
Pandas data processing and analysis tutorial: from basics to actual combat
Detailed explanation of commonly used data types and related operations in Python
[Latest in 2023] Detailed Explanation of Six Major Schemes to Improve Index of Classification Model