当前位置:网站首页>Summary of string processing skills II
Summary of string processing skills II
2022-06-11 06:46:00 【Dare you look at the avatar for three seconds?】
print Parameters sep and end
sep and end yes print Function object's own properties , among sep Represents a separator between multiple output values
print("sdjkhfs"," The job of a teacher is ",sep="|")
print("sdjkhfs"," The job of a teacher is ")
Sequence unpacking features and is considered to be false Several situations of
Sequence unpacking feature
x,y=333,555 # Separately x,y Assign a value
x,y=y,x # take x And y Swap the values of , It is equivalent to pointing the pointer to the other side
Think it's false Several situations of
none
0
“”
()
[]
{}
x=None
print(bool(x))
# Output false
exec and eval String rotation python perform
exec Is used to convert a string into python Execute statement execution , And will remain in memory , It's a bit like in javascript Use function objects to pass code to constructors in the form of strings
js:
var fun1=new Function("console.log('AddSearchProvider');");
p:
exec("a=6")
>>> a
6
You can do typical cases :gui 's calculator
python Object representation in
Similar to other languages (js,c#), An example represents an object :
person={
“name”:“ Xiangjiang xiaohaitao ”,sex:“ male ”}
Example object combination exec Function to pass values :
person={
“name”:“ Xiangjiang xiaohaitao ”,sex:“ male ”}
exec("print(' full name '+name+' Gender '+sex)",person)
# Output : Name: Xiangjiang xiaohaitao gender: male
About exec The application of function
ca=""
print(" If the input is ljyok, Indicates that the input is complete , Start execution ")
while True:
x=input("")
if x=="ljyok":
print("--------------------- Start executing code , And output the result --------------")
exec(ca)
break
else:
ca+=x+"\n"
# If the input is ljyok, Indicates that the input is complete , Start execution
# a=40
# b=23
# c=9
# x=a*b+c
# print(x)e
# ljyok
# --------------------- Start executing code , And output the result --------------
# 929
eval function
The execution effect of this function is similar to exec function ,eval Function will return an execution result , We can save the results of the execution
>>> x=eval("3")
>>> x
3
>>> y=exec("3")
>>> y
>>>
>>> x=eval("a+b",s,t)
>>> x
4
# This is repl The effect seen in the environment , The step-by-step execution effect of this environment is easier to see ,eval Function's ability to save execution results
The use of basic string processing operations
Use of the percent sign %
It used to be used for the remainder of numerical values 7%2=1
The percent sign is used in the string , You can replace the corresponding text in the string
give an example , The method of use is similar to c:
varstr=“ Welcome ,%s, Here is %s” %( Replace the previous content “x”,“ Programming ”)
Case operation :( adopt pyrhonrepl Environment to manipulate code )
varstr="hello,%s,this is %s" % (" Xiangjiang River "," Xiaohaitao ")
>>> print(varstr)
hello, Xiangjiang River ,this is Xiaohaitao
varstr2=" Hello! ,"+"leevis"+", Here is "+" Create program life "
>>> print(varstr2)
Hello! ,leevis, Here is to create a program life
Template string templete Class uses
templete Class to solve the problem of repeatability
In the use of template Class time , The text that needs to be replaced here needs to use $ Symbols and names
example :
import string
template=string.Template(" who $b It will eventually shake the world , have to $b A long body keeps silent ,"
" Who will light the lightning $a,$a Will drift like a cloud for a long time ")
print(template)
# Start replacing
#s=template.substitute(a=" Mummy mummy coax ")
s=template.substitute(a=" Mummy mummy coax ",b=" Fucking ")
print(s)
template=string.Template("dksjfhgs;kdfj${s}sdfg make $$")
print(template)
n=template.substitute(s="ttttt")
print(n)
#<string.Template object at 0x0141DE20>
# Who his mother will eventually shake the world , His mother will keep silent for a long time , Who will ignite the lightning? Mommy Mommy will coax , Mummy, mummy, will be drifting like clouds for a long time
#<string.Template object at 0x0141DCB8>
#dksjfhgs;kdfjtttttsdfg make $
Welcome to comment on , The next to continue .
边栏推荐
- 538. convert binary search tree to cumulative tree
- 洛谷P1091合唱队形(最长上升子序列)
- Array de duplication....
- instanceof到底是怎样判断引用数据类型的!
- Metasploitabile2 target learning
- Multimedia框架解析之MediaExtractor源码分析(一)
- 572. 另一个树的子树
- The classification effect of converting video classification data set to picture classification data set on vgg16
- Unity 全景漫游过程中使用AWSD控制镜头移动,EQ控制镜头升降,鼠标右键控制镜头旋转。
- 解决ffmpeg獲取AAC音頻文件duration不准
猜你喜欢

Simple integration of client go gin six list watch two (about the improvement of RS, pod and deployment)

Metasploitabile2 target learning

Zabbix 监控主机是否在线

Zvuldrill installation and customs clearance tutorial

JVM from getting started to giving up 2: garbage collection

Résoudre le problème de la durée inexacte du fichier audio AAC obtenu par ffmpeg

MongoDB安装

Vulnhub's breach1.0 range exercise

Redux learning (I) -- the process of using Redux

QT script document translation (I)
随机推荐
Handwritten promise [01] - Implementation of promise class core logic
Vulhub 8.1-backdoor vulnerability recurrence
Alias the path with the help of craco
数组去重。。。。
022-Redis数据库0基础入门
Quick sorting of graphic array [with source code]
Handwriting promise [03] - realize multiple calls and chain calls of then method
socket. IO cross domain stepping pit
Oracle提示无效数字
Jenkins user rights management
Learn C language well from keywords
JVM from getting started to giving up 2: garbage collection
Mediaextractor source code analysis of multimedia framework analysis (1)
QT socket setting connection timeout
FPGA interview topic notes (I) - FPGA development process, metastable state and competitive risk, build and hold time, asynchronous FIFO depth, etc
100. 相同的树
On cursor in MySQL
Starting from scratch (I)
Multimedia框架解析之MediaExtractor源码分析(一)
What are the differences and usages of break and continue?