Search code examples
pythonpython-3.xf-string

F-string in Python ":.3f"


I'm reading this textbook called "Practical Statistics for Data Scientists" and this :.3f keeps getting used in almost every f-string. What does :.3f mean? My guess is it has something to do with floating point numbers.

Example:

 {house_lm_factor.intercept_:.3f}

Solution

  • This is show you how many number are printing:

    >>> import math
    >>> flt = math.pi
    >>> f'{flt:.3f}'
    '3.142'
    
    >>> f'{flt:.5f}'
    '3.14159'
    
    >>> f'{flt:.10f}'
    '3.1415926536'