当前位置:网站首页>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 .

原网站

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