当前位置:网站首页>Summary of string processing skills III

Summary of string processing skills III

2022-06-11 06:46:00 Dare you look at the avatar for three seconds?

character string format Method - Nested formatting

Add 1
formaat The one to many form of , Instead of multiple different values, just write one parameter

#from Module name ,import An actual object , function ( function )
from math import pi
print(pi)
# First the pi Leave two decimal places 
x="pi={0},pi2={0}"
print(x.format(pi))

Add 2 Supplement zero
A string of format Method to fill zero : It's actually i Whether to add zero before the length , By default, spaces are added , If you add zero , Just add zero
If the value length is less than the given format length, the default right alignment

x="pi={0:20}\npi2={0:20.2f}"
print(x.format(pi))

Add 3 Base conversion
Add... Before the base symbol # The pound sign can indicate the form in which the decimal number can be displayed

y="{:#b}"
print(y.format(7))

y="{:#x}"
print(y.format(19))

The replacement scheme of adding four strings and blank areas
Be careful : The padding character must be used in conjunction with its symbol , And write it to the left of its symbol

s="{:#^30}"
print(s.format(" Xiaohaitao "))

s="{:%^30}"
print(s.format(" Xiaohaitao "))

alignment
Use the symbol for it separately < ^ > , The alignment symbol is used with zero filling

# Align left 
x="pi={0:015}\npi2={0:0<15.2f}"
print(x.format(pi))
# Center on it 
x="pi={0:015}\npi2={0:0^15.2f}"
print(x.format(pi))

Nesting uses

s="{a} Xiangjiang small {c:.{d}f} ocean waves {b}"
print(s.format(a="dcfdf",b="bbb",c=pi,d=3))

It's actually in string formatting { Continue to write the data to be formatted internally {}}

Code

# Or right pi Carry out actual combat 
#from Module name ,import An actual object , function ( function )

from math import pi





print(pi)
# First the pi Leave two decimal places 
x="pi={0},pi2={0}"
print(x.format(pi))

x="pi={0:15}\npi2={0:15.2f}"
print(x.format(pi))

x="pi={0:015}\npi2={0:015.2f}"
print(x.format(pi))

# Align left 
x="pi={0:015}\npi2={0:0<15.2f}"
print(x.format(pi))
# Center on it 
x="pi={0:015}\npi2={0:15.2f}"
print(x.format(pi))
print(" In this way, the string in the blank area cannot be filled , So you have to use padding characters ^")
x="pi3={0:015}\npi22={0:0^15.2f}"
print(x.format(pi))

s="{a} Xiangjiang small {c} ocean waves {b}"
print(s.format(a="dcfdf",b="bbb",c="ccc"))



s="{a} Xiangjiang small {c:.2f} ocean waves {b}"
print(s.format(a="dcfdf",b="bbb",c=pi,d=3))

s="{a} Xiangjiang small {c:.{d}f} ocean waves {b}"
print(s.format(a="dcfdf",b="bbb",c=pi,d=3))



print("*"*90)
# Hexadecimal conversion 
y="{:#b}"
print(y.format(7))

y="{:#x}"
print(y.format(19))




s="{:#^30}"
print(s.format(" Xiaohaitao "))

s="{:%^30}"
print(s.format(" Xiaohaitao "))



3.141592653589793
pi=3.141592653589793,pi2=3.141592653589793
pi=3.141592653589793
pi2=           3.14
pi=3.141592653589793
pi2=000000000003.14
pi=3.141592653589793
pi2=3.1400000000000
pi=3.141592653589793
pi2=           3.14
 In this way, the string in the blank area cannot be filled , So you have to use padding characters ^
pi3=3.141592653589793
pi22=000003.14000000
dcfdf Xiangjiang small ccc ocean waves bbb
dcfdf Xiangjiang small 3.14 ocean waves bbb
dcfdf Xiangjiang small 3.142 ocean waves bbb
******************************************************************************************
0b111
0x13
############# Xiaohaitao ##############
%%%%%%%%%%%%% Xiaohaitao %%%%%%%%%%%%%%

Process finished with exit code 0

character string center Method practice

center Method parameters
Method 1 center( Total width of custom string ) By default, fill with spaces
Method 2 center( Total width of custom string , The replacement character needs to be filled )
example

s=" Xiangjiang xiaohaitao "
s.center(30)
x="{}"+s+"{}"

print(s.center(3))
print(s.center(30,"="))
# print(s.center(30,"=="))
# use format Method 
#print("{:}".format())
print(x)
print(x.format("====","===="))
print("%"*100)
y="{:=^30}"

print(y.format(s))
y="{:^30}"

print(y.format(s))



# Output 
 Xiangjiang xiaohaitao 
============ Xiangjiang xiaohaitao =============
{
    } Xiangjiang xiaohaitao {
    }
==== Xiangjiang xiaohaitao ====
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
============ Xiangjiang xiaohaitao =============
             Xiangjiang xiaohaitao              









原网站

版权声明
本文为[Dare you look at the avatar for three seconds?]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020525471962.html