Python下多种方式实现字符串格式化

2022-05-03 00:00:00 字符串 多种 格式化

本文列举了python下多种方式对字符串进行格式化和字符替换

字符串替换-定位

first_name = 'Alex'
last_name = 'Marshall'
print('My name is {}.'.format(first_name))
print('My name is {} {}.'.format(first_name, last_name))

output

My name is Alex.
My name is Alex Marshall.

使用索引替换字符串

当位置不同时使用索引非常有用

print('My name is {0} {1}.'.format(first_name, last_name))
print('My name is {1}, {0}. First name {0}'.format(first_name, last_name))

output

My name is Alex Marshall.
My name is Marshall, Alex. First name Alex

对齐:向左、向右、居中对齐

{:<} align Left (default is align left, so this is optional)
{:>n} align Right with n padding spaces
{:^n} align Middle with n padding spaces

# align left - these both do the same thing
cases = [5, 16, 294]
for case in cases:
    print('Number of cases: {}'.format(case))
for case in cases:
    print('Number of cases: {:<}'.format(case))

output

Number of cases: 5
Number of cases: 16
Number of cases: 294
Number of cases: 5
Number of cases: 16
Number of cases: 294
# align right with 5 total spaces
for case in cases:
    print('Number of cases:{:>5}'.format(case))

output:

Number of cases:    5
Number of cases:   16
Number of cases:  294
# align center with 5 total spaces
for case in cases:
    print('Number of cases:{:^5}'.format(case))

output

Number of cases:  5  
Number of cases: 16  
Number of cases: 294 

整数和浮点数

{:d} Integer variable
{:5d} Integer with padding of 5
{:f} Floating point variable

length = 26
print('Length is {:d}.'.format(length))
# align right, padding=6, integer
print('Length is {:>6d}.'.format(length))
# named variable, align center, padding=4, integer
print("In dog years, I'm {age:^5d}.".format(age=8))

output

Length is 26.
Length is     26.
In dog years, I'm   8  .
# integer with commas
print('Distance to moon is {:,d} miles.'.format(238900))
Distance to moon is 238,900 miles.
radius = 4.78
print('Radius is {:f} inches.'.format(radius))
# round to 1 decimal place, float
print('Radius is {:.1f} inches.'.format(radius))
# padding=6 (pads with leading 0's), round to 1 decimal
print('Radius is {:06.1f} inches.'.format(radius))
# padding=5 decimal places
print('Radius is {:.5f} inches.'.format(radius))

output:

Radius is 4.780000 inches.
Radius is 4.8 inches.
Radius is 0004.8 inches.
Radius is 4.78000 inches.
# positive & negative signs
a, b, c = 15, -9, 33
print('A is {:+d}.  B is {:+d}.  C is {:-d}.'.format(a, b, c))
# {+3d} shows pos or neg sign, padding=3. 
# {: d} prints neg or a leading space if positive.
print('A is {:+3d}.  B is {:+4d}.  B is {: d}.'.format(a, b, b))

output:

A is +15.  B is -9.  C is 33.
A is +15.  B is   -9.  B is -9.

命名占位符

You can pass in named variables as keyword args, or as an unpacked dict.
And it's easy to pass in a list.

print("{name} is a {job}.".format(name='Mekael', job='Carpenter'))

output:

Mekael is a Carpenter.
name = 'Mekael'
job = 'Carpenter'
# THIS DOES NOT WORK!
print("{name} is a {job}.".format(name, job))
# This works great
print("{n} is a {j}.".format(n=name, j=job))

output:

Mekael is a Carpenter.
# Or use a dictionary, and ** unpacks the dictionary.
jobs = {'name':'Mekael', 'job':'Carpenter'}
print("{name} is a {job}.".format(**jobs))

output:

Mekael is a Carpenter.
# passing in a list is clean and easy
scores = [78, 96, 83, 86]
print('Score 2 is {s[1]}'.format(s = scores))

output:

Score 2 is 96

科学计数法

Use {:e}, or upper case E.
print('My big number is {:e}'.format(874.577))
print('A bigger number is {:E}'.format(602214090000000000000000))

output:

My big number is 8.745770e+02
A bigger number is 6.022141E+23

二进制和十六进制

{:b} converts decimal to binary {:x} converts decimal to hex. Or use upper case X for capitals.

print('The binary equivalent of 79 is {:b}'.format(79))
print('The Hexadecimal equivalent of 183 is {:X}'.format(183))

output:

The binary equivalent of 79 is 1001111
The Hexadecimal equivalent of 183 is B7

相关文章