Python | f-strings

Python | f-strings

Introduction: In the world of Python programming, string manipulation plays a crucial role in crafting readable and efficient code. Python offers various methods for string formatting, and one of the most powerful and simple approaches is using f-strings. In this article, we’ll explore the concept of f-strings in Python, explore their syntax, features, and demonstrate their usage through practical examples.

Understanding f-Strings: f-strings, introduced in Python 3.6, provide a concise and efficient way to format strings with expressions and variables. The ‘f’ prefix before the string indicates that it’s an f-string or formatted string, and expressions enclosed in curly braces {} within the string are evaluated and replaced with their corresponding values.

Syntax of f-Strings: The syntax of f-strings is straightforward:
[pre]
variable = “value”
result = f”String with {variable}”
[/pre]
In this example, the expression {variable} within the f-string is replaced with the value of the variable.

Example 1: Basic Usage of f-Strings:
Let’s start with a basic example to illustrate the usage of f-strings:

name = "Ankit Rai"
age = 26
message = f"Hello, my name is {name} and I am {age} years old."
print(message)

Output:

Hello, my name is Ankit Rai and I am 26 years old.

In this example, the variables name and age are substituted into the f-string, resulting in a formatted message.

Example 2: Arithmetic Operations in f-Strings:
f-strings allow for the evaluation of expressions within the string. Let’s see an example:

x = 100
y = 200
result = f"The sum of {x} and {y} is {x + y}."
print(result)

Output:

The sum of 100 and 200 is 300.

Here, the expression {x + y} within the f-string is evaluated, and the result is substituted into the string.

Example 3: Formatting Numeric Values in f-Strings:
f-strings support formatting options for numeric values. Here’s an example:

pi = 3.14159
formatted_pi = f"Value of pi: {pi:.2f}"
print(formatted_pi)

Output:

Value of pi: 3.14

In this example, :.2f specifies that the value of pi should be formatted as a floating-point number with two decimal places.

Example 4: Using f-Strings in List Comprehensions:
f-strings can be utilized within list comprehensions to create formatted lists. Here’s an example:

numbers = [1, 2, 3, 4, 5]
formatted_numbers = [f"Number_{num}: {num}" for num in numbers]
print(formatted_numbers)

Output:

['Number_1: 1', 'Number_2: 2', 'Number_3: 3', 'Number_4: 4', 'Number_5: 5']

Here, each element of the numbers list is formatted using an f-string within the list comprehension.

Conclusion: f-strings in Python offer a concise, readable, and efficient way to format strings with expressions and variables. With their simple syntax and powerful features, f-strings have become a preferred choice for string formatting in Python 3.6 and later versions.

Leave a Reply

Your email address will not be published.

📢 Need further clarification or have any questions? Let's connect!

Connect 1:1 With Me: Schedule Call


If you have any doubts or would like to discuss anything related to this blog, feel free to reach out to me. I'm here to help! You can schedule a call by clicking on the above given link.
I'm looking forward to hearing from you and assisting you with any inquiries you may have. Your understanding and engagement are important to me!

This will close in 20 seconds