Saturday, October 25, 2025
HomeLanguagesConvert String to Double in Python3

Convert String to Double in Python3

Given a string and our task is to convert it in double. Since double datatype allows a number to have non -integer values. So conversion of string to  double is the same as the conversion of string to float 

This can be implemented in these two ways

1) Using float() method

Python3




str1 = "9.02"
print("This is the initial string: " + str1)
 
# Converting to double
str2 = float(str1)
print("The conversion of string to double is", str2)
 
str2 = str2+1
print("The converted string to double is incremented by 1:", str2)


Output:

This is the initial string: 9.02
The conversion of string to double is 9.02
The converted string to double is incremented by 1: 10.02

2) Using decimal() method: Since we only want a string with a number with  decimal values this method can also be used

Python3




from decimal import Decimal
 
 
str1 = "9.02"
print("This is the initial string: " + str1)
 
# Converting to double
str2 = Decimal(str1)
print("The conversion of string to double is", str2)
 
str2 = str2+1
print("The converted string to double is incremented by 1:", str2)


Output:

This is the initial string: 9.02
The conversion of string to double is 9.02
The converted string to double is incremented by 1: 10.02
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS