Last answered:

14 Mar 2024

Posted on:

28 Dec 2022

1

Using an F-string

Why couldn't I just use an f string?

5 answers ( 0 marked as helpful)
Super learner
This user is a Super Learner. To become a Super Learner, you need to reach Level 8.
Posted on:

30 Dec 2022

0

I guess %s operator is in Python from python 2.0 and some were habitual to it so he tells about that while F-string introduced in python 3.6.
F-string easy to use and quite readable but its personal preference which one to use

Posted on:

31 Dec 2022

0

https://realpython.com/python-f-strings/ shows how the old way of formatting strings can quickly become hard to read.

Instructor
Posted on:

06 Jan 2023

0

Hi everyone!
Thanks for reaching out.

@: Mayank and Marvin

Thanks for sharing this piece of information with the Community!

@: Cynthia

The release of Python version 3.6 introduced formatted string literals, simply called “f-strings.” They are called f-strings because you need to prefix a string with the letter 'f' to create an f- string. The letter 'f' also indicates that these strings are used for formatting. Could you please share with us your code as a post? Thank you.

Looking forward to your answer.
Best,
Tsvetelin

Posted on:

14 Mar 2024

0

product_category = [500, 600]


# The string interpolation operator (%), or modulo operator:

print("We currently have %d available units of this item." % product_category[0])


# Same can be achieved with the f-string formatting:
print(f"We currently have {product_category[0]} available units of this item.")

Posted on:

14 Mar 2024

0

stock_share_price_list = [40.50, 60.35]


# The string interpolation operator (%), or modulo operator:
print("This stock costs $%.2f per share." % stock_share_price_list[0])


# f-string formatting:

print(f"This stock costs ${stock_share_price_list[0]:.2f} per share.")

Submit an answer