🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Python Notes
Topic #21

Format Strings


String Format

As we learned in the Python Variables chapter, we cannot combine strings and numbers like this:

Example

  age = 36
#This will produce an error:
txt = "My name is John, I am " + age
print(txt)

But we can combine strings and numbers by using f-strings or the format() method!


F-Strings

F-String was introduced in Python 3.6, and is now the preferred way of formatting strings.

To specify a string as an f-string, simply put an f in front of the string literal, and add curly brackets {} as placeholders for variables and other operations.

Example

  age = 36
txt = f"My name is John, I am {age}"
print(txt)

Placeholders and Modifiers

A placeholder can contain variables, operations, functions, and modifiers to format the value.

Example

price = 59

txt = f"The price is {price} dollars"

print(txt)

A placeholder can include a modifier to format the value.

A modifier is included by adding a colon : followed by a legal formatting type, like .2f which means fixed point number with 2 decimals:

Example

price = 59

txt = f"The price is {price:.2f} dollars"

print(txt)

A placeholder can contain Python code, like math operations:

Example

txt = f"The price is {20 * 59} dollars"

print(txt)

Learn more about String Formatting in our String Formatting chapter.

Want to go beyond the notes?

Join CodingNow 2.0's Python course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Format Strings – FAQs

Quick answers about learning Format Strings in Python.

This free note from CodingNow 2.0 explains Format Strings in Python — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Python topic on CodingNow 2.0, including Format Strings, is 100% free with no signup required.
With focused practice, most students grasp Format Strings in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now