Day #28 - f-strings in Python

Day #28 - f-strings in Python

ยท

3 min read

Introduction

Welcome to my 28th blog post. On day 28th of my python coding journey I learned about a way to format strings in python using f-strings. 'f-string' is the new formatting mechanism that got introduced in python in PEP 498. Let's dive into more details to understand 'f-strings'

So let's get started......

String formatting

In python, string formatting can be done using the 'format' Keyword

Example -

txt = "For only {price:.2f} dollars!"     # .2f sets the decimal points 
print(txt.format(price = 49))
For only 49.00 dollars!

f-strings

  • New string formatting mechanism introduced by PEP 498.

  • Also known as literal string Interpolation or more commonly as 'f-strings'

  • The primary focus of this mechanism is to make the interpolation easier.

  • When we prefix the string with the letter 'f', the string becomes the f-string itself.

  • f-string offers a convenient way to embed Python expressions inside string literals for formatting.

Example -

val = 'Geeks'
print(f"{val}for{val} is a portal for {val}.")

name = 'XYZ'
age = 100
print(f"Hello, My name is {name} and I'm {age} years old.")

Output -

GeeksforGeeks is a portal for Geeks.
Hello, My name is XYZ and I'm 100 years old.

Here in the above example, we have used the f-string to format the string. It evaluates at runtime; we can put all valid Python expressions in them.

Using f-string in a single statement -

Example

print(f"{2 * 30})"
60

Some more code -

letter = "Hey my name is {} and I am from {}"
country = "India"
name = "Chintan Jain"

print(letter.format(name, country))


#here the statment gets messed up, if we change the order
letter = "Hey my name is {} and I am from {}"
print(letter.format(country, name))
print("\n")

# to fix the above formatting we use numbers as below
letter = "Hey my name is {1} and I am from {0}"
print(letter.format(country, name))
print("\n")


#using fstrings
print(f"Hey my name is {name} and I am from {country}")

#to print the statement that we use fstrings like this
print(f"We use f-strings like this: Hey my name is {{name}} and I am from {{country}}")

Output -

Hey my name is Chintan Jain and I am from India
Hey my name is India and I am from Chintan Jain


Hey my name is Chintan Jain and I am from India


Hey my name is Chintan Jain and I am from India
We use f-strings like this: Hey my name is {name} and I am from {country}

Resources Used

You can watch the video of Day#28 by clicking on the below link ๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡

Conclusion

Thanks, guys for going through this blog post. On day #28 we learned about f-strings in Python and a new way to format the strings.

Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of python coding. This is it for Day #28

See you in the next one.....


About Me

Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.

I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.

So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.


ย