Basic data types for Python (1)

Source: Internet
Author: User

 Data types for Python

The basic data types in Python3 include: number (int), string (str), Boolean (bool), list, Ganso (tuple), dictionary (dict), and so on, all data can be queried by the type () function for its data type. You can query all usages of the data type by pressing and holding down the CTRL key while typing the data type (int, str, bool, list, tuple, dict, etc.) on the command line of Python.

? Shaping Magic (int)

  def bit_length (self): When the current string is represented in binary form, at least several representations are required, for example:

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

  ? String Magic (str_1)

  def capitalize (self): sets the initial letter to uppercase, for example:

1 ' Alex 2 s1 = str1.ca3print(S1)   4 Alex

def casefold (self): Converts all letters in a string to lowercase, for example:

1 ' Ale 2 s2 = str1.c3print(S2)  4 Alex

def lower (self): Same as Casefold (), but lower () is only useful for English letters, and for other languages, such as French, German, and so on, the conversion of uppercase and lowercase can produce errors such as:

1 ' Ale 2 s2 = str1.c3print(S2)  4 Alex

Def center (self, Width, fillchar=none): Sets the length of the string and centers the string, Fillchar is the padding value of the vacancy in the specified string, the default is a space, or it can be set artificially, for example:

 1  str1 =  " alex   " 2  s3 = Str1.center ( " #  "  )   3  print   (S3)  4  #  ######## # # # #alex #############  
1 ' Alex ' 2 ' $ ' )3print4 $$$$$$$$$$$$ $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 lookup range, the default value is 0,end is the cutoff for the lookup range, and the default is the entire string, for example:

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

def endswith (self, suffix, start=none, end=none): Determines whether a string starts with a substring, returns a Boolean value of true, or false. For example:

 1  str2 =  " hello,my name is Charles zhou!   " 2  v2 = str2.startswith ( " he  "  )  3  print   (v2)  4  True 
' hello,my name is Charles zhou! '  = Str2.startswith ("he")             print(v2)         False                     

def startswith (self, suffix, start=none, end=none): Determines whether a string ends with a substring, returns a Boolean value of true, or false. For example:

 1  str2 =  " hello,my name is Charles zhou!   " 2  v3 = str2.endswith ( " ou!   " )  3  pri         NT   (V3)  4  True 
1 ' hello,my name is Charles zhou! ' 2 v3 = Str2.endswith ("ou")               3print(v3)  4 False                               

def find (self, sub, Start=none, End=none): Finds a substring in a string and returns the position of the substring, returning 1 when the substring does not exist. For example:

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

def index (self, sub, Start=none, End=none): Finds a substring in a string and returns the position of the substring, and the code directly complains when the substring does not exist . For example:

 1  str2 =  " hello,my name is Charles zhou!   " 2  v4 = str2.index ( " n   " )  3  print   (v4)  4  9 
1 ' hello,my name is Charles zhou! ' 2 v4 = Str2.index ('z')                   3print(v4)                               4  not found

def format (self, *args, **kwargs): Replaces the placeholder in the string with the specified value. For example:

 1  str3 =  " hello,i\ ' m {name},i\ ' m {age}!     " 2  v5 = Str3.format (name =  " alex  " , age = 26 Span style= "COLOR: #008080" >3  print   (v5)  4  hello,i " m alex,i                              '  m 26! 
1 ' hello,i\ ' m {0},i\ ' m {1}! ' 2 " Alex ",     3print(                        V5)4 hello,i' m Alex,i 'm 26!

It is important to note that if you do not specify a name, the placeholder must start at 0 , or the system will error, for example:

1 ' hello,i\ ' m {1},i\ ' m {2}! ' 2 " Alex ",3print(V5)45indexerror:tuple Index out of range6 Process finished with exit code 1

def format_map (self, mapping): Assigns a value to a placeholder in a string, in the form of an index (dictionary) . For example:

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

To be Continued ...

Basic data types for Python (1)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.