Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator

What is float() in Python?

Mahnoor Zaheer

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The float() method in Python produces a floating-point number from a string or an integer.

Syntax

This method is declared as follows:

The syntax of float() method in Python

Parameter and return value

The float() method takes only one parameter, which is optional. This parameter can be a string or an integer.

The return value is a floating-point number. If a string needs to be passed in the float() method, it must constitute all numeric characters in it. In the case of no parameter, it returns 0.

Example

In the example below, for integers and strings with numeric characters, the float() method returns floating-point numbers. With non-numeric strings, this method raises an error.

#No parameter
print (float())
# Integer
print (float(10))
# String
print(float("13.33"))
# String with numeric characters and no decimal point
print(float("17"))

RELATED TAGS

python
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring