Menu Close

Python f-string Tutorial

python f-string tutorial

In this tutorial, you will learn all about python f-string and along with various examples. F-string is a string formatting technique that provides a facility to insert variable values inside a string.
Sometimes we have a large string and we want to insert variable values somewhere inside the string, Then at that time python, f-string plays the most important role.

In this guide, we will see what is f-string in python, syntax, and how to use f-string along with examples.

What is Python f-string?

Python f-string is a string formatting technique to insert variable values inside the string. f-strings in Python is the best way to place variable values inside a string.

Syntax

Every f-string statement consists of two-part the first is f or F and the next is the string you want to format. A string enclosed between single quote, double-quote, or triple quotes.

Display Variable

To display the variable value inside the string, use curly braces { }. The variables in the curly { } braces are displayed in the output as a normal print statement. Let’s see an example.

Example:

name = 'Programming Funda'
type = 'Educational'

a = f'{name} is an {type} website'
print(a)

The output will be:- Programming Funda is an Educational website

You can use F instead of f but still, the output will be the same.

name = 'Programming Funda'
type = 'Educational'

a = F'{name} is a {type} website'
print(a)

Expression

Python f-string also provides the best facility to evaluate expressions inside the string.

Example:

a = 12
b = 10
c = 20

print(f"Result will be:- {a + b * c}")

The result will be:- 212

You can also call the function inside the curly braces ( { } ). Let see an example and call function inside the curly braces ( { } ).

Example:

#A function to display a simple message
def welcome(name):
	return 'Welcome to ' + name

#Call the function inside f-string	
print(f'{welcome("Programming Funda.")}')

The output will be:- Welcome to Programming Funda.

In the same way, you can also call python built-in function inside the curly braces ( {} ).

mystring = 'programming funda is education blog where you can learn python with fun.'
print(f'{mystring.title()}')

Output

Programming Funda Is Education Blog Where You Can Learn Python With Fun.

Special Characters

If you want to display special characters like {},\’,\”, which are internally used by python for special meaning. You can use escape characters inside the Python f-string.

Quotations:

If you want to display quotation marks like single, double, or triple then we need to use the escape character to print quotation marks.
Suppose we want to display Welcome to ‘Programming Funda’.

name = "Programming Funda"
print(f"Welcome to \'{ name }\'")

The output will be:- Welcome to ‘Programming Funda’

You can also use double quotation instead of single quotation but still, the output will be the same.

name = "Programming Funda"
print(f"Welcome to \"{ name }\"")

The output will be:- Welcome to “Programming Funda”

Let’s see another type of quotation in python f-string.

print(f'{"Hello, Programming Funda"}')
print(f"{'Hello, Programming Funda'}")
print(f"\"{'Hello, Programming Funda'}\"")
print(f"\'{'Hello, Programming Funda'}\'")

Output

Hello, Programming Funda
Hello, Programming Funda
"Hello, Programming Funda"
'Hello, Programming Funda'

Braces:

Sometimes we need to print braces in string then we need to use double braces.

mystring  = 'Programming Funda' 
print(f"{{{mystring}}} funda is education blog.")

Output will be:- {Programming Funda} is education blog.

Dictionaries

You can access the python dictionary item inside curly braces ( {} ).

Example:

Student = {

'Name': 'John',
'Course': 'BCA',
'Occupation': 'Software Engineer'

}
print(f"My name is {Student['Name']} Rao, I am a {Student['Occupation']}. I have completed {Student['Course']}.")

Output

My name is John, I am a Software Engineer. I have completed BCA.

Conclusion

In this tutorial, you have learned all about Python f-string along with various examples. f-string in python is mostly used by python programmers to insert variable values inside a string. Personally, f-string in python is mostly used by me to string formatting.

If this article helps you, please share and visit for further python tutorials.

For more information:- Click Here

Python Tuple Tutorial
Python Lambda ( Anonymous ) Function

Related Posts