Python type conversion Convert instance analysis

Source: Internet
Author: User
Tags ord

In the development process of Python, it is unavoidable to encounter type conversion, here is a common type conversion demo:

type Description
int (x [, Base]) Convert x to an integer
Long (x [, Base]) Convert x to a long integer
Float (x) Convert x to a floating-point number
Complex (real [, Imag]) Create a complex number
STR (x) Convert an object x to a string
REPR (x) Convert an object x to an expression string
eval (str) Used to evaluate a valid Python expression in a string and return an object
Tuple (s) Converting a sequence s to a tuple
List (s) Convert the sequence s to a list
Chr (x) Converts an integer to one character
UNICHR (x) Converts an integer to a Unicode character
Ord (x) Converts a character to its integer value
Hex (x) Converts an integer to a hexadecimal string
Oct (x) Converts an integer to an octal string

Here is the demo I made:

#Type Conversions#Convert#Convert to intPrint('Int () is by default:', int ())Print('the str character type is converted to int:', Int ('010'))Print('Float floating-point conversion to int:', Int (234.23))#decimal Number 10, corresponding to 2 binary, 8 binary, 10 binary, 16 binary is: 1010,12,10,0xaPrint('int (\ ' 0xa\ ', +) =', Int ('0xa', 16))Print('int (\ ' 10\ ', ten) =', Int ('Ten', 10))Print('int (\ ' 12\ ', 8) =', Int (' A', 8))Print('int (\ ' 1010\ ', 2) =', Int ('1010', 2))#Convert to LongPrint('int floating-point conversion to int:', Int (23))#Convert to floatPrint('float () is by default:', float ())Print('the str character type is converted to float:', Float ('123.01'))Print('int floating-point conversion to float:', Float (32))#Covert to ComplexPrint('Create a complex number (real + imaginary):', Complex (12, 43))Print('Create a complex number (real + imaginary):', Complex (12))#Convert to StrPrint('str () is by default:', str ())Print('float character type converts to Str:', str (232.33))Print('int floating-point conversion to STR:', str (32)) Lists= ['a','b','e','C','D','a']Print('list of lists converted to STR:',"'. Join (lists))#Covert to ListSTRs ='Hongten'Print('sequence STRs converted to list:', List (STRs))#Covert to tuplePrint('list conversion to tuple:', tuple (lists))#conversion between a character and an integer#char coverted to intPrint('convert integers to character chr:', Chr (67))Print('the character Chr is converted to an integer:', Ord ('C'))Print('integer to 16 binary number:', Hex (12))Print('Integer to 8 binary number:', Oct (12))

Operating effect:

Python 3.3.2 (V3.3.2:d047928ae3f6, May, 00:03:43) [MSC v.1600 32bit (Intel)] on Win32type"Copyright","credits" or "license ()"  forMore information.>>> ================================ RESTART ================================>>>int () By default: The 0str character type is converted to int:10Float floating-point conversion to int:234Int ('0xa', 16) = 10Int ('Ten', 10) = 10Int (' A', 8) = 10Int ('1010', 2) = 10int floating-point conversion to int:23float () is by default:0.0the str character type is converted to float:123.01int floating-point conversion to float:32.0Create a complex number (the real part+ Imaginary part): (12+43j) to create a complex number (the real part+ Imaginary part): (12+0j) str () By default: Float character type is converted to str:232.33int floating-point conversion to STR:32list conversion to STR:ABECDA sequence STRs convert to list: ['h','o','N','g','T','e','N'] List is converted to a tuple: ('a','b','e','C','D','a'the integer is converted to the character Chr:c character Chr to an integer:67integer to 16 binary number:0xcInteger to 8 decimal number: 0o14>>>

Python type conversion Convert instance analysis

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.