Tenth: String

String

The role of the string:

  • Input terminal: Data-driven automation, whether it is stored in excel or a database, it is read in the form of a string, and the read string needs to be done Processing, such as cutting the value of different parameters for the string
  • Output terminal: automatic assertion, the content of the string is required Do text verification, such as UI automation or interface automation. For example, interface automation needs to verify the return value and verify the text of UI automation.

1. Access to obtain the string: obtain through index, slice, traverse method

str=' 
str=' span>hello world'

print (str[0])
print (str[:6]+'< span style="color: #800000;">wenky
')

Run result:
h
hello wenky

# Traverse each character of the string

for i in str:
print (i)

Run results:
h
e
l
l
o

w
o
r
l
d

Two, line breaks and escape characters:

share picture

Example: Please print out the value of the following variables:

n = 123
f = 456.789
s1 ='Hello, world'
s2 ='Hello, \'Adam\''
s3 = r'Hello, "Bart"'
s4 = r'''Hello,
Lisa!'''
n ='< /span>n= 123'

f
='f= 456.789'
s1
='s1= \'Hello, world\'\' '
s2
= 's2= \'Hello, \\\'Adam \\\'\''
s3
= 's3= r\'Hello, \"Bart\ "\''
s4
= 's4= r\'\'\'Hello, \n Lisa!\'\'\''
print('\n',n,'\n',f,'< span style="color: #800000;">\n
',s1,' \n',s2,'\n',s3,'\n', s4,'\n' )


Run result:
n= 123
f= 456.789
s1='Hello, world''
s2='Hello, \' Adam\''
s3= r'Hello, "Bart"'
s4= r'''Hello,
Lisa!'''

Example 2:

print (r"\ \\ ' ''''' \n \t \r") #Do not change the escape symbol

print ("\\ '\''''' \n \t \r")

Run results :
\ \\ '''''' \n \t \r
\''''''

Description: contains r is a natural or original character and does not change the transfer character |. The string is expressed with single quotes' or double quotes ". If you want to express yourself, you need to use \ escape character to escape

Three, string operators

share pictures

Example: +, *, [] value, [::] slice, in operation

b1='abc'

b2
='dfg'
print (b1+b2)
print (b1*2)
print (b1[1])
print (b1[::2]) # Take odd characters
print (b2[1::2]) #Take even characters

Run results:
abcdfg
abcabc
b
ac
f

Four. String formatting:

Share a picture

Description: %d represents an integer; %s represents a string; %f represents a floating point number. If you specify a decimal point, if you specify two decimal places, it is %.2f; if there is a "%" in the string, use %% To represent a %.

Example: Xiao Ming’s score has increased from 72 points last year to 85 points this year. Please calculate the percentage point of Xiao Ming’s improvement and display it in string format 'xx.x%', only keep 1 digit after the decimal point:

a=72

b
=85
r
=(ba)/b*100 #The division calculation result is Floating point number
print ('%.1f%%'%r)

Run results:
15.3%

5. String alignment:

%nd : Default right alignment, specify length n

%-nd: Left alignment, specify length n

#%10d specifies the length of 10, the default is right justified

print ("I am %10d years old."%19)
#%-10d specifies the length of 10, left-justified
print ("I am %-10d years old."%19)

Run result:
I am 19 years old .
I am 19 years old.

Example 2: Display the following information of the student: name, sex, age, school, where the name is displayed with a length of 15 characters, sex is 10 characters in length, age is 5 characters in length, and school is 15 characters in length, and they are all left-aligned, and enter the information of no less than 5 students.

print (' %-15s%-10s%-5s%-20s'%('name',< span style="color: #800000;">'sex' ,'age' span>,'shcool' ))

print ('%-15s%-10s%-5d%-15s'%('aaa','g',12,'school1'))
print ('%-15s%-10s%-5d%-15s'%('bbb','b',32,'school2'))
print ('%-15s%-10s%-5d%-15s'%('ccc','b',12,'school3'))
print ('%-15s%-10s%-5d%-15s'%('ddd','g',32,'school4'))< br>
operating results:
name sex age shcool
aaa g 12 school1
bbb b 32 school2
ccc b 12 school3
ddd g 32 school4

< /pre>

Left alignment function: S.ljust(width,[fillchar]): output width characters, S is left aligned, and the insufficient part is supplemented by fillchar, the default is empty< /p>

str='abc

print (str.ljust(10,'*')) #Not enough *supplement
print (str.ljust(3,'*')) #There is no space, there is no filling character
print (str.ljust(10)) #fill with spaces by default

Run result:
abc*******
abc
abc

Right alignment function: S.rjust(width,[filchar]): output width characters, S is left aligned, the insufficient part is supplemented by fillchar, the default is empty

str='abc'

print (str.rjust(10,'*')) #Not enough *supplement
print (str.rjust(3,'*')) #There is no space, there is no filling character
print (str.rjust(10)) #fill with spaces by default


Run result:
*******abc
abc
abc

< /span>

Intermediate alignment function: S.center(width, [fillchar]): output width character, S is aligned in the middle, the insufficient part is supplemented by fillchar, the default is empty

str='abc'

print (str.center(10,'*')) #Not enough *supplement
print (str.center(3,'*')) #There is no space, there is no filling character
print (str.center(10)) #fill with spaces by default


Run result:
***abc****
abc
abc

zfill() is right aligned, it is not enough to automatically fill with 0

str='123abc'

print (str.zfill(10))
print (str.rjust(10,'< span style="color: #800000;">0'))

Run result:
0000123abc
0000123abc

Six. Functions for cleaning up invisible characters: strip(), lstrip(), rstrip():

1. Description of S.strip() function:

You can remove the left and right spaces \t and other blank contents of the string. This function can remove the spaces, \t, etc. on the left and right sides of the string The blank content or the specified string is removed, and the processed result is returned, but the original string has not been changed.

The strip() function without parameters means to remove all the blank characters before and after S in S, including invisible characters such as'\n','\t','\r', '' String can be understood as replacing the blank strings before and after S with None;
The strip() function with parameters means that the specified string chars before and after S is removed.
# Clear blank characters before and after the default< /span>

s='boy boy boy'
print (s.strip())

# * is not the last digit, so it can’t be cleaned up
s1='***boy boy boy***\n '
print (s.strip(‘*‘))

# Clean up specified characters
s='hello boy boy boy'
print (s.strip('hello '))

Run result:
boy boy boy
boy boy boy ***
boy boy boy

2, lstrip(): The left side of the string \t and other blank content Remove

# Clean up the left blank characters by default 

s='boy boy boy'
print (s.lstrip())

# Clean up the specified characters on the left
s='hello boy boy boy'
print (s.strip('hello '))

# The * is not on the far left, so it can’t be cleaned up
s1='\n***boy boy boy***\n span>'
print (s1.lstrip('*'))

Run result:
boy boy boy
boy boy boy
***boy boy boy***

< p>3, rstrip(): Remove blank content such as the space \t on the right side of the string

#

span> Clear the left blank characters by default
s='boy boy boy'
print (s.rstrip())

# Clean up the specified characters on the right
s1='hello boy boy boy'
print (s1.rstrip('boy'))

# * is not on the far right, so it can’t be cleaned up
s2='\n***boy boy boy***\n span>'
print (s2.rstrip('*'))


Run result:
boy boy boy
hello boy boy
***boy boy boy***

Seven. Case swap of string: ascii code value swap, char()

#< span style="color: #008000;">Uppercase to lowercase: lower()

>>> str='ABC'
>>> print str.lower()
abc


#Lowercase to uppercase: upper()
>>> str='abc'
>>> print str.upper()
ABC


#Swapcase: swapcase()
>>> str1='asBDs'
>>> print str1.swapcase()
ASbdS


#The first letter is converted to uppercase: capitalize()
>>> s2='i love python'
>>> print s2.capitalize()
I love python


#Capitalize the first letter of each letter: capwords()-Yes The method of string module, the effect is the same as title()

>>> import string
>>> s4='study python'
>>> print string.capwords(s4)
Study Python
>>> print s4.title()
Study Python

8. String search

str= 'hello world' span>

print (str[0])
print (str[:6]+'< span style="color: #800000;">wenky')

Run result:
h
hello wenky

#traverse each string Characters

for i in str:
print (i)

Run results:
h
e
l
l
o

w
o
r
l
d

n ='n= 123'

f
='f= 456.789'
s1
='s1= \'Hello, world\'\' '
s2
= 's2= \'Hello, \\\'Adam \\\'\''
s3
= 's3= r\'Hello, \"Bart\ "\''
s4
= 's4= r\'\'\'Hello, \n Lisa!\'\'\''
print('\n',n,'\n',f,'< span style="color: #800000;">\n',s1,' \n',s2,'\n',s3,'\n', s4,'\n' )


Run result:
n= 123
f= 456.789
s1='Hello, world''
s2='Hello, \' Adam\''
s3= r'Hello, "Bart"'
s4 = r'''Hello,
Lisa!'''

print (r"\ \\ '''''' \n \t \r ") #Do not change the escape character

print ("\\ '\''''' \n \t \r")

Run results :
\ \\ '''''' \n \t \r
\''''''

b1='abc'

b2
='dfg'
print (b1+b2)
print (b1*2)
print (b1[1])
print (b1[::2]) # Take odd characters
print (b2[1::2]) #Take even characters

Run results:
abcdfg
abcabc
b
ac
f

a=72

b
=85
r
=(ba)/b*100 #The division calculation result is Floating point number
print ('%.1f%%'%r)

Run results:
15.3%

#%10d specifies the length of 10, the default is right aligned

print ("I am %10d years old."%19)
#%-10d specifies the length of 10, left-justified
print ("I am %-10d years old."%19)

Run result:
I am 19 years old .
I am 19 years old.

print ('%-15s%-10s%-5s%-20s'%('name','sex','age ','shcool span>'))

print ('%-15s%-10s%-5d%-15s'%('aaa','g',12,'school1'))
print ('%-15s%-10s%-5d%-15s'%('bbb','b',32,'school2'))
print ('%-15s%-10s%-5d%-15s'%('ccc','b',12,'school3'))
print (%-15s%-10s%-5d%-15s%(ddd,g,32,school4))

运行结果:
name           sex       age  shcool             
aaa            g         12   school1       
bbb            b         32   school2       
ccc            b         12   school3       
ddd            g         32   school4       

str=abc

print (str.ljust(10,*)) #不足以*补充
print (str.ljust(3,*)) #没空余位置,则没有填充字符
print (str.ljust(10)) #默认以空格填充

运行结果:
abc*******
abc
abc

str=abc

print (str.rjust(10,*)) #不足以*补充
print (str.rjust(3,*)) #没空余位置,则没有填充字符
print (str.rjust(10)) #默认以空格填充


运行结果:
*******abc
abc
       abc

str=abc

print (str.center(10,*)) #不足以*补充
print (str.center(3,*)) #没空余位置,则没有填充字符
print (str.center(10)) #默认以空格填充


运行结果:
***abc****
abc
   abc   

str=123abc

print (str.zfill(10))
print (str.rjust(10,0))

运行结果:
0000123abc
0000123abc

不带参数的strip()函数,表示把S中前后所有的空白字符全部去掉,包括’\n’ , ‘\t’ , ‘\r’ , ’ ’ 等不可见字符串,可以理解为把S前后空白字符串替换为None;

带参数的strip()函数表示将S前后指定字符串chars去掉。

# 默认清理前后空白字符

s=boy boy boy
print (s.strip())

# *号不是最后位,所以清理不了
s1=***boy boy boy***\n
print (s.strip(‘*‘))

# 清理指定字符
s=hello boy boy boy
print (s.strip(hello ))

运行结果:
boy boy boy
boy boy boy***
boy boy boy

# 默认清理左边空白字符

s=boy boy boy
print (s.lstrip())

# 清理左边指定字符
s=hello boy boy boy
print (s.strip(hello ))

# *号不是最左边,所以清理不了
s1=\n***boy boy boy***\n
print (s1.lstrip(*))

运行结果:
boy boy boy
boy boy boy
 
***boy boy boy***

boy boy boy
boy boy boy

 

***boy boy boy***

# 默认清理左边空白字符

s=boy boy boy
print (s.rstrip())

# 清理右边指定字符
s1=hello boy boy boy
print (s1.rstrip(boy))

# *号不是最右边,所以清理不了
s2=\n***boy boy boy***\n
print (s2.rstrip(*))


运行结果:
boy boy boy
hello boy boy
***boy boy boy***

boy boy boy
hello boy boy

***boy boy boy***

#大写转换小写:lower()

>>> str=ABC
>>> print str.lower()
abc


#小写转换大写:upper()
>>> str=abc
>>> print str.upper()
ABC


#大小写互换:swapcase()
>>> str1=asBDs
>>> print str1.swapcase()
ASbdS


#首字母转换为大写:capitalize()
>>> s2=i love python
>>> print s2.capitalize()
I love python


#每个字母的首字母大写:capwords()-是string模块的方法,效果同title()

>>> import string
>>> s4=study python
>>> print string.capwords(s4)
Study Python
>>> print s4.title()
Study Python

Leave a Comment

Your email address will not be published.