The string and number are two popular data or variable types used in Python. While working with these types we may require to convert a string into the number type. Because the string type consists of characters and can not be used for mathematical calculations. But the number type can be used for different mathematical calculations. The number type can an integer or floating-point number. In this tutorial, we learn how to convert the string into a number in Python.
Python Numbers or Numeric Types (Integer, Floating Point, Complex Numbers)
Before learning to convert string to numeric types we should learn what are numeric types are. Python provides 3 numeric types name integer, floating point and complex numbers.
- integer type is a decimal number which do not contains floating point.
- floating point type is used to store decimal and floating point part of a number.
- complex numbers are special numbers which are not used in daily life.
Convert String To Integer
String type can be converted into the integer type by using the int() method. The string should be consist of only numeric characters. If it contains non-numeric characters the conversion throws an exception.
a="3"
b="5"
x = int(a) + int(b)
print(x)
8
If the string contains spaces with numeric characters these spaces are trimmed or removed automatically which do not prevents the string to int conversion.
a=" 3"
b="5 "
x = int(a) + int(b)
print(x)
8
The string to number conversion with the int() method is not perfect. If the string contains non-numeric characters except spaces the int() method throws an exception. The exception is “ValueError: invalid literal for int() with base 10:” which means the given string literal is invalid as decimal type.
a="3g"
b = int(a)

If the provided string is an floating point the conversion throws exception too. As the dot character is used for floating point and using it with the int() method is an error. We can not used the int() method to convert floating point literals to floating point type.
a = "3.0"
b = "5.5"
c = int(a)
d = int(b)

Convert String To Floating Point
Floating point is another popular numeric type. The float() method is used to convert string to the floating point type. The dot character is used to split decimal and floating point part.
a = "3.0"
b = "5.5"
c = float(a)
d = float(b)
The float() method also converts the string which is not provided as floatin point format. This means there is no need for dot where it may look like an integer but by using the float() method it is converted as floating point.
a = "3"
b = "5"
c = float(a)
print(c)
d = float(b)
print(d)
3.0 5.0
Like the int() method if the string provides non-numeric characters except space and dot the float() method also throws an exception. The exception is “ValueError: could not convert string to float:“.
a = "3.4df"
b = "5.rr"
c = float(a)
d = float(b)
Convert String To Complex Number
Complex numbers are special numbers which is not used in daily life. The complex numbers are related with mathematical theory. The complex() method can be used to convert strings which are complex numbers into the complex number type.
a = "6+4j"
b = "8+9j"
x = complex(a)
print(x)
y = complex(b)
print(y)
(6+4j) (8+9j)
If the provided string is not in a complex number format the “ValueError: complex() arg is a malformed string” exception is returned by the complex() method.
a = "6+4jee"
b = "8+ 9 j"
x = complex(a)
y = complex(b)
