python basic data types

Basic data types of python

int type

1  #    int   
2  #    Convert the string to a number   
3  #    a='123a' cannot be changed   
4 a = " 123 "   
5 b = int(a)  
 6  print (b)  
 7  print (type(b))   #View    an element The type of   
8    
9  #The    ascii value is converted here, if it is more than one character, it cannot be converted   
10 a = ' b '   
11 b = int(b)  
 12  print (b)     
 13     
14  #Binary    conversion, convert to 2 system,   
15 num = "0011 "   
16 v = int(num, base=2 )  
 17  print (v)  
 18    
19  #The    binary of the current number, at least several bits are used to represent   
20 age = 10  
 21 r = age.bit_length()  
 22  print (r)

range method

1  #Create    consecutive numbers, from 7 to 2, subtracting 2,, 7, 5, 3 each time   
2 v = range(7, 1, -2)   #   -2 is the step size   
3  for i in v:  
 4      print ( i)

Common String Methods

test= " hello world "   
#    replace, replace ("aa", 'b'), aa is replaced by b, and a few can be replaced at the end   
#    commonly used join, split, find, strip, upper, lower, replace must know  
test = ' You are the wind and I am the sand '   
print (test)  
t = ' * '   
v = t.join(test)  
 print (v)   #There is one more space in each  

result
You are the wind and I am the sand --> you *is* the wind*er *I *is* the sand

split split string


#Split    string   # split(self, sep=None, maxsplit=-1)   
test = " absadfsfff " #All    split, if you add parameters later, it will be split several times   
v2 = test.split( ' s ' )    #According     to s split   print (v2, type(v2))    



result:
['ab', 'adf', 'fff'] <class 'list'> 
View Code

find finds a substring

#Find    the position where the substring appears for the first time, and there is another rfind that searches from the back to the front, and returns -1 if it is not found   
# find(self, sub, start=None, end=None)   
test = " hello "   
v = test.find( ' lo ' )  
 print (v)  
View Code

 

replace

#The    last parameter is the number of replacements   
# replace(self, old, new, count=None)   
test = " hello "   
v = test.replace( " ll " , ' tt ' )  
 print (v   
 
) #result   #    hetto  
View Code

 

strip removes whitespace, or other characters

lstrip(self, chars= None)  
 #Remove left and right blanks 
,    including newlines   #    Or remove specified characters   
test = ' \nAlex '   
l_v = test.lstrip()  
r_v = test.rstrip()  
v = test.strip()  
 print (l_v, r_v, v)  

result:
Alex    
Alex Alex  
View Code

 

upper and lower to uppercase or lowercase

test = " hello "   
v = test.upper()  
 print (v)   #all uppercase   
v = test.lower ()  
 print (v)   #all lowercase
View Code

 

slice

 
#Get    a certain character in the string, some use slice   
test = " abcde "   
v = test[0]  
v2 = test[1:2]   #Exclude    2   
v3 = test[:-1]   #The    first   
#string    length   after 
v4 = len(test)  
  
#String    once created cannot be modified,   
#Once    modified or spliced, a new string will be generated (open up new space), many languages ​​are  
View Code

Other ways to learn

# # Case conversion   
# test = "hello WORLD"   
# v = test.swapcase()   
# print(v).... There are many more, see its source code   
# Determine    whether the    string contains only letters or numbers, 
#whether Is a letter and a Chinese character, is a number islower is a lowercase, 
# isalnum(), isalpha, isdecimal isdigit isidentifier islower , 
# isnumeric isprintable isspace istitle isupper
View Code

 

Guess you like

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