Data Type Conversion in Python


Sometimes, you may need to perform conversions between the built-in types. To convert between types, you simply use the type name as a function.

There are several built-in functions to perform conversion from one data type to another. These functions return a new object representing the converted value.

Sr.No.Function & Description
1int(x [,base])
Converts x to an integer. base specifies the base if x is a string.
2long(x [,base] )
Converts x to a long integer. base specifies the base if x is a string.
3float(x)
Converts x to a floating-point number.
4complex(real [,imag])
Creates a complex number.
5str(x)
Converts object x to a string representation.
6repr(x)
Converts object x to an expression string.
7eval(str)
Evaluates a string and returns an object.
8tuple(s)
Converts s to a tuple.
9list(s)
Converts s to a list.
10set(s)
Converts s to a set.
11dict(d)
Creates a dictionary. d must be a sequence of (key,value) tuples.
12frozenset(s)
Converts s to a frozen set.
13chr(x)
Converts an integer to a character.
14unichr(x)
Converts an integer to a Unicode character.
15ord(x)
Converts a single character to its integer value.
16hex(x)
Converts an integer to a hexadecimal string.
17oct(x)
Converts an integer to an octal string.

Updated on: 24-Jan-2020

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements