Using an F-string
Why couldn't I just use an f string?
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
https://realpython.com/python-f-strings/ shows how the old way of formatting strings can quickly become hard to read.
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
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.")
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.")