How Can We Help?
Python is an interpreting language. It is pretty different from the programming languages like C++ and Java. Any programming language uses a few data types for typecasting, representing values, etc. Although in c++, python, etc., we represent the data types as double, int, long int, long, long int, etc. in python, we only have two data types for numbers. The first is int and the second one is float.
The data type double, which is used in c++, Java, etc., is represented as floating numbers in python. And hence a conversion to double is equivalent to a conversion to float. Note that python does not have inbuilt support for the double data type.
This article will explain how to convert the string into doubles in python. We will use the words double and float interchangeably.
Python str to Double Using the Float Function
is a built-in function of python. We can convert strings into float/doubles using the float function.
Syntax:
float(<the string>)
Return value:
Number with float/double data type
Example (1)
string_number="1234"
print(f"The number in it's string data type representation is as follows: {string_number}")
print(f"The data type of the variable is:{type(string_number)} ")
double_number=float(string_number)
print(f"The number in it's double data type representation is as follows: {double_number}")
print(f"The data type of the variable is:{type(double_number)} ")
Output:
The number in its string data type representation is as follows: 1234
The data type of the variable is:
The number in its double data type representation is as follows: 1234.0
The data type of the variable is:
Explanation:
- First, we created the variable named string_number. We assigned the value to the variable as 1234 but in the form of a string.
- Next, we printed the variable’s value to distinguish it from its floating-point representation later. Notice that it got printed with no decimals.
- We also used another built-in python type function to check the variable’s data type. Python returned the variable’s data type as <class ‘str’>, which confirms that the variable is of string data type.
- Next, we used the float function of python to convert the variable to a floating point number. We again printed the variable’s value; this time, we can observe that the variable’s value has been printed with a decimal.
- Further, we used the type function of python to check the data type of the variable double_number on which we stored the double value representation of the string variable. We see that python returned the variable’s data type as <class ‘float’>, which defines that the value is converted to float/double data type.
We can also convert the string with decimal numbers to a double data type in python.
Example (2)
string_number="566.878"
print(f"The number in it's string data type representation is as follows: {string_number}")
print(f"The data type of the variable is:{type(string_number)} ")
double_number=float(string_number)
print(f"The number in it's double data type representation is as follows: {double_number}")
print(f"The data type of the variable is:{type(double_number)} ")
Output:
The number in its string data type representation is as follows: 566.878
The data type of the variable is:
The number in its double data type representation is as follows: 566.878
The data type of the variable is:
Python str to Double Using Decimal
The Decimal is a slightly advanced form of the double. While the double uses 64 bits for precision, the Decimal uses 128 bits for precision. However, python programmers usually treat the decimal floats and doubles interchangeably. Hence it is worth understanding the conversion to Decimal too. In python, we need to use the decimal function to convert the string into a data type.
Syntax:
Decimal(<The string value>)
Return value:
Decimal number
Example (3)
from decimal import Decimal
str_num = "9.02"
print(f"The number in it's string data type representation is as follows: {str_num}")
print(f"The data type of the variable is:{type(str_num)}")
print(str_num*2)
str_double = Decimal(str_num)
print(f"The number in it's double data type representation is as follows: {str_double}")
print(f"The data type of the variable is:{type(double_number)} ")
print(str_double*2)
Output:
The number in its string data type representation is as follows: 9.02
The data type of the variable is:
9.029.02
The number in its double data type representation is as follows: 9.02
The data type of the variable is:
18.04
Explanation:
- First, we imported the function named Decimal from Decimal. Next, we created the string variable named str_num. We assigned some value to it. We then printed the variable using the print function.
- Next, we used the type function to check the variable’s data type. We also tried to multiply the variable with 2, and we observed that instead of multiplication, concatenation was performed. This confirms that the variable is of string data type.
- Next, we used the Decimal function to convert the data type of the variable str_num to decimal data type. We stored the value in another variable named str_double. We then printed the variable and the variable’s data type using the print and type functions.
- Now we tried to multiply the variable with 2, and we observed that the value got incremented twice. Hence we further confirm that the variable’s data type is changed to decimal data type.
Can we Convert any String into a Double in Python?
A self-evident question that will come to our mind is whether it can convert any string into a double in python. The answer is ‘no’. Python will only convert those strings that have numbers enclosed in them. Even if one character in the string is not a number, the alphabet python will raise an error.
Example (4)
name = "Hello World"
print(type(name))
name_double = float(name)
print(type(name_double))
Output:
ValueError Traceback (most recent call last)
Cell In [10], line 3
1 name = “Hello World”
2 print(type(name))
—-> 3 name_double = float(name)
4 print(type(name_double))ValueError: could not convert string to float: ‘Hello World’
Hence we should keep in mind that we are only eligible to convert the string, which consists of only numbers embedded within it.
Converting string elements to double elements in numpy
Programmers not only deal with simple variables. They also deal with other data types like arrays. Arrays are a list lie objects consisting of data of similar types. In this section, we will understand how to convert the data type of the strings in arrays. Numpy allows us to convert the string into different degrees of precision. Numpy has an extensive set of data types as follows:
Numpy Type | C Type | Description |
---|---|---|
numpy.bool_ | bool | Boolean (True or False) stored as a byte |
numpy.byte | signed char | Platform-defined |
numpy.ubyte | unsigned char | Platform-defined |
numpy.short | short | Platform-defined |
numpy.ushort | unsigned short | Platform-defined |
numpy.intc | int | Platform-defined |
numpy.uintc | unsigned int | Platform-defined |
numpy.int_ | long | Platform-defined |
numpy.uint | unsigned long | Platform-defined |
numpy.longlong | long long | Platform-defined |
numpy.ulonglong | unsigned long long | Platform-defined |
numpy.half / numpy.float16 | Half precision float: sign bit, 5 bits exponent, 10 bits mantissa | |
numpy.single | float | Platform-defined single precision float: typically sign bit, 8 bits exponent, 23 bits mantissa |
numpy.double | double | Platform-defined double precision float: typically sign bit, 11 bits exponent, 52 bits mantissa. |
numpy.longdouble | long double | Platform-defined extended-precision float |
numpy.csingle | float complex | Complex number, represented by two single-precision floats (real and imaginary components) |
numpy.cdouble | double complex | Complex number, represented by two double-precision floats (real and imaginary components). |
numpy.clongdouble | long double complex | Complex number, represented by two extended-precision floats (real and imaginary components). |
Reference: Numpy documentation(https://numpy.org/doc/stable/user/basics.types.html)
We can convert the string into any of the specified data types.
Syntax:
array.astype(<float/double data type>)
Return value:
An array of elements with double/float data type.
Example (5)
import numpy as np
str_num=np.array(['454','67','77.77'])
print(str_num.astype('float64'))
Output:
[454. 67. 77.77]
Conclusion
In this article, we have learned how to convert the string into doubles in python. We have discussed the most critical topics on the same. However, we strongly recommend that users look up the python documentation for the data types and their precisions to understand the topic more.