Introduction to basic types
2.1 Numeric int.
Numbers are mainly used for calculation, and there are not many ways to use them, just remember one:
2.2 String str stores a small amount of data
'alex' ,'1235443543'
'[1,2,3]'
3.3 Boolean value bool
True False
4.4 List List data of various data types, a large amount of data, easy to operate.
['name',True,[]....],
5.5 tupe read-only list
Use () to represent (1, 2, 3)
6.6 Dictionary dict stores a large amount of data, relational data. Store data in the form of key-value pairs
{'name':'old boy',
'name_list':['Reverse teaching material',.....]
'alex':{'age':40,
'hobby':'old_women',
}
},
7.7 set
{'wusir', 'alex', ....}
specific usage
int
i ='python 12th term'
# i1 = i[0] # print(i1) # i2 = i[4] # print(i2) # i3 =i[-1] # print(i3)
slice
(regardless of the head and the butt)
# i4 = i [0: 6] # print (i4) # i5 = i [: 6] # print (i5) python # i6 = i [1: 4] # print (i6) yth # i7 = i [5: ] # print (i7) n12 期 # i8 = i [:] # print (i8) python12 期
step size
# i9 = i[:5:2] The empty space in front is the beginning of the beginning to 5, the step size is 2 # print(i9) pto # i10 = i[4::2] The middle space is the end, the step size is 2 from 4 to the end # print(i10) o1 period # i11 = i[-1:-5:-1] back-to-front step is 1 # print(i11) period 21n # i12 = i[-3:-1] slice from -3 to -2 (regardless of head and tail) # print(i12) 12
Common operations on strings
s = 'laoNANhai'
** capitalize Capitalize the first letter and lowercase the rest.
# s1 = s.capitalize() # print(s1) # Laonanhai
* center is centered. The first parameter determines the size, * means to fill the left and right empty parts
# s2 = s.center(15,"*") # print(s2) #***laoNANhai***
# s3 = s.upper() # s31 er() # print(s3) LAONANHAI # print(s31) laonanhai
PS
# code = 'QAdr'.upper() # your_code = input('Please enter the verification code, case insensitive:').upper() # if your_code == code: # print('Verification successful')
*** startswith : Determine what content to start with, return bool, can be sliced, used for slices, separated
endswith :
startswith(self, prefix, start=None, end=None)
# s4 = s.startswith('l') # s41 = s.startswith('lao') # s42 = s.startswith('N', 3, 6) # print(s42) True # print(s4) True # print(s41) False
* swapcase : case flip
# s5 = s.swapcase() # print(s5)
* title : Capitalize the first letter of each non-letter-separated word
# ss = 'gdsj wusir6taibai*ritian' # s6 = ss.title() # print(s6)
# s7 = s.find('a') # s71 = s.find('A',2,) 2 Find from the second # s71 = s.find('Q',) # print(s71) # s72 = s.index('Q') # print(s72) # print(s71) # ss = '\talex\n' # print(ss)
# ss = 'ablabsexsba' # s9 = ss.strip('a') # s9 = ss.strip('abs') # lstrip() strips from the left # rstrip() strips from the right # print(s9)
ps prevents users from swiping to enter spaces or other whitespace characters
username = input( ' Please enter your username ' ).strip() if username == ' Wanrong ' : print ( ' Login successful ' )
# s = 'wusir alex taibai' # st = 'wusir,alex,taibai' # st1 = 'QwusirQalexQtaibai' # s10 = s.split() # print(s10) # s101 = st.split(',') # print (s101) # s102 = st1.split('Q',2) 2 means the first two Q's to split # print(s102)
*** join list ---> str in some cases
# s = 'alex' # s11 = '+'.join(s) # print(s11) # l = ['wusir', 'alex', 'taibai'] # s111 = ' '.join(l) # print(s111,type(s111))
**repalce replace the specified character
replace(self, old, new, count=None)
# 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', 'large Hammer', 2) The number 2 means to replace only the first two characters # print(s12)
public method
s = 'fdsajlskgjdsaf;jdskfsdaf'
len() total number
# print(len(s))
count ('') counts the number of occurrences of certain elements, which can be sliced
# c1 = s.count('f') # print(c1)
format formatted output
three ways
# msg = 'My name is {}, this year {}, my hobby {}'.format('Taibai', '20', 'girl') # print(msg)
# msg = 'My name is {0}, this year is {1}, my hobby is {2}, my name is still {0}'.format('Taibai', '20', 'girl') # print(msg)
# msg = 'My name is {name}, this year is {age}, and my hobby is {hobby}'.\ # format(name='Taibai', hobby='girl', age='20') # print(msg)
Judge sentences
# name='jinxin123' # print(name.isalnum()) #The string consists of letters or numbers True # print(name.isalpha()) #The string consists of only letters False # print(name.isdigit()) # string only consists of numbers False
PS