Python – How to convert float to String

In Python, we can use str() to convert float to String.


pi = 3.1415

print(type(pi))  # float

piInString = str(pi)  # float -> str

print(type(piInString))  # str

Output


<class 'float'>
<class 'str'>

References

  1. Python docs – str()
  2. Python – How to convert int to String
  3. Python – How to convert String to float

4 comments on “Python – How to convert float to String

  1. c’est une mauvaise technique de faire nombre = str(nombre)

    car si on fait 5219174198521.17495127414 = str(5219174198521.17495127414)
    on obtient 5219174198521.175
    alors que l'on veut '5219174198521.17495127414'
    c'est pour ça que c'est trés problèmatique avec les flottants (float)
    
    Reply
  2. # variable expr1 and its value
    expr1 = 1700/19
    print(“The value of expr1:”, expr1)

    # variable expr2
    expr2 = _1700/19_
    print(“The value of converted into string expr2:”, _ _ _)

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *