Type conversion in python, python type conversion
Function Description
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
The sequence supports the following operations:
Python code
Operation description
S + r sequential join
S * n, n * s n copies, n is an integer
S % d string formatting (string only)
S [I] Index
S [I: j] Slice
X in s, x not in s subordination
For x in s: Iteration
Len (s) Length
Min (s) minimum element
Max (s) maximum element
S [I] = x is s [I] re-assigned
S [I: j] = r: returns the list fragment value.
Del s [I] deletes an element from the list.
Del s [I: j] deletes a clip from the list.
Operation description
S + r sequential join
S * n, n * s n copies, n is an integer
S % d string formatting (string only)
S [I] Index
S [I: j] Slice
X in s, x not in s subordination
For x in s: Iteration
Len (s) Length
Min (s) minimum element
Max (s) maximum element
S [I] = x is s [I] re-assigned
S [I: j] = r: returns the list fragment value.
Del s [I] deletes an element from the list.
Del s [I: j] deletes a clip from the list.
Numeric operation:
Python code
X <y shifts left
X> y shift right
X & y
X | y by bit or
X ^ y exclusive or)
~ X flip by bit
X + y plus
X-y Subtraction
X * y Multiplication
X/y
X // y floor Division
X ** y multiplication (xy)
X % y modulo (x mod y)
-X changes the symbol bit of the operand.
+ X does nothing.
~ X ~ X =-(x + 1)
Abs (x) absolute value
Divmod (x, y) returns (int (x/y), x % y)
Pow (x, y [, modulo]) returns (x ** y) x % modulo
Round (x, [n]) Rounding, n is the number of decimal places
X <y is less
X> y is greater
X = y equals
X! = Y is not equal to (same as <>)
X> = y is greater than or equal
X <= y is less than or equal
Type conversion problems in Python
Var is a tuple, such as var = ('Kim ', '23', '123 ')
Name, age, weight = var can be converted
Conversion of date types in python
You can use strptime () and strftime () in the time module ().
Strptime () controls the string interpretation date based on the format you specify,
Strftime () controls the string output date based on the format you specify.
For example, convert "12-Jan-06 10:06:00":
>>> From time import strptime, strftime
>>>
>>> MyDate = '12-Jan-06 10:06'
>>> Parsed = strptime (myDate, '% d-% B-% y % H: % m ')
>>> Converted = strftime ('% Y-% m-% d % H: % M: 00', parsed)
>>>
>>> Converted
'2017-01-12 10:06:00'