The conversion between bool values and empty string is False and there is content in the string is True.
a = 11 c = str(a) # convert int to str print (type(c)) a = ' 123456 ' b = int(a) # convert str to int print (type(b)) a = '' #empty string is False print (bool(a)) b = ' ' #The content in the string is True print (bool(b))
Common functions of str (***important)
**
capitalize
capitalize()
s = ' ZHANGyaJIe '
s1 = s.capitalize() #The first letter is capitalized, and the rest are all lowercase. print (s1)
*
center
s2 = s.center(27, '<') # center and fill
print(s2)
***
upper
lower
s1 = s.upper() #all uppercase
print(s1)
s2 = s.lower() #all lowercase
print(s2)
implements a small function of verification code.
mode = 'AEad'.upper()
name = input('Please enter the verification code').upper()
if name == mode:
print('The output is successful')
***
startswith
endswith
s = 'laonanhai'
print(len(s))
s2 = s. startswith ('lao')#startswith is to judge what starts with (the judgment is not True, it is False)
s2 = s.startswith(' nan',3,6) # Can be sliced, slices are separated by commas, and the index starts from 0.
s3 = s.startswith('ona',2,5) # Can be sliced, the slices are separated by commas, and the index starts from 0.
s1 = s.endswith('hai')
s1 = s. endswith ('lao',0,3) #Can be sliced, the slices are separated by commas, and the index 0 starts (regardless of the head and the tail)
print(s1)
*
swapcase
s = 'laonanhai'
s1 = s.swapcase() # case flip
*
title
s = 'lao5nan4hai'
s1 = s.title() #Separate non-letters, the first letter of each word is capitalized.
***
index
s = 'lao5nan4hai'
s1 = s.index('4ha',7,10) #Find the index through the element, it can be sliced, and an error is not found.
s = 'ABCDEFG1AHIJAK'
print(len(s))
s1 = s.find('DEFG', 2, 7,) Find the index by element, it can be sliced, if it is not found, return -1.
***
strip
name = input('please input').strip() #Remove leading and trailing spaces and newlines, tabs
pwd = input('please input').strip()
if name == 'zhang' and pwd == '123' :
print('Login successful').
***
split
s = 'zhang alex taibai'
s1 = s.split()#Convert the string to list format. space-separated by default
s1 = s.split(',')#Convert string to list format. space-separated by default
***
join
li = ['zhang', 'alex', 'taibai']
s11 = ','.join(li) #Convert the list to str mode by join.
**
replace
s = 'Ha ha, Xiaofennen is just to pass a lot of things. Dog Xiaofennen broth is not ah, is it Xiaofennen?'
s11 = s.replace('Xiaofennen','Big Hammer',2) #Only from Replace from left to right. Replacement is not possible from the right.
***
count
s = '2sdffhdyertdgdyhsfgdfhrgsdfg'
at =s.count('f',0,6) #Find the number of occurrences in the element.
***
format
msg = 'My name is {}, I am {} years old this year, and I have a hobby of {}'.format('MT',17,'playing basketball')
msg = 'My name is {0}, I am {1} this year, and my hobby is {2} , my name is still {0}'.format('MT',17,'basketball')
msg = 'My name is {name}, this year is {age}, and my hobby is {hobby}'.format(name = 'MT', age =16,hobby='playing basketball')
**
isalpha
isalnum
isdigit
name ='zhangyajie1324'
print(name.isalpha()) #Determine whether the string is composed of letters.
print(name.isalnum()) #Determine whether the string is composed of letters or numbers
print(name.isdigit()) #Determine whether the string is composed of numbers.