Predictive Hacks

Number of decimal digits in f-strings

If we want to return a specific number of decimal digits in f-strings we can use the f'{v:.2f}' where v is our variable. For example:

import numpy as np
my_random = np.random.rand(10)

for i, n in enumerate(my_random):
    print(f'The {i} number is {n}') 

Now, if we pass the :.2f we will get 2 decimal places. For example:

for i, n in enumerate(my_random):
    print(f'The {i} number is {n:.2f}')   

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Python

Image Captioning with HuggingFace

Image captioning with AI is a fascinating application of artificial intelligence (AI) that involves generating textual descriptions for images automatically.

Python

Intro to Chatbots with HuggingFace

In this tutorial, we will show you how to use the Transformers library from HuggingFace to build chatbot pipelines. Let’s