Hey! If you love Python and building Python apps as much as I do, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

How to Format a String in Python

Learn the art of string formatting in Python with this comprehensive guide. From basic usage to advanced techniques, we’ll cover everything you need to know to format strings like a pro!| …


Updated June 30, 2023

|Learn the art of string formatting in Python with this comprehensive guide. From basic usage to advanced techniques, we’ll cover everything you need to know to format strings like a pro!|

Definition

String formatting in Python refers to the process of inserting values into a string using specific placeholders or formats. This allows for more readable and maintainable code when dealing with dynamic data.

Step-by-Step Explanation

Using the % Operator

The most basic way to format a string in Python is by using the % operator, also known as the string formatting operator. Here’s an example:

name = "John"
age = 30

print("My name is %s and I'm %d years old." % (name, age))

Output: My name is John and I'm 30 years old.

In this example:

  • %s is a placeholder for the string value
  • %d is a placeholder for the integer value
  • (name, age) are the values to be inserted into the placeholders

Using F-Strings (Python 3.6+)

F-strings are a more modern and powerful way to format strings in Python. They allow for more complex formatting and better readability.

Here’s an example:

name = "John"
age = 30

print(f"My name is {name} and I'm {age} years old.")

Output: My name is John and I'm 30 years old.

In this example:

  • f before the string indicates it’s an f-string
  • {name} and {age} are placeholders for the values, which can be expressions or variables

Using the str.format() Method

The str.format() method is another way to format strings in Python. It allows for more complex formatting and better readability.

Here’s an example:

name = "John"
age = 30

print("My name is {} and I'm {} years old.".format(name, age))

Output: My name is John and I'm 30 years old.

In this example:

  • {} are placeholders for the values
  • .format(name, age) are the values to be inserted into the placeholders

Advanced Techniques

Formatting Multiple Values

You can format multiple values in a single string using the following methods:

  • % operator: print("My name is %s and my friend's name is %s." % (name, friend_name))
  • F-strings: print(f"My name is {name} and my friend's name is {friend_name}.")
  • str.format() method: print("My name is {} and my friend's name is {}".format(name, friend_name))

Formatting Numbers

You can format numbers using the following methods:

  • % operator: print("My age is %d years old." % (age))
  • F-strings: print(f"My age is {age} years old.")
  • str.format() method: print("{} years old.".format(age))

Formatting Boolean Values

You can format boolean values using the following methods:

  • % operator: print("I am %s." % (is_admin))
  • F-strings: print(f"I am {is_admin}.")
  • str.format() method: print("{}.".format(is_admin))

Conclusion

String formatting in Python is a powerful tool that allows you to insert values into strings using specific placeholders or formats. From basic usage to advanced techniques, we’ve covered everything you need to know to format strings like a pro! Whether you’re working with dynamic data or just want to make your code more readable, string formatting is an essential skill for any Python programmer.

Stay up to date on the latest in Python, AI, and Data Science

Intuit Mailchimp