From the beginning of Python learning episod 6 -- string operation

Keywords: Python REST

String operation

1, Output duplicate string

print('smile'*6)
#Output 6 smile s

 

2, Output part of string through index

print('smile'[1:])
print('smile'[1:3])
#output
#mile
#mi
#ims

This is the same as the previous list slicing rules.

 

3, Checking string contents with in

a = 'Tomorrow will be a better day. '
print('day' in a)
print('zzz' in a)
#output
#True
#False

Returns False if there is no corresponding string in the string

Returns True if there is a corresponding string in the string

 

4, Format string

a = 'Tomorrow'
b = 'The day after tomorrow'
print('%s will be a better day. '% a)
print('%s will be a better day. '% b)
#output
#Tomorrow will be a better day. 
#The day after tomorrow will be a better day. 

In this way, strings can be used more flexibly, and it is convenient to modify the contents of sentences without changing the fixed parts.

 

5, String splicing

a = 'aaa'
b = 'bbb'
c = 'ccc'
d = ''.join([a,b,c])
print(d)
d = '*'.join([a,b,c])
print(d)
#output
#aaabbbccc
#aaa*bbb*ccc

 

6, A series of built-in methods

st = 'smile puppy'

print(st.count('p'))            #Number of statistical elements
print(st.capitalize())          #Make the initial uppercase output smile puppet
print(st.center(21,'#'))        #Subtract the content of st, center it, and then fill it with symbols on both sides. The number is parameter 1, subtract the content length of st
print(st.startswith('s'))       #Judge whether to start with a content, return True or False
print(st.endswith('y'))         #Judge whether to end with a content, return True or False
print(st.expandtabs(tabsize=10))#Modify \ tnumber of spaces represented
print(st.find('e'))             #Find the first element and return its index value

# st = 'smile puppy {name}'
# print(st.format(name='puppy'))#Another way to format, but you have to add {xxx} when defining a string

print('123abd Noon'.isalnum())    #Determine whether all characters in the string are letters or numbers or Chinese
print('1513'.isdigit())          #Determine whether the string is an integer
print('1165162'.isnumeric())     #Check whether the string is composed of numbers only
print('31351'.isidentifier())    #Judge whether it is an illegal variable
print('anc'.islower())           #Judge whether it is all lowercase
print('ABD'.isupper())           #Judge whether it is all capitalized
print('ABD'.isspace())           #Judge whether it is all blank
print('My Name'.istitle())       #Determine whether it is a title (whether the initial of each word is capitalized)
print('My Name'.lower())         #All lowercase
print('My Name'.upper())         #Capitalize all
print('My Name'.swapcase())      #Upper case elements become lower case, lower case elements become upper case
print('My Name'.ljust(10,'*'))   #The content is on the left, and the rest is filled with "*"
print(' My Name\n'.strip())      #Clear spaces and line breaks at beginning and end \ n
print('My Name'.replace('Name','Car',1))#Replace a "Name" with "Car", and modify all the parameters by default
print('My Name'.rfind('e'))      #Retrieve the target element from right to left to return its true index value (from left to right)
print('My Name Name'.split(' ',1)) #The string is divided by specifying a separator. The number parameter is the number of times of division, and full division is not filled in by default
                                   #Output ['My ','name']
print('My Name Name'.rsplit(' ',1))#Splits a string by specifying a separator, starting on the right
print('My title title'.title())    #Returns the 'captioned' string, i.e. all words are capitalized and the rest are lowercase

Posted by bogdan on Sat, 28 Dec 2019 08:45:50 -0800