Skip to main content

Command Palette

Search for a command to run...

Python F-string

Published
3 min readView as Markdown

F-strings offer a versatile and expressive way to create formatted strings in Python, accommodating variables, expressions, and complex data structures.

Let's delve deeper into Python f-strings and explore more complex examples:

1. Variable Substitution:

#!/usr/bin/python3

fruit = "apple"
count = 3
f_string_with_variables = f"I have {count} {fruit}s."

Explanation: This f-string includes the variables count and fruit. It substitutes these variables into the string, resulting in a formatted string.

Expected Output: "I have 3 apples."

2. Expression Evaluation:

#!/usr/bin/python3
x = 5
f_string_with_expression = f"The square of {x} is {x**2}."

Explanation: Here, an arbitrary Python expression {x**2} is included in the f-string. The expression is evaluated, and the result is substituted into the string.

Expected Output: "The square of 5 is 25."

3. Formatted Output:

#!/usr/bin/python3

pi = 3.141592653589793
f_string_with_formatting = f"The value of pi is approximately {pi:.2f}."

Explanation: The f-string includes a formatted expression {pi:.2f}, which formats the value of pi as a floating-point number with two decimal places.

Expected Output: "The value of pi is approximately 3.14."

4. Using with Other Strings:

#!/usr/bin/python3
greeting = "Hello"
name = "Bob"
full_greeting = f"{greeting}, {name}!"

Explanation: This f-string concatenates the variables greeting and name into a string, creating a full greeting.

Expected Output: "Hello, Bob!"

5. Multiline f-strings:

#!/usr/bin/python3
name = "Alice"
age = 25
multiline_f_string = f"""
Name: {name}
Age: {age}
"""

Explanation: Triple quotes (""") are used for multiline f-strings. Variables name and age are substituted into the string.

Expected Output:

"
Name: Alice
Age: 25
"

6. Escaping Characters:

#!/usr/bin/python3
opening_brace = "{{"
closing_brace = "}}"
literal_braces = f"This is an example of {opening_brace}literal{closing_brace} braces."

Explanation: To include literal curly braces in the f-string, double curly braces are used.

Expected Output: "This is an example of {literal} braces."

7. Using with Functions:

#!/usr/bin/python3
def calculate_area(radius):
    return 3.14 * radius**2

radius = 5
area_message = f"The area of a circle with radius {radius} is {calculate_area(radius)}."

Explanation: The f-string calls the calculate_area function with the radius variable and includes the result in the string.

Expected Output: "The area of a circle with radius 5 is 78.5."

8. Using with Lists and Dictionaries:

#!/usr/bin/python3
my_list = [1, 2, 3]
my_dict = {'name': 'Alice', 'age': 25}

list_message = f"The second element of the list is {my_list[1]}."
dict_message = f"{my_dict['name']} is {my_dict['age']} years old."

Explanation: These f-strings access elements from a list and a dictionary and include them in the strings.

Expected Output:

#!/usr/bin/python3
"The second element of the list is 2."
"Alice is 25 years old."

9. Formatted Expressions:

#!/usr/bin/python3
num1, num2 = 10, 20
result_message = f"The sum of {num1} and {num2} is {num1 + num2}."

Explanation: This f-string includes a formatted expression that calculates the sum of num1 and num2.

Expected Output: "The sum of 10 and 20 is 30."

Feel free to run these examples in a Python environment to see the results and experiment with f-strings! If you have any questions or need further clarification, feel free to ask.