Summary of common Python data type conversion functions

Source: Internet
Author: User
1. chr (i)
The CHR () function returns a string corresponding to the ASCII code.
Copy CodeThe code is as follows:

>>> Print Chr (65)
A
>>> Print Chr (66)

>>> print Chr (+CHR) (66)
Ab


2, complex (Real[,imaginary])
The complex () function converts a string or number to a plural.
Copy CodeThe code is as follows:


>>> Complex ("2+1j")
(2+1j)
>>> Complex ("2")
(2+0J)
>>> Complex (2,1)
(2+1j)
>>> Complex (2l,1)
(2+1j)


3, float (x)
The float () function converts a number or string into a floating point.
Copy CodeThe code is as follows:

>>> Float ("12")
12.0
>>> Float (12L)
12.0
>>> Float (12.2)
12.199999999999999


4, Hex (x)
The hex () function converts integers to hexadecimal numbers.
Copy CodeThe code is as follows:

>>> Hex (16)
' 0x10 '
>>> Hex (123)
' 0x7b '


5, Long (X[,base])
The long () function converts numbers and strings to grow integers, and base is an optional cardinality.
Copy CodeThe code is as follows:

>>> Long ("123")
123L
>>> Long (11)
11L


6. List (x)
The list () function converts a sequence object to a list. Such as:
Copy CodeThe code is as follows:

>>> list ("Hello World")
[' H ', ' e ', ' l ', ' l ', ' o ', ' ', ' w ', ' O ', ' r ', ' L ', ' d ']
>>> list ((1,2,3,4))
[1, 2, 3, 4]


7, int (x[,base])
the int () function converts numbers and strings into an integer, base is an optional cardinality.
Copy CodeThe code is as follows:

>>> Int (3.3)
3
>>> Int (3L)
3
>>> Int ("13")
13
>>> Int ("14", 15)
19


8. Min (x[,y,z ...])
The min () function returns the minimum value of the given parameter, which can be a sequence.
Copy CodeThe code is as follows:

>>> min (1,2,3,4)
1
>>> min ((2,3,4))
(1, 2, 3)


9. Max (X[,y,z ...])
The max () function returns the maximum value for a given parameter, which can be a sequence.
Copy CodeThe code is as follows:

>>> Max (1,2,3,4)
4
>>> Max ((+), (2,3,4))
(2, 3, 4)


10. Oct (x)
The OCT () function converts an integer given to an octal number.
Copy CodeThe code is as follows:

>>> Oct (8)
' 010 '
>>> Oct (123)
' 0173 '


11, Ord (x)
The Ord () function returns the ASCII or Unicode value of a string parameter.
Copy CodeThe code is as follows:

>>> Ord ("a")
97
>>> Ord (U "a")
97


12, str (obj)
The STR () function converts an object into a printable string.
Copy CodeThe code is as follows:

>>> Str ("4")
' 4 '
>>> Str (4)
' 4 '
>>> Str (3+2J)
' (3+2j) '


13, tuple (x)
The tuple () function converts a sequence object to a tuple.
Copy CodeThe code is as follows:

>>> tuple ("Hello World")
(' h ', ' e ', ' l ', ' l ', ' o ', ' ', ' w ', ' O ', ' r ', ' L ', ' d ')
>>> tuple ([1,2,3,4])
(1, 2, 3, 4)


14. Type (x)
Type () can receive anything as a parameter-and return its data type. Integers, strings, lists, dictionaries, tuples, functions, classes, modules, and even type objects can be accepted as parameters by the type function.
Copy CodeThe code is as follows:

>>> Type (1)

>>> li = []
>>> Type (LI)

>>> Import Odbchelper
>>> type (odbchelper)

>>> Import Types
>>> type (odbchelper) = = types. Moduletype
True
  • 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.