Type Specifying Errors
Demonstrate ValueError while doing forced type-conversions
Python3
# When explicitly converted floating-point # values to decimal with base-10 by 'd' # type conversion we encounter Value-Error. print ( "The temperature today is {0:d} degrees outside !" . format ( 35.567 )) # Instead write this to avoid value-errors ''' print("The temperature today is {0:.0f} degrees outside !" .format(35.567))''' |
Output
ValueError: Unknown format code 'd' for object of type 'float'
Python String format() Method
The format()
method is a powerful tool that allows developers to create formatted strings by embedding variables and values into placeholders within a template string. This method offers a flexible and versatile way to construct textual output for a wide range of applications. Python string format() function has been introduced for handling complex string formatting more efficiently. Sometimes we want to make generalized print statements in that case instead of writing print statements every time we use the concept of formatting.