basic data type

1.str

The index of the string.

The index is the subscript, starting from the first 0 in the string, and so on.

a = 'ABCDEFGHIJK'
print(a[0])
print(a[3])
print(a[5])
print(a[7])

Slicing is to intercept a string by index to form a new string. (Principle of the head regardless of the tail)

 

 
 
a = 'ABCDEFGHIJK'
print(a[0:3])
print(a[2:5])
print(a[ 0 :]) #default to the end
print(a[ 0 :- 1 ]) #- 1 is the last
print(a[ 0 : 5 : 2 ]) #Add step size
print(a[ 5 : 0 :- 2 ]) #reverse step size

String common methods

 
 
# *** upper: all uppercase 
# lower: all lowercase
s = "laoNANhai"
s3 = s.upper()
s31 = s.lower()
print(s3)
print(s31)

#* 居中 center
s = "laoNANhai"
s2 = s.center(27,"*")
print(s2)

# *** startswith : Determine what content starts with, 
# Return bool, which can be sliced, and slices are separated by commas.
# ***endswith
s = 'laoNANhai'
s4 = s.startswith('l')
s41 = s.startswith('lao')
s42 = s.startswith('N', 3, 6)
print(s42)
print( s4)
print(s41)

#***Find the index by element 
# index: find the index by element, can be sliced, can not find an error
# find: find the index by element, can be sliced, can't find return -1
s = "Alex wusir aAQjinxing"
s7 = s .find('a')
print(s7)
s71 = s.find('A',2,)
print(s71)
s72 = s.index('Q')
print(s72)
 
 
#*** strip Remove leading and trailing spaces, newlines, and tabs. 
ss = 'ablabsexsba'
s8 = ss.strip()
print(s8)
s9 = ss.strip('a')
print(s9)
s9 = ss.strip('abs')
print(s9)

# ***split str ---> list #The 
default is to split by spaces,
s = 'wusir alex taibai'
st = 'wusir,alex,taibai'
st1 = 'QwusirQalexQtaibai'
s10 = s.split()
print(s10)
s101 = st.split()
print(s10)
s102 = st1.split('Q',2) #Let the split several times
print(s102)

#***join 
# in some cases, list --- >str
s = 'alex'
s11 = '+'.join(s)
print(s11)
l = ['wusir', 'alex', 'taibai ']
s111 = ''.join(l)
print(s111,type(s111))
 
 
# repalce replace 
s = 'small pink tender little pink tender ghlasdfg small pink tender'
# # s12 = s.replace('small pink tender', 'big hammer')
s12 = s.replace('small pink tender', ' sledgehammer',2) #replace how many
print(s12)

len() calculates the length 

count() calculates how many times an element appears

#format formatted output,
1, 
msg = 'My name is {}, this year {}, hobby {}'.format('Taibai', '20', 'girl')
print(msg)
2. 
msg = 'My name is {0}, this year is {1}, my hobby is {2}, my name is still {0}'.format('Taibai', '20', 'girl')
print(msg)
3. 
msg = 'My name is {name}, this year is {age}, and my hobby is {hobby}'.\
format(name='Taibai', hobby='girl', age='20')
print(msg)
isdigit() determines whether the string is a number 

isalpha() determines whether the string is in English
isalnum() determines whether the string is composed of numbers and English

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324996574&siteId=291194637