Summary of common python data type conversion functions

Source: Internet
Author: User
This article mainly introduces the commonly used python data type conversion functions, and illustrates the usage of these functions with actual examples. For more information, see 1. chr (I)
The chr () function returns the string corresponding to the ASCII code.

The code is as follows:

>>> Print chr (65)
A
>>> Print chr (66)

>>> Print chr (65) + chr (66)
AB


2. complex (real [, imaginary])
The complex () function converts a string or number to a plural number.

The 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 to a floating point number.

The code is as follows:

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


4. hex (x)
The hex () function converts an integer to a hexadecimal number.

The code is as follows:

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


5. long (x [, base])
The long () function converts numbers and strings into integers, and the base is an optional base.

The code is as follows:

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


6. list (x)
The list () function converts a sequence object to a list. For example:

The 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 the optional base.

The 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 a given parameter, which can be a sequence.

The code is as follows:

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


9. max (x [, y, z...])
The max () function returns the maximum value of a given parameter, which can be a sequence.

The code is as follows:

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


10. oct (x)
The oct () function converts an integer to an octal number.

The code is as follows:

>>> Oct (8)
'0'
>>> Oct (123)
'123'


11. ord (x)
The ord () function returns the ASCII or Unicode value of a string parameter.

The code is as follows:

>>> Ord ("")
97
>>> Ord (u "")
97


12. str (obj)
The str () function converts an object to a printable string.

The 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.

The 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. Integer, string, list, dictionary, tuples, functions, classes, modules, and even type objects can be accepted as parameters by type functions.

The 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.