Python Code Example: convert specific datatype to int

In Python it is possible to convert data types to other data types. The method int() converts a number or string to an integer, or return 0 if no arguments are given. If x is a number, return x.__int__(). For floating point numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in the given base. The literal can be preceded by ‘+’ or ‘-‘ and be surrounded by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to interpret the base from the string as an integer literal.

Syntax

Here, x can be a number or a string. If x is a number, it can be an integer, a float, or a complex number. If x is a string, it must contain a numeric value, and an optional prefix indicating the base of the number.

int(x)

Code Example

intNumber = 12
floatNumber = 1.2
numStr = '12'
numChar = '4'

a = intNumber + int(floatNumber)
b = int(numStr) + intNumber
c = int(numChar)

print("data types of a, b and c: " + str(type(a)) 
      + str(type(b)) + str(type(c)))
print("value of a " + str(a))
print("value of b " + str(b))
print("value of c " + str(c))

Output

data types of a, b and c: <class 'int'><class 'int'><class 'int'>
value of a 13
value of b 24
value of c 4

Code Explanation

intNumber = 12
floatNumber = 1.2
numStr = '12'
numChar = '4'

These lines define four variables: intNumber is an integer with value 12, floatNumber is a floating-point number with value 1.2, numStr is a string with value '12', and numChar is a string with value '4'.

a = intNumber + int(floatNumber)

In this line, floatNumber is first converted to an integer using the int function, which rounds down to the nearest whole number (in this case, 1). Then, intNumber and the converted floatNumber are added together, and the result is stored in the variable a.

b = int(numStr) + intNumber

In this line, numStr is first converted to an integer using the int function, which returns the integer value of the string (in this case, 12). Then, intNumber and the converted numStr are added together, and the result is stored in the variable b.

c = int(numChar)

In this line, numChar is converted to an integer using the int function, which returns the integer value of the string (in this case, 4). The result is stored in the variable c.

print("data types of a, b and c: " + str(type(a)) 
      + str(type(b)) + str(type(c)))
print("value of a " + str(a))
print("value of b " + str(b))
print("value of c " + str(c))

These lines print the data type and value of the variables a, b, and c. The type function is used to get the data type of each variable, and the str function is used to convert the data type and value to a string so that they can be concatenated with the string "data types of a, b and c: ", "value of a ", and "value of b ". The output shows that all three variables are of type int and have the values 13, 24, and 4, respectively.