The Best Guide to String Formatting in Python

Python is a popular programming language with several concepts; one of them is String Formatting. In this tutorial, you’ll learn the different approaches to string formatting in Python, as well as their strengths and shortcomings.

Python Training Course

Learn Data Operations in PythonExplore Course
Python Training Course

What Is String Formatting?

String formatting is the process of inserting a custom string or variable in predefined text. This sounds similar to string concatenation but without using “+” or concatenation methods.

String_Formatting_In_Python_1.

Output:

String_Formatting_In_Python_2

Types of String Formatting in Python

There are mainly four types of String Formatting techniques in Python:

  • Formatting with the % operator
  • Formatting with format() string method
  • Formatting with string literals, f-strings
  • Formatting with String Template class

Formatting With the % Operator

Strings in Python possess a unique built-in operator that is % operator. This % Operator, also known as Modulo or Interpolation Operator, helps you do simple positional formatting in strings.

This is the oldest method used for string formatting, also known as C-Style String Formatting.

Now, look at a simple example to understand this:

String_Formatting_In_Python_3

Output:

String_Formatting_In_Python_4.

Here ‘%s’ is used to embed strings, ‘%d’ for integers, ‘%f’ for floating-point values, and ‘%b’ for binary format. For other different formats, you can visit the official documentation.

Free Course: Python for Beginners

Master the fundamentals of PythonEnroll Now
Free Course: Python for Beginners

Now, have a look at another example:

String_Formatting_In_Python_5

Output:

String_Formatting_In_Python_6

Formatting With format() string Method

Python with version Python3 introduced a new way to do string formatting later back-ported to Python 2.7. This “new style” string formatting is done by calling the .format() method on a String object. You can put one or more replacement fields and placeholders defined by a pair of curly braces { } into a string and parallelly call the str.format() method. The values are passed as parameters into the format function corresponding to the placeholders.

String_Formatting_In_Python_7

Output:

String_Formatting_In_Python_8.

This “new style” formatting method has de-emphasized the %- operator formatting, but it has not been deplored. It is still supported in the latest versions of Python.

You can use the format() method to do variable substitutions by name and use them in any order you want. This feature turns very useful as it allows for rearranging the order of display without changing the arguments passed to format():

String_Formatting_In_Python_9.

Output:

String_Formatting_In_Python_10
String_Formatting_In_Python_11.

Output:

String_Formatting_In_Python_12.

The introduction of Python 3.6 brought another way to format the strings.

Learn From The Best in The Data Science Business!

Caltech Data Science BootcampExplore Course
Learn From The Best in The Data Science Business!

Formatting With String Literals, f-strings

With the advancements in Python with version 3.6, the world got introduced to a new string formatting method, f-strings or Literal String Interpolation. This formatting method lets you use embedded Python expressions inside string constants. Here's a simple example to make you familiar with the concept:

String_Formatting_In_Python_13.

Output:

String_Formatting_In_Python_14

In the above example, you can identify the prefix "f" before the string literal. With this formatting method, you can embed arbitrary Python expressions and perform inline arithmetic with them.

String_Formatting_In_Python_15.

Output:

String_Formatting_In_Python_16

String_Formatting_In_Python_17

Output:

String_Formatting_In_Python_18

F-strings provide a concise and convenient way to embed Python expressions inside string literals for formatting.

Now, look at an example using the lambda function:

String_Formatting_In_Python_19

Output:

String_Formatting_In_Python_20

Now, you will look at some limitations of f-strings.

  • An f-string expression cannot be empty.
  • Triple-quoted f-string can’t contain comments.
  • You cannot include a backslash in f-string

Free Course: Programming with Python

Learn the Basics of Programming with PythonEnroll Now
Free Course: Programming with Python

Formatting With String Template Class

Template strings are not a basic language feature, but they’re made available by the string module in the standard library. You need to import the template class from Python's built-in string module to implement string formatting. Template strings don’t allow format specifiers instead they use placeholder names formed by $ with valid Python identifiers.

String_Formatting_In_Python_21.

Output:

String_Formatting_In_Python_22

Which String Formatting Method Is the Best?

When provided with user-supplied format strings, use the Template Strings method. 

While working on Python 3.6+, use f-strings as this method is faster and better than both %-formatting and str.format(). 

You have the privilege to embed expressions in the f-string method and these f-strings expressions inside the braces are evaluated at runtime and returned as the final string. And while on Python version less than 3.6, use .format() method.

Learn data operations in Python, strings, conditional statements, error handling, and the commonly used Python web framework Django. Check out Simplilearn's Python Training course.

Conclusion

Python provides you with more than one way to perform string formatting. Moreover, the Python community has embedded newer methods with the progression of Python versions. You can visit and learn these methods through a video tutorial. Consider signing up for Simplilearn’s Python Training Course, which provides a great foundation for this dynamic programming language.

If you have any questions or inputs for our editorial team regarding this “The Supreme Guide to String Formatting in Python” tutorial, do share them in the comments section below. Our team will review them and help solve them for you very soon!

Happy learning!

About the Author

Abhisar AhujaAbhisar Ahuja

Abhisar Ahuja is a computer science engineering graduate, he is well versed in multiple coding languages such as C/C++, Java, and Python.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.
  • *According to Simplilearn survey conducted and subject to terms & conditions with Ernst & Young LLP (EY) as Process Advisors