Examveda

What will be the output of the following Python function?
float('   -12345\n')
(Note that the number of blank spaces before the number is 5)

A.     -12345.0 (5 blank spaces before the number)

B. -12345.0

C. Error

D. -12345.000000000.... (infinite decimal places)

Answer: Option B

Solution (By Examveda Team)

The correct answer is Option B: -12345.0.
The float() function in Python is used to convert a string or a number to a floating-point number. When you pass the string ' -12345\n' (which includes leading and trailing whitespace characters) to the float() function, it strips the leading and trailing whitespace and converts the remaining characters to a floating-point number. In this case, it converts '-12345' to a float, resulting in -12345.0.

This Question Belongs to Python Program >> Python Built In Functions

Join The Discussion

Related Questions on Python Built In Functions