Apply Now Apply Now Apply Now
header_logo
Post thumbnail
PYTHON

How to Use Python f-Strings With Examples 

By Vishalini Devarajan

Python offers several ways to format strings, but f-strings are among the simplest and most readable methods. They let developers easily insert variables, expressions, and calculations directly into a string without complicating the code.

If you are learning Python for data science, automation, backend development, or machine learning, knowing about f-strings is important because they are commonly used in actual Python projects.

In this blog, you will discover what f strings are in Python, how they work, their syntax, benefits, and various examples beginners should try.

Table of contents


  1. TL:DR
  2. Why Use f-Strings?
  3. Syntax of Python f-Strings
  4. Basic f String Examples
    • Example 1: Printing a Name
    • Example 2: Printing Age
    • Example 3: Combining Multiple Variables
  5. Using Variables Inside f Strings
  6. Performing Calculations in f Strings
  7. Formatting Numbers Using f Strings
    • Decimal Formatting
    • Percentage Formatting
    • Comma Separator
  8. Using Functions in f-Strings
  9. Multi-Line Strings
  10. Escape Characters in f Strings
  11. Real World Applications of Python f-Strings
    • Web Development
    • Data Science
    • Automation Scripts
    • Machine Learning Projects
  12. Advantages of Using f-Strings
  13. Common Mistakes Beginners Make
    • Forgetting the Letter f
    • Using Invalid Variables
    • Mixing Quotes Incorrectly
  14. Conclusion
  15. FAQs
    • What does the f mean in Python f strings?
    • Are f-strings available in all Python versions?
    • Are f-strings faster than format()?
    • Can we perform calculations inside f-strings?
    • Are f strings recommended for beginners?

TL:DR

  1. Python f-strings are a modern way to format strings using variables and expressions directly within text.
  2. They were introduced in Python 3.6 and are faster and clearer than older formatting methods.
  3. f strings use curly braces {} to insert variables, calculations, and function outputs.
  4. They help beginners write cleaner and shorter Python code.
  5. f strings are widely applied in real-world Python applications, including automation, web development, and data science.

What Are f-Strings in Python?

f-strings (formatted string literals) are a string formatting feature introduced in Python 3.6 that allows variables, expressions, and calculations to be embedded directly inside strings using curly braces {}. By placing the letter f before a string, Python evaluates the expressions within the braces and inserts their values into the final output. Compared to older formatting methods such as % formatting and str.format(), f-strings are more concise, readable, and efficient, making them the preferred approach for string formatting in modern Python.

Why Use f-Strings?

Before f-strings became available, developers mostly used string concatenation or the format() method.

Here is an example using concatenation:

name = “Harini”
print(“Hello ” + name)

Example using format():

name = “Harini”
print(“Hello {}”.format(name))

Example using f string:

name = “Harini”
print(f”Hello, {name}”)

The f-string version looks more natural and is easier to read.

Syntax of Python f-Strings

The syntax of f-strings is straightforward:

f”text {variable}”

You can place variables, expressions, or even function calls inside curly braces.

For example:

name = “Python”
version = 3.13
print(f”{name} version is {version}”)

Output:

Python version is 3.13

Basic f String Examples

Example 1: Printing a Name

name = “Rahul”
print(f”My name is {name}”)

Output:

My name is Rahul

Example 2: Printing Age

age = 24
print(f”I am {age} years old”)

Output:

I am 24 years old

Example 3: Combining Multiple Variables

city = “Chennai”
profession = “Developer”
print(f”I live in {city} and work as a {profession}”)

Output:

I live in Chennai and work as a Developer

Using Variables Inside f Strings

One of the main benefits of f-strings is that they let you insert variables directly into strings.

Example:

product = “Laptop”
price = 55000
print(f”The price of the {product} is Rs.{price}”)

Output:

The price of the Laptop is Rs. 55000

This makes creating dynamic strings easier in real applications.

Performing Calculations in f Strings

You can also perform calculations directly inside strings.

Example:

 a = 10
b = 20
print(f”The sum is {a + b}”)

Output:

The sum is 30

Another example:

 length = 5
width = 4
print(f”Area = {length * width}”)

Output:

Area = 20

This feature cuts down on the need for extra variables.

You can also download HCL GUVI’s Python ebook to learn variables, loops, functions, OOP concepts, and beginner-friendly Python programs in one place. 

MDN

Formatting Numbers Using f Strings

f strings support formatting options for decimals, percentages, and alignment.

Decimal Formatting

pi = 3.141592
print(f”Value of pi is {pi:.2f}”)

Output:

The value of pi is 3.14

Percentage Formatting

score = 0.95
print(f”Success rate: {score:.0%}”)

Output:

Success rate: 95%

Comma Separator

salary = 1500000
print(f”Salary: {salary:,}”)

Output:

Salary: 1,500,000

Using Functions in f-Strings

You can even call functions inside f-strings.

Example:

name = “python”
print(f”Uppercase: {name.upper()}”)

Output:

Uppercase: PYTHON

Another example:

text = “machine learning”
print(f”Length: {len(text)}”)

Output:

Length: 16

Multi-Line Strings

f strings can also work with multi-line text.

Example:

name = “Harini”
course = “Python”
message = f””
Student Name: {name}
Course Name: {course}
“””
print(message)

Output:

Student Name: Harini
Course Name: Python

This helps generate reports or structured outputs.

Escape Characters in f Strings

Sometimes you may need to use quotation marks within f-strings.

Example:

name = “Harini”
print(f’Her name is “{name}”‘)

Output:

Her name is “Harini.”

You can use single and double quotes strategically to avoid syntax errors.

Real World Applications of Python f-Strings

Python f-strings are widely used in various areas of software development because they make dynamic text generation easier.

Web Development

Developers use f-strings to create dynamic messages, logs, and user outputs in Flask and Django applications.

Example:

username = “Harini”
print(f”Welcome back, {username}”)

Data Science

Data analysts use f-strings when displaying calculated outputs, metrics, and formatted reports.

Example:

accuracy = 96.5
print(f”Model Accuracy: {accuracy}%”)

Automation Scripts

Automation scripts often use f-strings for file names, logs, and notifications.

Example:

filename = “report”
print(f”{filename}.pdf generated successfully”)

Machine Learning Projects

Machine learning engineers use f-strings for printing training results and evaluation metrics.

Example:

epoch = 10
loss = 0.245
print(f”Epoch {epoch} completed with loss {loss}”)

If you want to practice more Python concepts beyond f-strings, explore this Python Tutorial for Beginners guide. 

Advantages of Using f-Strings

  1. Easier to read and write
  2. Faster than older formatting methods
  3. Supports calculations directly
  4. Reduces unnecessary code
  5. Makes dynamic string creation simple
  6. Widely used in modern Python applications

Because of these advantages, f strings are seen as the preferred string formatting method in Python.

Common Mistakes Beginners Make

Forgetting the Letter f

Wrong:

name = “Harini”
print(“Hello {name}”)

Output:

Hello {name}

Correct:

name = “Harini”
print(f”Hello {name}”)

Using Invalid Variables

Wrong:

print(f”Age is {age}”)

If the variable is not defined, Python will raise an error.

Mixing Quotes Incorrectly

Wrong:

print(f”Python’s version is {version}”)

If quotes don’t match, syntax errors may happen.

Curious about how these concepts work in real life? Join HCL GUVI’s Python course to build Python projects and learn automation, backend development, and data science fundamentals. 

Conclusion

f-strings are one of the most useful features introduced in modern Python. They simplify string formatting, making it quicker and easier to understand, especially for beginners.

Whether you are creating automation scripts, web applications, machine learning projects, or data analysis programs, you will often use f-strings in Python.

Learning them early enables you to write more professional and readable Python code.

FAQs

1. What does the f mean in Python f strings?

The letter f stands for formatted string literal. It allows you to insert variables and expressions directly into strings.

2. Are f-strings available in all Python versions?

No, f strings were introduced in Python 3.6.

3. Are f-strings faster than format()?

Yes, in most cases, f strings are faster and clearer than the format() method.

4. Can we perform calculations inside f-strings?

Yes, you can perform expressions and calculations directly within curly braces.
Example:
a = 5
b = 10
print(f”Result = {a + b}”)

MDN

Yes, f strings are beginner-friendly because they are simple, readable, and widely used in real-world Python development.

Success Stories

Did you enjoy this article?

Schedule 1:1 free counselling

Similar Articles

Loading...
Get in Touch
Chat on Whatsapp
Request Callback
Share logo Copy link
Table of contents Table of contents
Table of contents Articles
Close button

  1. TL:DR
  2. Why Use f-Strings?
  3. Syntax of Python f-Strings
  4. Basic f String Examples
    • Example 1: Printing a Name
    • Example 2: Printing Age
    • Example 3: Combining Multiple Variables
  5. Using Variables Inside f Strings
  6. Performing Calculations in f Strings
  7. Formatting Numbers Using f Strings
    • Decimal Formatting
    • Percentage Formatting
    • Comma Separator
  8. Using Functions in f-Strings
  9. Multi-Line Strings
  10. Escape Characters in f Strings
  11. Real World Applications of Python f-Strings
    • Web Development
    • Data Science
    • Automation Scripts
    • Machine Learning Projects
  12. Advantages of Using f-Strings
  13. Common Mistakes Beginners Make
    • Forgetting the Letter f
    • Using Invalid Variables
    • Mixing Quotes Incorrectly
  14. Conclusion
  15. FAQs
    • What does the f mean in Python f strings?
    • Are f-strings available in all Python versions?
    • Are f-strings faster than format()?
    • Can we perform calculations inside f-strings?
    • Are f strings recommended for beginners?