Why is %s used in python?

Do you want to add a value into a Python string? You need not look further than the %s operator. This operator lets you format a value inside a string. The %s syntax is more elegant than the concatenation operator with which you may be familiar.

In this guide, we talk about what the %s symbol means and how it works. We run through an example of this operator to help you learn how to use it in your code.

Why is %s used in python?

Find Your Bootcamp Match

  • Career Karma matches you with top tech bootcamps
  • Access exclusive scholarships and prep courses

Select your interest
First name

Last name

Email

Phone number

By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

What is the %s Operator?

The %s operator adds a string inside another string.

Here, we write a program that calculates the speed at which a car travelled, on average, to reach a destination. To calculate this information, use the following formula:

speed = distance / time

Start by asking the user for the distance they travelled, the time it took them to reach their destination, and where they were going:

distance = input("How far did you travel (in miles)? ")
time = input("How long did it take you to reach your destination (in hours)? ")
where = input("Where were you going? ")

Next, we calculate the average speed at which the user was traveling:

speed = round(float(distance) / float(time), 2)

We’ve converted the values of “distance” and “time” to floating point numbers so we can perform a mathematical operation using those values. We’ve also rounded the result of our speed calculation to two decimal places.

Now that we’ve calculated this value, we inform the user in the Python console of their average speed. To do this, use the string formatting:

print("On your journey to %s, you drove at an average speed of %s miles per hour." % (where, speed))

There are three parts to our %s syntax:

  • The %s operator is where string values are added.
  • The % (where, speed) is where we specify what values should be added to our string.

The number of values you want to add to a string must be equal to those specified in parenthesis after the % operator at the end of a string. Otherwise, you encounter a “TypeError: not enough arguments for format string” error.

In our code, we are adding two values into our string. We’ve used the %s operator twice and there are two values in parenthesis after the % sign at the end of our string.

Run our program:

How far did you travel? 63
How long did it take you to reach your destination? 2
Where were you going? London
On your journey to London, you drove at an average speed of 31.5 miles per hour.

Our code successfully calculates our average speed.

The %s operator automatically converts a value to a string. This means we don’t have to change the data type associated with “speed” when we format our value.

The % String Formatting Syntax

There’s a lot more to the % string formatting syntax than just the %s operator. You can also use the % syntax to format numbers in a string.

To learn more about formatting numbers using the % formatting syntax, read the Python documentation for string formatting. We’ve also written a tutorial on how to round a value to two decimal places using the % operator.

New Methods of String Formatting

With the introduction of the format() syntax in Python 2.6, the % string formatting operand has fallen out of favor by many developers.

This is because the formatting syntax is arguably more powerful. What’s more, the format() syntax is not very difficult to use. Consider the following statement:

print("On your journey to {}, you drove at an average speed of {} miles per hour.".format(where, speed))

This statement prints the same message that we generated from earlier. We have used the .format() syntax to add the values “where” and “speed” into our string.

The .format() syntax lets you do things like set names for each value you want to add into a string. These features are not offered by the %s syntax.

In Python 3, f strings were introduced as another alternative for the % syntax. F strings are an easy way to embed a value or an expression into a string using string literals. You can learn more about f strings in our article on Python 3 f strings.

Conclusion

The %s operator lets you add a value into a Python string. The %s signifies that you want to add a string value into a string. The % operator can be used with other configurations, such as %d, to format different types of values.

In more modern versions of Python, the % syntax has become less widely used in favor of f strings and the format() method.

Now you’re equipped with the knowledge you need to use the %s operator in your code like a professional Python developer!

What is %s formatting in python?

Python's str. format() technique of the string category permits you to try and do variable substitutions and data formatting. This enables you to concatenate parts of a string at desired intervals through point data format.

What is %s and %D in python?

%s is used as a placeholder for string values you want to inject into a formatted string. %d is used as a placeholder for numeric or decimal values.

What is %s and %D used for?

%s%d%s%d\n is a format string. It is used to specify how the information is formatted on an output.

How do you use %d %s in python?

In, Python %s and %d are used for formatting strings. %s acts a placeholder for a string while %d acts as a placeholder for a number. Their associated values are passed in via a tuple using the % operator. This code will print abc 2.