Python programming and development-type conversion convert instance analysis, python programming and development
This article describes how to convert the type of python programming to convert. We will share this with you for your reference. The details are as follows:
During python development, you will inevitably encounter type conversion. Here is a common type conversion demo:
Int (x [, base]) converts x to an integer.
Long (x [, base]) converts x to a long integer.
Float (x) converts x to a floating point number.
Complex (real [, imag]) creates a plural number
Str (x) converts object x to a string
Repr (x) converts object x to an expression string
Eval (str) is used to calculate a valid Python expression in a string and return an object.
Tuple (s) converts the sequence s into a tuples
List (s) converts sequence s into a list
Chr (x) converts an integer to a character
Unichr (x) converts an integer to a Unicode Character
Ord (x) converts a character into its integer
Hex (x) converts an integer into a hexadecimal string.
Oct (x) converts an integer into an octal string
Below is my demo:
# Type conversion # convert to intprint ('int (): 'by default, int () print ('str character type conversion to int :', int ('010 ') print ('float floating point type converted to int:', int (234.23) # decimal number 10, corresponding 2-digit, 8-digit, in decimal format, the hexadecimal values are 10, 12, 10, and 0 xaprint ('int (\ '0xa \ ', 16) =', int ('0xa ', 16 )) print ('int (\ '10 \ ', 10) =', int ('10', 10) print ('int (\ '12 \ ', 8) = ', int ('12', 8) print ('int (\ '000000', 2) =', int ('000000', 2 )) # convert to longprint ('int floating point type to int: ', int (23) # convert to floatprint ('float () by default:', float ()) print ('str struct type to float: ', float ('100. 123. 01 ') print ('int floating point type to float:', float (32) # covert to complexprint ('create a plural number (real part + virtual part ):', complex (12, 43) print ('create a complex number (real part + virtual part): ', complex (12) # convert to strprint ('str () by default: ', str () print ('float character type to str:', str (232.33) print ('int floating point type to str: ', str (32 )) lists = ['A', 'B', 'E', 'C', 'D', 'a'] print ('list list converted to str :', ''. join (lists) # covert to liststrs = 'hongten 'print ('sequence strs to list:', list (strs )) # covert to tuple print ('list list to tuple: ', tuple (lists) # conversion between characters and integers # char coverted to intprint ('integer to character chr: ', chr (67) print ('character chr to integer:', ord ('C') print ('integer to hexadecimal number :', hex (12) print ('integer to octal number: ', oct (12 ))
Running effect:
Python 3.3.2 (v3.3.2: d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32Type "copyright", "credits" or "license () "for more information. >>> ================================== RESTART ======== ============================================>> int () by default, it is converted from 0str numeric to int: 10float floating point to int: 234int ('0xa ', 16) = 10int ('10', 10) = 10int ('12', 8) = 10int ('100', 2) = 10int float type to int: 23 float (). By default, the float type is 0.0str float: 123.01int floating point type to float: 32.0 create a plural number (real part + virtual part): (12 + 43j) create a plural number (real part + virtual part): (12 + 0j) str () by default, the float character type is converted to str: 232.33int floating point type to str: 32 list to str: abecda sequence strs to list: ['h', 'O ', 'N', 'G', 't', 'E', 'n'] list conversion to tuple: ('A', 'B', 'E ', 'C', 'D', 'A') convert integer to character chr: c character chr to integer: 67 integer to hexadecimal number: 0xc integer to octal: 0o14 >>>
I hope this article will help you with Python programming.