0% found this document useful (0 votes)
13 views

Introduction To F-String

Uploaded by

astronautahmed5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Introduction To F-String

Uploaded by

astronautahmed5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Introduction to f-string

The f-string (formatted string literal) in Python is a concise and readable way to embed
expressions inside string literals using curly braces {} . It was introduced in Python 3.6 and
provides an elegant alternative to the older format() method and the % operator for string
formatting.

Syntax of f-string
To create an f-string, you simply prefix the string with an f or F , and within the curly braces {} ,
you can insert any valid Python expression, including variables, functions, or more complex
expressions.

name = "Alice"
age = 25
greeting = f"Hello, {name}. You are {age} years old."
print(greeting)

Output:

Hello, Alice. You are 25 years old.

Key Features of f-strings


1. Variables Inside Strings:
You can directly embed variables into an f-string:

price = 99.99
product = "laptop"
message = f"The price of the {product} is ${price}."
print(message)

Output:

The price of the laptop is $99.99.


2. Expressions Inside f-strings:
f-strings support the embedding of any valid Python expression, not just variables.

width = 5
height = 10
area = f"The area is {width * height} square units."
print(area)

Output:

The area is 50 square units.

3. Calling Functions:
You can call functions inside f-strings:

def double(x):
return x * 2
value = 5
result = f"Double of {value} is {double(value)}."
print(result)

Output:

Double of 5 is 10.

4. Formatting Numbers:
f-strings allow you to format numbers, such as specifying decimal places, commas for
large numbers, or even scientific notation.

pi = 3.14159
formatted_pi = f"Pi rounded to two decimals is {pi:.2f}."
print(formatted_pi)

Output:

Pi rounded to two decimals is 3.14.

Here’s another example with large numbers:


big_number = 1000000
formatted_number = f"{big_number:,}"
print(formatted_number)

Output:

1,000,000
5. Multiline f-strings:
You can use multiline f-strings by simply wrapping the string in triple quotes ( """ or
''' ):

name = "Alice"
message = f"""
Dear {name},

Welcome to our platform! We are glad to have you.

Regards,
The Team
"""
print(message)

This outputs the text as written within the multiline string.


6. Escaping Braces:
If you need to include a literal curly brace ( { or } ) in your f-string, you can escape it by
doubling the braces:

expression = f"Use double curly braces to escape: {{5 + 3}}"


print(expression)

Output:

Use double curly braces to escape: {5 + 3}

Advantages of f-strings
1. Readability: f-strings make the code more readable by embedding expressions directly in
the string.
2. Efficiency: f-strings are faster than other string formatting methods like % and
.format() because they are evaluated at runtime.
3. Simplicity: They are easier to write and debug compared to other formatting approaches.

You might also like