This example describes the type conversion of Python programming development. Share to everyone for your reference, as follows:
In the development process of Python, it is unavoidable to 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 complex number
STR (x) converts an object x to a string
REPR (x) converts an object x to an expression string
Eval (str) is used to calculate a valid Python expression in a string and returns an object
Tuple (s) converts a sequence s to a tuple
List (s) converts 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:
#类型转换 #convert#convert to Intprint (' int () By default: ', int ())) print (' str character type converted to int: ', int (' 010 ')) print (' float floating-point conversion to int: ', Int (234.23)) #十进制数10, corresponding to 2 binary, 8 binary, 10 binary, 16 binary is: 1010,12,10,0xaprint (' int (\ ' 0xa\ ', +) = ', int (' 0xa ', +)) print (' int ' 10\ ', ten) = ', int (' Ten ', ') ') print (' int (\ ' 12\ ', 8) = ', int (' n ', 8)) print (' int (\ ' 1010\ ', 2) = ', int (' 1010 ', 2)) #convert to Longprint (' int ' floating-point conversion to int: ', int (at all)) #convert to Floatprint (' float () By default: ', float ()) print (' str character type converted to float: ', Float (' 123.01 ')) print (' int floating-point conversion to float: ', float (+)) #covert to Complexprint (' Create a complex number (real + imaginary): ', complex (+)) print ( ' Create a complex number (real + imaginary part): ', Complex ()) #convert to Strprint (' str () By default: ', str ()) ' Print (' float ' character type to str: ', str (232.33)) print (' int floating-point conversion to STR: ', str (+)) lists = [' A ', ' B ', ' e ', ' C ', ' d ', ' A ']print (' list conversion to str: ', ' ". Join (lists)) #covert to Liststrs = ' Hongten ' Print (' sequence STRs converted to list: ', List (STRs)) #covert to tuple print (' list conversion to tuple: ', tuple (lists)) #字符和整数之间的转换 # Char coverted to Intprint (' integer converted to character Chr: ', CHR) print (' character CHR converts to an integer: ', ORD (' C ')) print (' int to 16 decimal: ', Hex ()) print (' int to 8 decimal: ', Oct (12))
Operating effect:
Python 3.3.2 (V3.3.2:d047928ae3f6, May, 00:03:43) [MSC v.1600 + bit (Intel)] on Win32type "copyright", "credits" or "license ()" For more information.>>> ================================ RESTART =========================== =====>>> int () By default: 0str character conversion to int:10float floating-point conversion to Int:234int (' 0xa ', +) = 10int (' + ') = 10int (' + ', 8) = 10i NT (' 1010 ', 2) = 10int floating-point conversion to Int:23float () by default: 0.0str character conversion to float:123.01int floating-point conversion to float:32.0 create a complex number (real + imaginary): (12+43j) Create a complex number (real + imaginary): (12+0j) str () By default: float character conversion to str:232.33int floating-point conversion to str:32 list 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 ') integer converted to a character Chr:c character Chr converted to an integer: 67 integer to 16 binary: 0xc int to 8 in number: 0 O14>>>
I hope this article is helpful for Python program design.