Python String %s — What Does It Mean?

white and black printer paper

Python string with %s is an old string formatting technique.

The %s is placed into a string and it acts as a placeholder for another string variable.

For example:

name = "John"
print("Hello, %s" % name)

Output:

Hello, John

However, as more modern string formatting techniques have been added to Python, you rarely see this in use. And you should not use it either!

But let’s have a quick look at how string formatting works using the % operator. Then let’s take a look at the more modern approaches you can use.

Python String Formatting with Percent (%) Operator

Python supports multiple ways to format strings. One of which is the % operator approach. This string formatting originates from C programming. It is one of the original built-in string formatting mechanisms in Python.

To format strings using this approach, place a % operator followed by a character that describes the type of the variable into a string.

For example, to embed a string into another:

name = "John"
print("Hello, %s" % name)

Output:

Hello, John

Formatting Different Types with % in Python

%s is not the only formatting option when it comes to formatting strings with the % operator in Python.

Here is a full list of all the possible string formatting placeholders using % in Python:

# %c — a placeholder for single character
char = "A"
sentence = "The first letter in alphabetics is %c" % char
# --> The first letter in alphabetics is A

# %s — a string placeholder
name = "John"
sentence = "My name is %s" % name
# --> My name is John

# %i, %d — Placeholders for signed decimal integers
number = 10
sentence = "Number %i is my favorite number" % number
# --> Number 10 is my favorite number

# %u — Placeholder for an unsigned decimal integer
s_int = -10
u_int = -10 + (1 << 32)

sentence = "This is an example of unsigned integer: %u" % u_int
# --> This is an example of unsigned integer: 4294967286

# %o — a placeholder for an octal integer
num = 15
sentence = "Number %i is %o as an octal number" % (num, num)
# --> Number 15 is 17 as an octal number

# %x — a placeholder for a hexadecimal integer (in lowercase characters)
num = 255
sentence = "Number %i is %x in hexadecimal" % (num, num)
# --> Number 255 is ff in hexadecimal

# %X - a placeholder for a hexadecimal integer (in uppercase characters)
num = 255
sentence = "Number %i is %X in hexadecimal" % (num, num)
# --> Number 255 is FF in hexadecimal

# %e - a placeholder for exponential notation with lowercase 'e'
num = 1324314234
sentence = "Number %i is %e in exponential notation" % (num, num)
# --> Number 1324314234 is 1.324314e+09 in exponential notation

# %E - a placeholder for exponential notation with lowercase 'E'
num = 1324314234
sentence = "Number %i is %E in exponential notation" % (num, num)
# --> Number 1324314234 is 1.324314E+09 in exponential notation

# %f - a placeholder for
price = 5.99
sentence = "The price is %f" % price
# --> The price is 5.990000

# %g - a placeholder that does the same as %f and %e above
num = 140014653546
sentence = "Number %i is %g in exponential notation" % (num, num)
# --> Number 140014653546 is 1.40015e+11 in exponential notation

price = 11.99
sentence = "The price is %g" % price
# --> The price is 11.99

# %G - a placeholder that does the same as %f and %E above
num = 999834234
sentence = "Number %i is %G in exponential notation" % (num, num)
# --> Number 999834234 is 9.99834E+08 in exponential notation

price = 2.39
sentence = "The price is %G" % price
# --> The price is 2.39

A Modern Way to Format Python Strings—Avoid % in Python

Instead of using the old-school approach % to format strings, it is recommended to use one of the two more modern approaches.

Format() Method in Python

As of Python 3.0, it’s been possible to format strings using the format() method.

For example:

age = 50
sentence = "My age is {}".format(age)

print(sentence)

Output:

My age is 50

This is already a way more elegant approach to formatting strings in Python than using the % approach.

But as of Python 3.6, there is an even better alternative called F-strings.

F-strings in Python

An even more elegant string formatting approach was introduced in Python 3.6. This is called the F-strings approach.

Instead of having to call format() or use confusing % signs, you can use an f in front of a string and embed the variables in a really intuitive way.

For example:

age = 50
sentence = f"My age is {age}"

print(sentence)

Output:

My age is 50

Conclusion

Python string %s means string formatting where a string variable is placed inside another string.

Thanks for reading.

Happy coding!

Further Reading

50 Python Interview Questions

F-strings in Python

Share on facebook
Facebook
Share on google
Google+
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on pinterest
Pinterest

Leave a Comment

Your email address will not be published. Required fields are marked *