Learn Code With Durgesh LogoLCWD
HomeCoursesBlogsContact Us
HomeCoursesBlogsContact Us
LearnCodeWithDurgesh Logo

Learn Code With Durgesh

Offering free & premium coding courses to lakhs of students via YouTube and our platform.

Explore

  • About Us
  • Courses
  • Blog
  • Contact

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Support

Contact

  • 📞 +91-9839466732
  • support@learncodewithdurgesh.com
  • Substring Technologies, 633/D/P256 B R Dubey Enclave Dhanwa Deva Road Matiyari Chinhat, Lucknow, UP, INDIA 226028
© 2025 Made with ❤️ by Substring Technologies. All rights reserved.
Advanced Formatting with Python's f-strings

Advanced Formatting with Python's f-strings

By durgesh • Tue Jul 09 2024

Advanced Formatting with Python's f-strings

Today, we're diving into an advanced Python tip: f-strings!"

"F-strings, or formatted string literals, were introduced in Python 3.6. They make string formatting super easy and readable."

Basic example of using f-string:

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

 

But, we can do much more with f-strings, let's have a look:

 

  • To format the number to desired decimal positions, e.g. : 

 

x=7869.3278654

print(f"{x:.4f}")

 

Here .4f formats the number to 4 decimal places

 

  • Change the alignment using <,>,^ :

 

name="John"

print(f"{name:>30}")

 

It will shift the value by 30 characters left

 

  • Show expressions inside the f-string: "F-strings can also evaluate expressions directly inside the curly braces!"

 

print(“Value of 5*4 is {5*4}”)

F-strings are powerful and flexible! Try these advanced formatting tricks in your next Python project

 

Share this article ...

💬WhatsApp📘Facebook💼LinkedIn🐦X

Trending Blogs...

Maven Tutorial

Maven Tutorial

Maven is a build automation tool primarily used for Java projects. It addresses two main aspects of building software: dependency management and project build lifecycle management.

Important DSA Topics for placements

Important DSA Topics for placements

Data Structures and Algorithms (DSA) are fundamental building blocks of computer science. A strong understanding of DSA is essential for every developer, regardless of their specialization.

Amazing Windows key shortcuts in Windows 11

Amazing Windows key shortcuts in Windows 11

Shortcuts are everyone’s favorite, be it in life or in Keyboard. We don’t know about life but we have some amazing shortcut tricks for your keyboard, which are newly introduced in windows 11. Let's have a look and understand these amazing shortcuts to reduce our finger pain

Share this article ...

💬WhatsApp📘Facebook💼LinkedIn🐦X