当前位置:网站首页>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
边栏推荐
- Difference between foreach, for... In and for... Of
- 关于parseInt()
- Zvuldrill installation and customs clearance tutorial
- Redux learning (III) -- using Redux saga, writing middleware functions, and splitting reducer files
- JVM from getting started to abandoning 1: memory model
- JVM from getting started to giving up 2: garbage collection
- []==![]
- JS two methods to determine whether there are duplicate values in the array
- break,continue有什么区别和用法?
- Wan Zichang's article shows you promise
猜你喜欢

搜狐员工遭遇工资补助诈骗 黑产与灰产有何区别 又要如何溯源?

Use of qscriptengine class

A highly controversial issue

What is sentinel produced by Ali?

347. top k high frequency elements

A piece of code has been refactored six times by the boss, and my mind is broken

100. same tree

Vulnhub's breach1.0 range exercise

Mediaextractor source code analysis of multimedia framework analysis (1)

Multimedia框架解析之MediaExtractor源码分析(一)
随机推荐
QT socket setting connection timeout
UEFI查找PCI设备
June training (day 11) - matrix
Vulhub 8.1-backdoor vulnerability recurrence
统计某次操作(函数)耗时时长
ijkPlayer中的错误码
021-MongoDB数据库从入门到放弃
Won't virtual DOM be available in 2022? Introduction to virtual Dom and complete implementation of diff and patch
Resolve typeerror: ctx injections. tableRoot.$ scopedSlots[ctx.props.column.slot] is not a function
Wechat applet (authorized login of TP5)
Moment time plug-in tips -js (super detailed)
NPM upgrade: unable to load file c:\users\administrator\appdata\roaming\npm\npm-upgrade ps1
你知道IT人才外派服务报价是怎样的么?建议程序员也了解下
How to arrange the dataframe from small to large according to the absolute value of a column?
Redux learning (I) -- the process of using Redux
JS implementation of Hill sort of graphic insertion sort [with source code]
核查医药代表备案信息是否正确
不引入第三个变量,交换两个值
235-二叉搜索树的最近公共祖先
Exchange two values without introducing the third variable