当前位置:网站首页>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
边栏推荐
猜你喜欢

Fix the problem that the right mouse button does not have a vscode shortcut

572. 另一个树的子树

Moment time plug-in tips -js (super detailed)

关于SIoU的原理和代码实现(回顾IoU、GIoU、DIoU、CIoU)

Who is stronger, zip or 7-Zip, and how to choose?

Autojs, read one line, delete one line, and stop scripts other than your own
![Illustrate the principle of one-way linked list and the method of JS to realize linked list [with source code]](/img/0d/2de3413fcb77ac2bd093677035f2e7.jpg)
Illustrate the principle of one-way linked list and the method of JS to realize linked list [with source code]

Implementation of customization function page of online Fox game server room configuration wizard service

洛谷P1091合唱队形(最长上升子序列)

FPGA interview topic notes (I) - FPGA development process, metastable state and competitive risk, build and hold time, asynchronous FIFO depth, etc
随机推荐
Mongodb installation
[]==! []
Biweekly investment and financial report: capital rush yuan universe game
搜狐员工遭遇工资补助诈骗 黑产与灰产有何区别 又要如何溯源?
Metasploitabile2 target learning
Training and testing of super score model in mmediting
解决ffmpeg獲取AAC音頻文件duration不准
Implementation of customization function page of online Fox game server room configuration wizard service
572. 另一个树的子树
What is sentinel produced by Ali?
Alias the path with the help of craco
关于组织开展2022年宁波市重点首版次软件申报工作的通知
563. slope of binary tree
Use meshlab to sample the point cloud of CAD model and display it in PCL
Moment time plug-in tips -js (super detailed)
What are the differences and usages of break and continue?
Text overflow failure
Flat design, blog website (VIII) code source code
Handwriting promise [02] - asynchronous logic implementation
021-MongoDB数据库从入门到放弃