Conversions between Python data types

Source: Internet
Author: User
Tags ord string format

When converting to Python's built-in data types, you can use built-in functions, which are commonly used for type conversion functions

python common type conversion functions
function format Using the example Describe
int (x [, Base]) Int ("8") Can be converted to include string types and other numeric types, but will lose precision
Float (x) Float (1) or float ("1") You can convert string and other numeric types, and the insufficient number of digits is 0, for example 1 becomes 1.0
Complex (real, imag) Complex ("1") or complex The first argument can be a string or a number, the second argument can only be a numeric type, and the second parameter is not 0 by default.
STR (x) STR (1) Convert a number to a string
REPR (x) Repr (Object) Returns the string format of an object
eval (str) Eval ("12+23") Executes a string expression that returns the result of the calculation, as shown in the example 35
Tuple (SEQ) Tuple ((1,2,3,4)) A parameter can be a tuple, a list, or a dictionary, a collection of key components that return a dictionary when Wie
List (s) List ((1,2,3,4)) Converts a sequence into a list of parameters that can be a tuple, dictionary, list, or dictionary, which returns a collection of key dictionaries
Set (s) Set ([' B ', ' R ', ' U ', ' o ', ' n '), or set ("ASDFG") Transforms an iterative object into a mutable set, and repeats, returning the result that can be used to calculate the difference x-y, and the set × | Y, intersection X & y
Frozenset (s) Frozenset ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) Transforms an iterative object into an immutable set, with parameters such as tuples, dictionaries, lists, and so on.
Chr (x) Chr (0x30) Chr () returns a corresponding character by using an integer (that is, 0~255) in range (256) as a parameter. The return value is the ASCII character corresponding to the current integer.
Ord (x) Ord (' a ') Returns the corresponding ASCII value, or the Unicode value
Hex (x) Hex (12) Convert an integer to a hexadecimal string
Oct (x) Oct (12) Convert an integer to an octal string

Conversions between Python data types

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.