bzdww

Get answers and suggestions for various questions from here

Cool format

cms

The % that used to format strings has been used recently, and I recently learned about format. Format is the new way to format strings in Python (compared to %), it is very convenient to use, can achieve a lot of functions that can not be achieved. This article will introduce the cool format.


Basic usage

"Hello {0}".format("Peppa Pig") # 引用第一个参数
# 输出 'Hello Peppa Pig'

"{} is cute".format("Peppa Pig") # 引用第一个参数
# 输出 'Peppa Pig is cute'

"My name is {name}".format(name="Peppa Pig") # 引用名字为name的参数
# 输出 'My name is Peppa Pig'


Type conversion

"Peppa pig is a cute {0!s}".format("baby") # !s 相当于对于参数调用str()
# 输出 'Peppa pig is a cute baby'

"Peppa pig is a cute {0!r}".format("baby") # !r 相当于对于参数调用repr()
# 输出 "Peppa pig is a cute 'baby'"


Binary conversion in format

'{0} in HEX is {0:#x}'.format(16)
# 输出 '16 in HEX is 0x10'

'{0} is OCT is {0:#o}'.format(16)
# 输出 '16 is OCT is 0o20'


Align string

"{:>5}".format(1) # 设置宽度为5,右对齐
"{:>5}".format(10)
"{:>5}".format(100)
"{:>5}".format(1000)
# 输出下面的结果
'    1'
'   10'
'  100'
' 1000'

'{:_<10}'.format('test') # 左对齐,并且指定"_"填充空白部分
# 输出 'test______'

'{:_^10}'.format('test') # 居中对齐,并且指定"_"填充两侧
# 输出 '___test___'
# 注意:用%格式化字符串不支持居中对齐


Truncated string

'{:.5}'.format('Hello Peppa') # 截取前5个字符
# 输出 'Hello'


Time formatting

from datetime import datetime

'{:%Y-%m-%d %H:%M}'.format(datetime(2018, 5, 19, 21, 00))
# 输出 '2018-05-19 21:00'
# 注意:用%格式化字符串不支持此功能


Access object properties

class Pig(object):
    def __init__(self, name, age, weight):
        self.name = name
        self.age = age
        self.weight = weight

    def __repr__(self):
        return '<Pig name="{0}" age={1}>'.format(self.name, self.age)

a = Pig("Peppa", 5, 30)
print("Peppa is {0.age} years old, {0.weight} kilograms".format(a))

# 输出 Peppa is 5 years old, 30 kilograms
# 注意:用%格式化字符串不支持此功能


Accessing elements in a tuple

a = (1,2)
'X: {0[0]};  Y: {0[1]}'.format(a)
# 输出 'X: 1;  Y: 2'
# 注意:用%格式化字符串不支持此功能


Accessing elements in the dictionary

peppa = {"name": "Peppa", "age": 5}
"My name is {pig[name]} and my age is {pig[age]}".format(pig=peppa)
# 输出 'My name is Peppa and my age is 5'
# 注意:用%格式化字符串不支持此功能


Parameter specification format

Format supports the specified format of the incoming parameters, as follows

'{:{char}{align}{width}}'.format('test', char='_', align='^', width='10')
# 输出 '___test___'
# 注意:用%格式化字符串不支持此功能

from datetime import datetime
dt = datetime(2018, 5, 19, 21, 0)
'{:{dfmt} {tfmt}}'.format(dt, dfmt='%Y-%m-%d', tfmt='%H:%M')
# 输出 '2018-05-19 21:00'
# 注意:用%格式化字符串不支持此功能


First sort out these, welcome to add ~


This article has updated the same name public number [Python and Data Analysis] (ID: PythonML), here to share interesting Python knowledge, and do interesting data analysis with Python. Welcome attention~