
As of Python 3, string formatting is performed chiefly using the format() function, which belongs to the String class. We won’t be spending any more time talking about it – except to mention that it could be useful when dealing with older versions of Python.


This outdated method has been explicitly disavowed in the Python 3 documentation because it’s error-prone and outdated. > print("Lorem %s dolor sit %s." % ("ipsum", "amet")) Its usage primarily consisted of adding a C-style placeholder in the string where a variable should go and then linking to it by appending the variable references to the string using %. > print("customer_name ordered a order_name for price with a tip_percentage tip")Ĭustomer_name ordered a order_name for price with a tip_percentage tipīefore the introduction of Python 3, string formatting was mainly achieved through the % operator. What happens when we need to read the customer’s name, their order, its price, and the tip amount from variables? In this snippet, we are printing the details of an order in a pizza restaurant. John ordered a Pepperoni Pizza for $4.25 with a 15% tip > print("John ordered a Pepperoni Pizza for $4.25 with a 15% tip") Take, for instance, the code snippet below: After all, all the ubiquitous “Hello World!” really does is print one string:Ĭomplications arise, however, when we need to introduce dynamic data – meaning data that may change over time or with each execution. Working with strings is one of the first things any developer learns to do as such, it seems as if it should be one of the simplest. (Check out this article if you’ll also be manipulating strings in SQL.) What is String Formatting in Python? Today you will learn about how Python handles string formatting, or the art of combining a string with dynamic data in such a way that it is easily legible by either a human reader or an expecting machine.

You may know all kinds of ways to manually manipulate strings in Python – maybe you learned from our Working with Strings in Python course (part of the Python Basics track).

Only contains a 'lat' and a 'lng' attribute.Everything you need to know about formatting strings in Python and the Python formatting mini-language. When you leave the width at the default of eighty characters,
