Table of contents
1.
Introduction
2.
Syntax
3.
Use cases
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024

F-Strings in Python

Author Malay Gain
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

 

F-string is a concise and convenient way to format strings instead of using the format( ) method. It is also called formatted string literals. Here we will see how to use f-string and where it can be used with examples. The objective behind f-string is to make string interpolation easy. 

You can also read about the Multilevel Inheritance in Python, Swapcase in Python

Syntax

 

In python programming language, we denote string by double "" or single quotes ''. Any f-string starts with f or F before any followed by the opening quote of the string.

f_string=f"it is an f-string"
print(f_string)
You can also try this code with Online Python Compiler
Run Code

Output

it is an f-string

 

Read about, Floor Division in Python, Fibonacci Series in Python

Use cases

 

Printing variables 

In python, we usually print variables using format( ) method by passing values of the variable as its argument.

platform="Coding Ninjas Studio"
print("welcome to the {}".format(platform));
You can also try this code with Online Python Compiler
Run Code

Output

welcome to the Coding Ninjas Studio

Above requirement can be fulfilled easily by just using f-string. In that case, we don’t need to use any function to format the string for printing.

 

platform="Coding Ninjas Studio"
print(f"welcome to the {platform}")
You can also try this code with Online Python Compiler
Run Code

Output

welcome to the Coding Ninjas Studio

In the above example, the variable platform is replaced with Coding Ninjas Studio .

 

 Evaluating expression

It can also be used for evaluating the expression while printing its value as the expression is evaluated during runtime.

val1=10
val2=20
print(f"product of val1 and val2 : {val1*val2}")
You can also try this code with Online Python Compiler
Run Code

Output

product of val1 and val2 : 200

 

Conditional statement

F-string is also used with conditional if…else statement. 

val1=10
val2=20
print(f"product of val1 and val2 is greater than 100 :{True if val1*val2>100 else False}")
You can also try this code with Online Python Compiler
Run Code

Output

product of val1 and val2 is greater than 100 :True

 

F-string with Function 

Function can be called inside a f-string. If the function returning any string that string will be concatenated with the remaining f-string.

def evaluate(num):
  if(num>100):
    return f"{num} is greater than 100"
  else :
    return f"{num} is not greater than 100"

print(f"result of evaluation: {evaluate(50)}")
You can also try this code with Online Python Compiler
Run Code

Output

result of evaluation: 50 is not greater than 100

 

You can try it on online python compiler.

FAQs

Q1. Is f-string is faster than the format( ) method for formatting a string?

Ans.

Yes, f-string is faster in formatting a string.

 

Q2. How f-string is used to justify string while printing?

Ans.

By default, a string is justified to left. To justify a string to left > character is    used. It follows the colon character.

 

s1='c'
s2='cd'
s3='cde'
print(f"{s1:>5}")#width of the output set to 5 characters
print(f"{s2:>5}")
print(f"{s3:>5}")
You can also try this code with Online Python Compiler
Run Code

Output

c
   cd
  cde

 

 Q3. How f-string is formatted using f-string?

       

num=2.5
print(f"{num:.3f}") # precision value 3 is mentioned 
##output
2.500
You can also try this code with Online Python Compiler
Run Code

 

Q4. How to format date-time using f-string?

 

import datetime
now= datetime.datetime.now()
print(f"{now:%Y-%m-%d %H:%M}")
##output
2022-02-05 06:44
You can also try this code with Online Python Compiler
Run Code

 

Key Takeaways

 

This article covered how to format string using f-string and where it can be used.

Check out the Coding Ninjas Studio Guided Path on Python programming for learning Python language from basics.

 

Side by side, you can also practice a wide variety of coding questions commonly asked in interviews in Coding Ninjas Studio. Along with coding questions, you can also find the interview experience of scholars working in renowned product-based companies here. 

Recommended Readings -

Recommended problems -

 

Happy learning!

Live masterclass