HomeLinuxPython Float to String

Python Float to String


For concatenating the float with different strings, displaying the float as a string in this system’s output, or writing a file, the Python float is required to be transformed to a string. Python gives varied methods to transform a Python float to a string such because the “str()” operate, “f-string” methodology, or “repr()” operate, and so on. On this Python information, all of those strategies are illustrated with acceptable examples

How you can Convert Python Float to a String?

To transform a Python float to a string, the next approaches can be utilized:

Methodology 1: Convert Python Float to a String in Python by Making use of the “str()” Perform

Strings could be transformed into any information kind utilizing the Python “str()” operate. Right here is the syntax:

Syntax

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

Within the above syntax:

  • The “object” parameter is the item that must be transformed right into a string.
  • The “encoding” parameter/attribute defines the encoding of the string. It defaults to ‘utf-8‘.
  • The “errors” parameter specifies how errors must be dealt with whereas encoding the string. It defaults to ‘strict‘.

Instance
The beneath code converts the desired float to a string:

float_value = 45.45
print(float_value)
print(kind(float_value), n)
output = str(float_value)
print(output)
print(kind(output))

On this instance code, the “str()” operate takes the desired float worth as its parameter and yields the string illustration.

Output

The above snippet exhibits that the given string has been transformed/remodeled right into a Python string.

Methodology 2: Convert Python Float to a String in Python by Making use of the “f-string” Methodology

In Python model “3.6”, the “f-string” methodology was launched for formatting strings in Python. The “f-strings” is a string literal that begins with an “f” or “F” and is adopted by curly braces “{}”. The curly braces are used as placeholders for variables that will likely be changed/up to date by their values at runtime.

Instance
Within the following code, the “f-string” methodology is used to transform the desired float to a string:

float_value = 45.45
print(float_value)
print(kind(float_value), n)
output = f‘{float_value:.2f}’
print(output)
print(kind(output))

Based mostly on the above code snippet, the “f-string” methodology converts the desired float to a string in accordance with the offered decimal worth.

Output

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

Methodology 3: Convert Python Float to a String in Python Utilizing the “repr()” Perform

The “repr()” operate retrieves a string description/illustration of an object that can be utilized to recreate the item.

Syntax

Within the above syntax, the “obj” parameter is the item whose printable illustration must be returned. The “repr()” operate produces a printable representational string of the enter object.

Instance
Let’s perceive it by the next instance code:

float_value = 789.45654
print(float_value)
print(kind(float_value), n)
output = repr(float_value)
print(output)
print(kind(output))

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

Output

Based mostly on the above consequence, the given float worth has been transformed right into a Python string.

Conclusion

To transform a Python float to the desired string, the “str()” operate, the “f-string” methodology, or the “repr()” operate is utilized in Python. The “str()” operate is a straightforward and environment friendly solution to convert a Python float to a string. The “f-string” methodology and “repr()” operate may convert/rework the Python float to a string. This put up introduced varied strategies utilizing acceptable examples to transform a float to a string in Python.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments