When to use %r instead of %s in Python?


In Python, %r and %s are used as string formatting operators to insert values into a string.

The %s operator is used for representing strings, whereas %r is used to represent a string as it would appear in its raw form, including quotes and special characters.

Use %s when you want to display a string in a user−friendly way, without any extra quotes or special characters.

Use %r when you want to display the exact string representation of a variable, including any quotes or special characters it might contain.

Use %s for most situations, as it provides a more user−friendly output that is easier to read and understand.

Here are some examples to illustrate the difference:

Example

In the first case, %s is used to insert the values of name and age into the string, resulting in a formatted string where the values are represented as strings.

In the second case, %r is used instead, which inserts the values of name and age in their raw form, including the quotes around the string value of name.

# Using %s
name = 'John'
age = 30
print("My name is %s and I am %s years old." % (name, age))

# Using %r
name = 'John'
age = 30
print("My name is %r and I am %r years old." % (name, age))

Output

My name is John and I am 30 years old.
My name is 'John' and I am 30 years old.

Here are few more examples to further illustrate the difference between %s and %r string formatting operators:

Using %s and %r with a float

Example

In this example, %s and %r both display the float value of number as it is.

number = 3.14159
print("The value of pi is approximately %s." % number)
print("The value of pi is approximately %r." % number)

Output

The value of pi is approximately 3.14159.
The value of pi is approximately 3.14159.

Using %s and %r with a boolean value

Example

In this example, %s displays the boolean value of is_raining as a string ('True'), while %r displays it in its raw form ('True').

is_raining = True
print("It is %s that it's raining outside." % is_raining)
print("It is %r that it's raining outside." % is_raining)

Output

It is True that it's raining outside.
It is True that it's raining outside.

Using %s and %r with a list

Example

In this example, %s displays the list fruits as a string, while %r displays it in its raw form, including the square brackets and quotes around each element.

fruits = ['apple', 'banana', 'orange']
print("My favorite fruits are: %s." % fruits)
print("My favorite fruits are: %r." % fruits)

Output

My favorite fruits are: ['apple', 'banana', 'orange'].
My favorite fruits are: ['apple', 'banana', 'orange'].

Here are three more examples that demonstrate the differences between %s and %r string formatting operators in Python:

Using %s and %r with an integer

Example

In this example, both %s and %r display the integer value of number as it is.

number = 42
print("The answer to life, the universe, and everything is %s." % number)
print("The answer to life, the universe, and everything is %r." % number)

Output

The answer to life, the universe, and everything is 42.
The answer to life, the universe, and everything is 42.

Using %s and %r with a string

Example

In this example, %s displays the string value of name as it is, while %r displays it in quotes to indicate that it is a string.

name = "Alice"
print("Hello, my name is %s." % name)
print("Hello, my name is %r." % name)

Output

Hello, my name is Alice.
Hello, my name is 'Alice'.

Using %s and %r with a dictionary

Example

In this example, %s displays the dictionary values using string formatting syntax, while %r displays the raw dictionary representation, including the curly braces and quotes around the keys and values.

person = {'name': 'Bob', 'age': 30}
print("My name is %(name)s and I am %(age)s years old." % person)
print("My name is %(name)r and I am %(age)r years old." % person)

Output

My name is Bob and I am 30 years old.
My name is 'Bob' and I am 30 years old.

In general, you should use %s when you want to format a value as a string, and %r when you want to format a value as a raw representation. Here are some situations where you might want to use one or the other:

It should be kept in mind that there are other string formatting operators available in Python, such as f−strings and the format() method. It's always a good idea to choose the appropriate string formatting operator for the situation at hand, to ensure that your code is easy to read and maintain.

So, in summary, you should use %s when you want to insert a value as a string, and %r when you want to insert a value in its raw form.

Updated on: 11-Aug-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements