What does %s do in Python?

Posted in /  

What does %s do in Python?
vinaykhatri

Vinay Khatri
Last updated on December 26, 2024

    There are many ways to add a value to a string, and if you are familiar with the C programming language, there you must have used the format specifiers to place or add values to a string.  Python also supports the format specifiers syntax to add data values to a string. In this Python tutorial, we will talk about the Python %s format specifier and discuss how to use it to add values to a string. We will also discuss the alternative and more modern ways to add values to a string.

    What is %s operator in Python?

    The %s operator is one of the string format specifiers,  we can use it with a string value to add different data values inside the string. This technique of adding string into another string is also known as string formatting. As python string is not a mutable data type when we use string format specifiers, it does not add the data values to the actual string, instead, it adds it on a copy of the string and return it.

    Syntax

    "first string %s second string %s third string %s " %(value1, value2, value3)

    The %s operator inside the string act as a placeholder to the values that are specified after the string. In the above syntax, we have specified 3 %s placeholders inside the string double quotes "" , and there are three values value1, value2, and value3 that will be placed inside the string in the ordered way as they are specified. The syntax of the %s operator has two parts.

    1. The place holder %s inside the string.
    2. The values %(value1, ...) that need to be placed inside the string.

    The number of placeholders operator %s must be equal to the values specified, else we will receive the errors "TypeError: not enough arguments for format string" or  "TypeError: not all arguments converted during string formatting". Example Let's say we have to print a message based on the user input. First, we will ask the user to enter their username, age and weight. And print a string message that holds all the values entered by the user.

    username = input("username: ")
    age = int(input("age: "))
    height = float(input("height(ft): "))
    weight = float(input("weight(kg): "))
    
    # format string with %s operator
    message = "\nWelcome %s \nYour Details \n Age: %s \n Height: %s \n Weight: %s "%(username, age, height, weight)
    
    print(message)

    Output

    username: Rajesh
    age: 22
    height(ft): 5.11
    weight(kg): 56
    Welcome Rajesh 
    Your Details
     Age: 22
     Height: 5.11
     Weight: 56.0

    In this examples username is of string type, age is int and weight and height are float, and using the string formating %s operator we add all the different data types values into the message string as a complete string without converting the data types of the values.

    Other Ways of string formatting in Python

    In modern Python, we have more efficient ways to format and add values into a string. There are two most common approaches you will see in the current python coding meta to format a string.

    1. using string concatenation operator with type conversion.
    2. using Python f string (most used)

    1. String concatenation operator

    String concatenation operator + can also be used to format a string. But when we use the string concatenation operator we need to take care of the data type values that we are adding into a string. The string concatenation operator can only add a value to a string if the value is also of string data type otherwise, it returns "TypeError: can only concatenate str ". So to tackle this error we can convert the values data type to string using str() function.

    Example

    Let's format the same message string of the above example using string concatenation.

    username = input("username: ")
    age = int(input("age: "))
    height = float(input("height(ft): "))
    weight = float(input("weight(kg): "))
    
    # string formatting with concatenation
    message = "\n Welcome "+ username +"\nYour Details \n" " Age: "+ str(age) +"\n Height: " + str(height) + "\n Weight: "+ str(weight)
    
    print(message)

    Output

    username: Rahul
    age: 23
    height(ft): 6
    weight(kg): 58
    
     Welcome Rahul
    Your Details
     Age: 23
     Height: 6.0
     Weight: 58.0

    2. String f string

    Python f string is the most advance and used string formatting technique. It is more elegant than %s placeholder operator and less complex than string concatenation.

    Syntax

    f"string 1 {value1} string2 {value2} string3 {value3}"

    Example

    Let's implement the same example with f string format.

    username = input("username: ")
    age = int(input("age: "))
    height = float(input("height(ft): "))
    weight = float(input("weight(kg): "))
    
    # string formatting with f string
    message = f" Welcome {username} \nYour Details \n Age: {age} \n Height: {height} \n Weight: {weight}"
    
    print(message)

    Output

    username: Rohan
    age: 24
    height(ft): 5.10
    weight(kg): 58
     Welcome Rohan 
    Your Details
     Age: 24
     Height: 5.1
     Weight: 58.0

    Conclusion

    We can use the %s operator to add different data values inside a string. The %s operator act as a placeholder and the values must also be specified in the %(values) after the string. The numbers of %s placeholder must equal the number of values you want to add in the string. The %s operator is obsolete because developers use f string to format a string. Still, Python supports the %s syntax for string formatting, and as a Python developer, you must know its syntax.

    People are also reading:

    Leave a Comment on this Post

    0 Comments