What does %s and %d mean in Python?

Edith 112 Published: 09/08/2024

What does %s and %d mean in Python?

In Python, %s and %d are placeholders used for string formatting and variable substitution. These placeholders are part of the printf-style formatting system, which allows you to insert values into a string.

The %s placeholder is used for strings (or more generally, any object that can be converted to a string). When you use %s, Python will replace it with the actual value of the string or object. For example:

name = "John"

print("My name is %s" % name) # Output: My name is John

In this example, name is a string variable that contains the value "John". The %s placeholder in the format string is replaced with the actual value of name, resulting in the output "My name is John".

On the other hand, the %d placeholder is used for integers. When you use %d, Python will replace it with the actual value of the integer. For example:

age = 25

print("I am %d years old" % age) # Output: I am 25 years old

In this example, age is an integer variable that contains the value 25. The %d placeholder in the format string is replaced with the actual value of age, resulting in the output "I am 25 years old".

The %s and %d placeholders are used in conjunction with the % operator to perform formatting. For example:

name = "John"

age = 25

print("My name is %s, and I'm %d years old" % (name, age)) # Output: My name is John, and I'm 25 years old

In this example, the format string contains both %s and %d placeholders. The values of name and age are passed as a tuple to the % operator, which replaces the placeholders with the actual values.

It's worth noting that in Python 3.x, the % operator is no longer used for formatting. Instead, you can use the str.format() method or f-strings (formatted string literals) to perform formatting.

For example:

name = "John"

age = 25

print("My name is {}, and I'm {} years old".format(name, age)) # Output: My name is John, and I'm 25 years old

name = "John"

age = 25

print(f"My name is {name}, and I'm {age} years old") # Output: My name is John, and I'm 25 years old

In these examples, the str.format() method or f-strings are used to replace placeholders with actual values, without using the % operator.

Python format function

I'd be happy to help!

Here's a brief introduction on how you can use the format function in Python:

The format method is used to format strings. It allows you to insert values into strings. You can use placeholders (like {}) and then fill them with values from your variables.

Here are some examples of using the format function in Python:

Example 1: Basic Formatting

name = "John"

age = 25

print("My name is {} and I'm {} years old.".format(name, age))

Output:

My name is John and I'm 25 years old.

Example 2: Formatting Numbers

pi = 3.14159

print("The value of pi is {:.2f}".format(pi))

Output:

The value of pi is 3.14

Example 3: Formatting Strings

first_name = "John"

last_name = "Doe"

print("{} {}".format(first_name, last_name))

Output:

John Doe

Example 4: Formatting Multiple Values

name = "John"

age = 25

country = "USA"

print("My name is {} and I'm {} years old. I am from {}.".format(name, age, country))

Output:

My name is John and I'm 25 years old. I am from USA.

You can also use keyword arguments (like name and age) with the str.format method:

Example 5: Keyword Arguments

name = "John"

age = 25

print("My name is {name} and I'm {age} years old.".format(name=name, age=age))

Output:

My name is John and I'm 25 years old.

These are just some basic examples of how you can use the format function in Python. It's a powerful tool that allows you to customize your output strings.

Note: In Python 3.x, the str.format method has been replaced by string formatting using f-strings (like f"My name is {name} and I'm {age} years old.").