What Does %s Mean in a Python Format String?

What Is %s In Python

A string is a very commonly used data type in Python. Python deals with strings with great importance. Python has a lot of functionality surrounding string datatype. In this article, we’re going to dive deep into Python strings and understand what Format strings are, what placeholders are in format strings and what is %s in Format strings.

Format string In Python

Format strings are very important as it helps us create dynamic output. Usually, when you try to print something using the built-in print function available in Python, you will have to do a lot of type conversions as the print function takes only one type of a variable at a time. That is, if you are printing a string and an integer in the same line, you will have to convert the integer into a string or we can separate it using commas.

This process can be very tedious sometimes. Therefore, for this purpose, Python blesses us with format strings. Formatted strings have a lot of advantages. It gives us flexibility and readability and makes debugging process much easier.

Syntax

f'The string to be printed'

Related: Formatted Strings in Python

You can also use the format() function (quick format) for formatted strings.

It’s hard to understand these complex ways of writing code if you are new to programming or started programming with Python itself. Let’s try to understand this with some examples.

Understanding formatted strings with the help of examples

Format string

f'This is a format string'
Code And Output For Format String In Python
Code And Output For Format String In Python

The above code is the simplest example of a formatted string. You just have to write f, put inverted commas and write the text you want to print in between the inverted commas.

Using a variable in a format string

string = "format string"
f'This is a tutorial for {string}'
Code And Output For Use Of Variable In Format String
Code And Output For Use Of Variable In Format String

In the above code, we printed the variable string using the format string. Wherever you want to place a variable in a format string, you just have to put curly braces and write the name of the variable in it. So, we stored a string “format string” in the string variable, and then when we wrote the format string we put curly braces in it where we wanted our variable to be in the string and wrote the name of the variable “string” in the braces. So when the interpreter goes through the line it replaces the part of the entire braces with the value of the variable.

You can also put multiple variables in a single formatted string.

Name = 'Rohan'
Age = 19
Place = 'Mumbai'
f'Hello, My name is {Name}. I am {Age} years old and I live in {Place}.'
Multiple Variables In A Single Formatted String
Multiple Variables In A Single-Formatted String

In this example, just like the previous example, we used curly braces to place the Name, Age, and Place variables in the format string.

What is %s?

%s is a placeholder for string datatype. Placeholder is the concept picked up from the C language. They are used in format strings to represent a variable whose type will be determined in runtime. Placeholders are represented as a character or a set of characters used after a % symbol. % represents the placeholder and the following character or set of characters represents the data type of the value that will replace the placeholder. For integers, there’s %d, for float there’s %f, and for strings, there is %s.

How to use placeholders in a formatted string?

age = 25
message = f"My name is %s." %'Rohan' # format string with single placeholder value.
message = f"My name is %s and I am %d years old." %('Rohan',age) # format string with multiple placeholder value.
print(message)
Code And Output For Format Strings
Code And Output For Format Strings

In the above code, we have two examples of placeholders in format strings. We have to write a format string in single inverted commas as usual and wherever we want to put dynamic value we just place a placeholder there. After finishing the format string, we will write a percentage symbol and write the dynamic value after that. So in the first example, we wrote “My name is” like how we write a normal format string. After that, we put a %s, which represents a string placeholder. After that, we finish the format string by ending the single inverted comma. After that, we use a modulus symbol and wrote our value.

Whenever the interpreter sees a placeholder in the format string, it searches for a modulus symbol and the format string. Then it replaces the placeholder with the value after the modulus symbol. So as wrote ‘Rohan’ after the modulus symbol, the placeholder got replaced with Rohan in the output.

In the second example, we used the integer and the string placeholder in the same line and instead of passing a value for the integer placeholder, we passed a variable name that contains our desired value. We can do this for any type of variable. First I used %s placeholder for the name and then age %d for age. After finishing the format string we put a modulus and then in brackets, we passed the values we want the placeholders to get replaced with in order. Remember to write them in order, otherwise, it will lead to errors.

Situations that may lead to errors

Format strings can be confusing in some cases. This may lead to errors. To avoid them, remember a few basic things:

  • You cannot pass string values for integer placeholders or float placeholders.
  • For multiple placeholders in single format string use brackets () to write the values for placeholders after the % symbol.
  • Use {}(curly braces) for variables in the middle of format strings not () (parentheses) or [] (square brackets).
  • %s is for string placeholders, %d for integers, and %f for float values.

Conclusion

Formatted strings are an incredibly powerful tool that can be proven to be a lot better than normal strings. But as the syntax seems complicated, new programmers just skip over it. Just keep on practicing and once you get a grip on it, it’ll help you a lot.

References

stack overflow

Official Python documentation