Mastering Python — Section 4: (f-string) An efficient and easy-to-read way to format strings in

<p>In Python 3.6 and above, strings prefixed with&nbsp;<code>f</code>&nbsp;or&nbsp;<code>F</code>&nbsp;are known as f-strings (formatted string literals). This is a modern and faster way to format strings compared to using the&nbsp;<code>.format()</code>&nbsp;method or the&nbsp;<code>%</code>&nbsp;operator.</p> <p>In f-strings, you can evaluate expressions inside curly braces&nbsp;<code>{}</code>&nbsp;directly within the string. The expression will be replaced by its evaluated value.</p> <p>Example:</p> <pre> book_number = 1 print(f&quot;Book number {book_number} is Python for Beginners.&quot;</pre> <pre> book_number = 1 print(f&quot;Book number {book_number} is Python for Beginners.&quot;)</pre> <p>Outputt</p> <pre> Book number 1 is Python for Beginners.</pre> <p>When you write&nbsp;<code>print(f&quot;Book number {1} is&quot;)</code>, the number 1 inside the curly braces&nbsp;<code>{}</code>&nbsp;will remain as the number 1. So, the output will be:</p> <pre> Book number 1 is</pre> <p>You can also perform operations or call functions within the curly braces. For example:</p> <pre> print(f&quot;The result of 2 + 2 is {2 + 2}.&quot;)</pre> <p>Output:</p> <pre> The result of 2 + 2 is 4.</pre> <p>F-strings are an efficient and easy-to-read way to format strings in Python.</p> <p><a href="https://medium.com/@selikurdev/mastering-python-section-4-f-string-an-efficient-and-easy-to-read-way-to-format-strings-in-88829f35c528">Click Here</a></p>
Tags: Coding Python