Python Float to String

For concatenating the float with other strings, displaying the float as a string in the program’s output, or writing a file, the Python float is required to be converted to a string. Python provides various ways to convert a Python float to a string such as the “str()” function, “f-string” method, or “repr()” function, etc. In this Python guide, all of these methods are illustrated with appropriate examples

How to Convert Python Float to a String?

To convert a Python float to a string, the following approaches can be used:

Method 1: Convert Python Float to a String in Python by Applying the “str()” Function

Strings can be converted into any data type using the Python “str()” function. Here is the syntax:

Syntax

str(object, encoding='utf-8', errors='strict')

In the above syntax:

  • The “object” parameter is the object that needs to be converted into a string.
  • The “encoding” parameter/attribute defines the encoding of the string. It defaults to ‘utf-8‘.
  • The “errors” parameter specifies how errors should be handled while encoding the string. It defaults to ‘strict‘.

Example
The below code converts the specified float to a string:

float_value = 45.45
print(float_value)
print(type(float_value), '\n')
output = str(float_value)
print(output)
print(type(output))

In this example code, the “str()” function takes the specified float value as its parameter and yields the string representation.

Output

The above snippet shows that the given string has been converted/transformed into a Python string.

Method 2: Convert Python Float to a String in Python by Applying the “f-string” Method

In Python version “3.6”, the “f-string” method was introduced for formatting strings in Python. The “f-strings” is a string literal that starts with an “f” or “F” and is followed by curly braces “{}”. The curly braces are used as placeholders for variables that will be replaced/updated by their values at runtime.

Example
In the following code, the “f-string” method is used to convert the specified float to a string:

float_value = 45.45
print(float_value)
print(type(float_value), '\n')
output = f'{float_value:.2f}'
print(output)
print(type(output))

Based on the above code snippet, the “f-string” method converts the specified float to a string in accordance with the provided decimal value.

Output

The above snippet implies that the float has been converted into a Python string appropriately.

Method 3: Convert Python Float to a String in Python Using the “repr()” Function

The “repr()” function retrieves a string description/representation of an object that can be used to recreate the object.

Syntax

repr(obj)

In the above syntax, the “obj” parameter is the object whose printable representation has to be returned. The “repr()” function produces a printable representational string of the input object.

Example
Let’s understand it by the following example code:

float_value = 789.45654
print(float_value)
print(type(float_value), '\n')
output = repr(float_value)
print(output)
print(type(output))

The above code shows that the “repr()” function takes the float as a parameter and converts it into a Python string.

Output

Based on the above outcome, the given float value has been converted into a Python string.

Conclusion

To convert a Python float to the specified string, the “str()” function, the “f-string” method, or the “repr()” function is used in Python. The “str()” function is a simple and efficient way to convert a Python float to a string. The “f-string” method and “repr()” function can also convert/transform the Python float to a string. This post presented various methods using appropriate examples to convert a float to a string in Python.



from https://ift.tt/e1PKWjL

Post a Comment

0 Comments