Basic data types of Python (1)

  Python data types

  The basic data types in Python3 include: number (int), string (str), boolean value (bool), list (list), tuple (tuple), dictionary (dict), etc. All data can be passed through type() function to query its data type. Type a data type (int, str, bool, list, tuple, dict, etc.) on the Python command line and hold down the Ctrl key while clicking the left button to query all usages of the data type.

  ♪ plastic magic (int)

  def bit_length(self): When the current string is expressed in binary form, at least a few bits are required, for example:  

1 num = 256
2 n1 = num.bit_length()
3 print(n1)
4 9

  ♪ string magic (str_1)

  def capitalize(self): Make the first letter uppercase, for example:  

1 str1 = 'alex
2 s1 = str1.ca
3 print(s1)   
4 Alex

  def casefold(self): Convert all letters in the string to lowercase, for example:  

1 str1 = 'ale
2 s2 = str1.c
3 print(s2)  
4 alex

  def lower(self): the same as casefold(), but lower() is only useful for English letters. It will cause errors in case conversion of other languages ​​such as French, German, etc., for example:  

1 str1 = 'ale
2 s2 = str1.c
3 print(s2)  
4 alex

  def center(self, width, fillchar=None): Set the length of the string and display the string in the center. fillchar is the filling value of the vacancy in the specified string. The default is a space, or it can be set manually, for example:

1 str1 = 'alex'
2 s3 = str1.center(30, '#')
3 print(s3) 
4 #############alex#############
1 str1 = 'alex'
2 s3 = str1.center(30, '$')
3 print(s3) 
4 $$$$$$$$$$$$$alex$$$$$$$$$$$$$

  def count(self, sub, start=None, end=None): the number of occurrences of a substring in the current string, start is the starting position of the search range, the default value is 0, and end is the search range As of now, the default is the entire string, for example:

1 str2 = 'Hello,my name is Charles Zhou!'
2 v1 = str2.count('a')                   
3 print(v1)       
4 2                       

  def endswith(self, suffix, start=None, end=None): Determines whether the string starts with a substring, and returns the boolean value True or False. E.g:

1 str2 = 'Hello,my name is Charles Zhou!'
2 v2 = str2.startswith("He")             
3 print(v2)                              
4 True
str2 = 'Hello,my name is Charles Zhou!'
v2 = str2.startswith("he")             
print(v2)         
False                     

  def startswith(self, suffix, start=None, end=None): Determines whether the string ends with a substring, and returns the boolean value True or False. E.g:

1 str2 = 'Hello,my name is Charles Zhou!' 
2 v3 = str2.endswith("ou!")               
3 print(v3)                      
4 True         
1 str2 = 'Hello,my name is Charles Zhou!' 
2 v3 = str2.endswith("ou")               
3 print(v3)
4 False                               

  def find(self, sub, start=None, end=None): Find a substring in a string and return the position of the substring, if the substring does not exist, return -1. E.g:

1 str2 = 'Hello,my name is Charles Zhou!'
2 v4 = str2.find('a')                    
3 print(v4) 
4 10                             
str2 = 'Hello,my name is Charles Zhou!'
v4 = str2.find('z')                    
print(v4)                              
-1

  def index(self, sub, start=None, end=None): Find a substring in a string and return the position of the substring. When the substring does not exist, the code directly reports an error. E.g:

1 str2 = 'Hello,my name is Charles Zhou!'
2 v4 = str2.index('n')                   
3 print(v4)        
4 9                      
1 str2 = 'Hello,my name is Charles Zhou!'
2 v4 = str2.index('z')                   
3 print(v4)                              
4 ValueError: substring not found

  def format(self, *args, **kwargs): Replace the placeholders in the string with the specified value. E.g:

1 str3 = 'Hello,I\'m {name},I\'m {age}!'   
2 v5 = str3.format(name = "Alex", age = 26)
3 print(v5) 
4 Hello,I'm Alex,I'm 26!                            
1 str4 = 'Hello,I\'m {0},I\'m {1}!'
2 v5 = str4.format( "Alex",26)     
3 print(v5)                        
4 Hello,I'm Alex,I'm 26!

  It is worth noting that if you do not specify a name, the placeholder must start with 0 , otherwise the system will report an error, for example:

1 str4 = 'Hello,I\'m {1},I\'m {2}!'
2 v5 = str4.format( "Alex",26)
3 print(v5)
4 
5 IndexError: tuple index out of range
6 Process finished with exit code 1

  def format_map(self, mapping): Assign a value to a placeholder in a string in the form of an index (dictionary) . E.g:

1 test = 'I am {name},age {a}'
2 v4 = test.format_map({"name":'df',"a":26})
3 print(v4)
4 I am df,age 26

  To be continued...

 

Guess you like

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