How To Use %s in Python To Format Strings

Variable manipulation is one of the best things about Python. Most programming languages put heavy restrictions on what you can do with any given variable type. Python obviously does have some limitations. But you can generally format your data in a variety of powerful ways with an ease that isn’t found in most other languages. String formatting is particularly notable within Python. A single operator, the %s, allows you to format strings in various ways. So how do we go about the process?

The Basics of a Powerful Operator

In Python, the % is known as a modulo operator. It can be combined with different variable types to work with different types of data. Modulo operators are most commonly used as placeholders of a sort. Instead of redefining or concatenating strings, you can often use a modulo operator to format them to meet your needs. This can involve the use of a number of different conversion specifiers. In the case of %s, we’re using a string conversion specifier to denote that we want our data formatted as a standard Python string.

A Deeper Dive Into String Formatting

Formatting strings with %s might sound complex. But take a look at the following example to see just how simple it can be.

print(“This is an %s of string %s.” % (“example”,”formatting”) )

The modulo operator acts as a placeholder for the variables we supply, and in the order they’re listed in. So the first %s grabs the first variable. And the second modulo goes for the second variable provided. The s in the %s refers to string. There’s a wide variety of other options that could be used other than s. For example, %d refers to a signed integer decimal. An %x produces signed hexadecimal. And we even have other methods to produce a string, such as the %a which would output an ASCII formatted string.

These characters following the % are referred to as a specifier or conversion specifiers. And make no mistake, the term conversion is apt. The modulo can essentially act like a conversion function. This is easily demonstrated by changing the “example” to a 1 within the earlier code. If you run the script again you’ll see that the 1 has been converted into a string just as if it had been passed to the str function.

Practical Use of %s

The %s works just as you’d expect from anything within Python. This means full compatibility with different containers and variables. Consider the following example.

exString=”example”
ourVars=[exString,”formatting”,2]
print(“This is an %s of string %s: version %s” % ( ourVars[0],ourVars[1],ourVars[2] ) )

We start out by assigning “example” to a new exString variable. We then create a list that starts with exString, an explicitly declared “example” string, and an integer with a value of 2. The third line is similar to the initial example. But notice how we pass the variables to the modulo operator. The first string comes from the ourVars list. And that item was itself passed from an initial declaration of the exString variable. The next string comes from one declared when creating our ourVars list. And the final string begins as an integer in the ourVars list. But it’s converted into a string through the use of the string conversion specifier. All of this highlights the fact that the %s works with data just like any function or method that works with strings. Meaning that it really doesn’t matter where a string is pulled for or defined. The only thing that matters is whether or not data provided to %s is compatible with a standard string or string conversion.

This might seem overly academic at first. But now think about how this can simplify variable handling. For example, you might have someone input their name and store it as a string. Retrieving their name could be as simple as using this code.

print(“Welcome back %s.” % ( name ) )

Or consider if you had someone’s name in a list, like the following example. We could easily create a string with his full name using %s.

names = [“Alfred”, “Benjamin”, “Exampleton”]
name = “%s %s %s” % (names[0], names[1], names[2])
print(“Welcome back %s.” % ( name ) )

In this example, we use the %s to pass strings from a list that are used in the creation of an all-new string called name. This example highlights the sheer level of versatility in Python’s types and variables. We can generally mix and match them together to meet any given need. And while these are fairly simple examples, it should be clear at this point just how useful the %s operator can be in maintaining highly readable and well-organized code.

How To Use %s in Python To Format Strings
Scroll to top