Common Python data type conversion function Summary _python

Source: Internet
Author: User

1, Chr (i)
The CHR () function returns the string corresponding to the ASCII code.

Copy Code code 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 Code code 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 floating-point numbers.
Copy Code code as follows:
>>> Float ("12")
12.0
>>> Float (12L)
12.0
>>> Float (12.2)
12.199999999999999

4, Hex (x)
The hex () function converts an integer to a hexadecimal number.
Copy Code code as follows:
>>> Hex (16)
' 0x10 '
>>> Hex (123)
' 0x7b '

5, Long (X[,base])
The long () function converts numbers and strings to grow integers, base as an optional base.
Copy Code code as follows:
>>> Long ("123")
123L
>>> Long (11)
11L

6, List (x)
The list () function converts a sequence object to a list. Such as:
Copy Code code 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 base.
Copy Code code 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 Code code 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 for the given parameter, which can be a sequence.
Copy Code code 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 given to an octal number.
Copy Code code as follows:
>>> Oct (8)
' 010 '
>>> Oct (123)
' 0173 '

11, Ord (x)
The Ord () function returns the ASCII code or Unicode value of a string parameter.
Copy Code code as follows:
>>> Ord ("a")
97
>>> Ord (U "a")
97

12, str (obj)
The STR () function converts an object into a printable string.
Copy Code code as follows:
>>> Str ("4")
' 4 '
>>> Str (4)
' 4 '
>>> Str (3+2J)
' (3+2j) '

13, tuple (x)
The tuple () function converts a sequence object into a tuple.
Copy Code code 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. Integral types, strings, lists, dictionaries, tuples, functions, classes, modules, and even type objects can be accepted as parameters by the type function.
Copy Code code as follows:
>>> Type (1)
<type ' int ' >
>>> li = []
>>> Type (LI)
<type ' list ' >
>>> Import Odbchelper
>>> type (odbchelper)
<type ' module ' >
>>> 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.