Open In App

What does %s mean in a Python format string?

Last Updated : 27 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python, the %s format specifier is used to represent a placeholder for a string in a string formatting operation. It allows us to insert values dynamically into a string, making our code more flexible and readable. This placeholder is part of Python's older string formatting method, using the % operator, which is still widely used for simple formatting tasks.

Let’s look at a basic example of how %s is used in string formatting.

Python
s = "Coco"

greeting = "Hello, %s!" % s
print(greeting) 

Output
Hello, Coco!

Explanation:

  • We define the string variable name with the value "Coco".
  • The string "Hello, %s!" contains the placeholder %s, which will be replaced by the value of the name variable.
  • The method % is used to replace %s with the value of name.

Syntax of %s

"string" % value

  • string: The string containing placeholders (like %s) where the value will be inserted.
  • value: The value to replace the placeholder. It can be any object that can be converted into a string.

Parameters:

  • %s: This is the placeholder that expects a value, which can be a string or any data type that can be converted to a string.

Return Type:

  • The return type is a string, where the placeholder %s has been replaced with the provided value.

Examples of %s mean

Inserting a String

Here’s an example where we replace %s with a string.

Python
s = "Kay"

message = "Welcome, %s!" % s
print(message)  

Output
Welcome, Kay!

Explanation:

  • The placeholder %s is replaced by the value of name.

Inserting Multiple Values

We can use multiple %s placeholders in a single string and pass multiple values to replace them.

Python
name = "Bob"
age = 25
info = "Name: %s, Age: %s" % (name, age)
print(info)  

Output
Name: Bob, Age: 25

Explanation:

  • We use two %s placeholders in the string.
  • The values for name and age are passed as a tuple (name, age) and inserted in the string.

Using Objects

We can also insert custom objects into strings using %s. The object will be automatically converted to a string using its __str__() method.

Python
class Person:
    def __str__(self):
        return "Person object"
    
obj = Person()
message = "The object is: %s" % obj
print(message)  

Output
The object is: Person object

Explanation:

  • Even custom objects can be used with %s. Python automatically calls the __str__() method of the object to convert it to a string before inserting it into the format string.

Next Article
Article Tags :
Practice Tags :

Similar Reads

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy