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.
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!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot