1. Resident mechanism :
Resident mechanism ( Interactive mode ): The method of saving only one copy of the same and immutable string , Different values are stored in the resident pool , The subsequent creation of the same string will not open up new space , But directly assign an address 1. The length of the string is 0 perhaps 1 when 2. A string that matches the identifier ( Letter , Numbers , Underline composition ) 3.[-5,256] An integer number between
2. String query operation :
#index and rindex Throw when it doesn't exist error
#find And rfind Return... When not present -1
s='hello,hello'
print(s.index('lo'))#3, Be careful index The English stop sign is in front
print(s.rindex('lo'))#rindex: For the last time lo The location of
#print(s.index('m')) #value error
print(s.find('lo'))
print(s.rfind('lo'))#rfind: For the last time lo The location of
print(s.find('m'))# -1
3. String case conversion :
s='hello,pytHon'
print(s.upper())# After being capitalized, a new string object will be generated
print(s.lower())# After being converted to lowercase, a new string object will be generated
print(s.swapcase())# Big, small , Small to large
print(s.capitalize())# Capitalize the first character , The rest are lowercase
print(s.title())# Turn the first word of each word into capital , Others are all lowercase
HELLO,PYTHON
hello,python
HELLO,PYThON
Hello,python
Hello,Python
4. String alignment operation :
s='hello,python'
print(s.center(20,'*'))# Align center , The first parameter is the width , The second parameter is the filler
print(s.center(20))# If there is no second parameter, the default space
print(s.center(10,'*'))# When the width is less than the original parameter, align all outputs directly to the left
print(s.ljust(20,'*'))# Align left
print(s.ljust(10,'*'))
print(s.ljust(20))
print(s.rjust(20,'*'))# Right alignment
print(s.zfill(20))# Right alignment , Use 0 fill
print(s.zfill(10))# Returns the original string
print('-8910'.zfill(6))
5. Splitting operation of string :
#split() And rsplit( Split right )
s='hello world python'
print(s.split())# The default split symbol is space
s1='hello|world|python'
print(s1.split())
print(s1.split(sep='|'))# Split symbol is |
print(s1.split(sep='|',maxsplit=1))# Only once
6. How to judge the string :
s1='hello,world'
print(s1.isidentifier())# Determine whether it is a legal string
print('\t'.isspace())# Determine whether the string consists of white space characters
print('abc'.isalpha())# Determine whether the string is composed of letters
print('123'.isdecimal())# Determine whether the string is made up of decimal numbers
print('123 Four '.isnumeric())# Determine whether a string consists of numbers
print('123abc'.isalnum())# Determine whether a string consists of letters and numbers
7. String replacement and merging :
s='hello,python'
print(s.replace('python','java'))
s1='hello,python,python,python'
print(s1.replace('python','java',1))# The first string to be replaced , The second string to be replaced , The third maximum number of replacements
lst=['hello','java','python']
print('|'.join(lst))
print(''.join(lst))
t=('hello','java','python')
print(''.join(t))
print('*'.join(t))
8. String comparison operation :
print('apple'>'app')#True
print('apple'>'banana')#False
print(ord('a'),ord('b'),ord(' Yang '))
print(chr(97),chr(98),chr(26472))
#== Compare value is Compare id
9. String slicing operation ;
s='hello,world'
s1=s[0:5]
s2=s[6:]
s3=s1+'!'+s2
print(s3)
print(s[::-2])# Negative numbers indicate reverse order
print(s[-5::1])
10. Formatted string :
#1. % Place holder
name=' Zhang San '
age=20
print(' My name is %s, This year, %d year ' %(name,age))
print(' My name is '+name+' This year, '+str(age)+' year ')
#2.{} Place holder
print(' My name is {0}, This year, {1} year '.format(name,age))
#3.fstring
print(f' My name is {name}, This year, {age} year ')
print('%10d' %99)#10 Width
print('%.3f' %3.1415926)#.3 It means three digits after the decimal point
print('%10.3f' %3.1415926)# The total width is 10, Three digits after the decimal point
print('{0:.3}'.format(3.141592))#.3 Express 3 digit
print('{:.3f}'.format(3.141592))#.3f Express 3 Decimal place
print('{:10.3f}'.format(3.141592))# The total width is 10, Three digits after the decimal point
11. Character encoding and decoding :
# code
s=' Horizon time '
print(s.encode(encoding='GBK'))#GBK in , One Chinese takes two bytes
print(s.encode(encoding='UTF-8'))#UTF-8 in , One Chinese takes up three bytes
# decode
#byte For binary ( Byte type )
byte=s.encode(encoding='GBK')
print(byte.decode(encoding='GBK'))