Convert String to Float & Float to String in Python

Convert String to Float & Float to String in Python

Convert string to float and float to string in python; In this tutorial, you will learn two simple methods to convert string to float and convert float to string in python.

You can use the built-in float method to convert the string into float number in python. And You can use the built-in string str() function of Python to convert float numbers to string in python.

How to convert string to float and float to string in python

  • The python float() is a standard built-in function to convert the string into a float value.
  • The python str() is a standard built-in function to convert the integer, float number to a string value.

Python Convert String to float

You can convert a string to float in Python using a python built-in float() method. Internally float() function calls specified object __float__() function.

Let’s look at a simple example to convert a string to float in Python.

s = '10.5674'

f = float(s)

print(type(f))
print('Float Value =', f)

Output:

<class 'float'>
Float Value = 10.5674

Python program to convert a string to float:

If you have a string in python and you want to convert it to float number. So you can use python built-in function float() to convert string to floating number in python.

Python program to convert string to float:

num = "3.1415"

print(num)
print(type(num))  # str

pi = float(num)  # convert str to float

print(pi)
print(type(pi))  # float

Output

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

Python Convert float to String

You can convert float to a string easily using the pyhon built-in str() method. This might be required sometimes where we want to concatenate float values.

python program converts float to string:

pi = 3.1415

print(type(pi))  # float

piInString = str(pi)  # float -> str

print(type(piInString))  # str

Output:

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

Recommended Python Tutorials

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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