Open In App

What does %s mean in a Python format string?

Last Updated : 29 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The % symbol is used in Python with a large variety of data types and configurations. %s specifically is used to perform concatenation of strings together. It allows us to format a value inside a string. It is used to incorporate another string within a string. It automatically provides type conversion from value to string. 

The %s operator is put where the string is to be specified. The number of values you want to append to a string should be equivalent to the number specified in parentheses after the % operator at the end of the string value. 

The following Python code illustrates the way of performing string formatting. 

Simple use of %s

Python3




# declaring a string variable
name = "Geek"
 
# append a string within a string
print("Hey, %s!" % name)


Output

Hey, Geek!

Multiple %s

Multiple strings can also be appended within a single string using the %s operator. The strings are replaced in the order of their position in the brackets, wherever there is an %s sign. This is illustrated using the following code snippet :

Python3




# declaring a string variable
var1 = "Geek!"
var2 = "Geeks for Geeks"
 
# append multiple strings within a string
print("Hello %s Are you enjoying being at %s for preparations." % (var1, var2))


Output

Hello Geek! Are you enjoying being at Geeks for Geeks for preparations.

Mapping strings to %s

However, the number of occurrences of this operator must be equal to the number of strings to replace with after the % sign. Otherwise, an error of the type “TypeError: not enough arguments for format string” is thrown.

Python3




# declaring string variables
str1 = 'Understanding'
str2 = '%s'
str3 = 'at'
str4 = 'GeeksforGeeks'
 
# concatenating strings but %s not equal to string variables
final_str = "%s %s %s %s" % (str1, str3, str4)
 
# printing the final string
print("Concatenating multiple strings using Python '%s' operator:\n")
print(final_str)


Error

Traceback (most recent call last):

  File “/home/c7b65fabd2ad00163eba70bbc39685d3.py”, line 8, in <module>

    final_str = “%s %s %s %s” % (str1, str3, str4)

TypeError: not enough arguments for format string

Correct Code

Python3




# declaring string variables
str1 = 'Understanding'
str2 = '%s'
str3 = 'at'
str4 = 'GeeksforGeeks'
 
# concatenating strings
final_str = "%s %s %s %s" % (str1, str2, str3, str4)
 
# printing the final string
print("Concatenating multiple strings using Python '%s' operator:\n")
print(final_str)


Output

Concatenating multiple strings using Python '%s' operator:

Understanding %s at GeeksforGeeks

Order %s using dictionary

The strings are printed in whatever order they are appended using the dictionary key in output.

Python3




# declaring string variables with dictionary
dct = {'str1': 'at',
       'str2': 'GeeksforGeeks',
       'str3': 'Understanding',
       'str4': '%s'}
 
# concatenating strings
final_str = "%(str3)s %(str4)s %(str1)s %(str2)s" % dct
 
# printing the final string
print("Concatenating multiple strings using Python '%s' operator:\n")
print(final_str)


Output

Concatenating multiple strings using Python '%s' operator:

Understanding %s at GeeksforGeeks

List as a string for %s

A non-string operator can also be formatted using the %s symbol in Python. Tuples can also be both inserted and formatted using this operator. 

Python3




# declaring string variables
str1 = 'Understanding'
str2 = 'integers'
str3 = 'at'
str4 = 'GeeksforGeeks = '
 
# declaring list variables
lst = [1, 2, 3]
 
# concatenating strings as well as list
final_str = "%s %s %s %s %s" % (str1, str2, str3, str4, lst)
 
# printing the final string
print("Concatenating multiple values using Python '%s' operator:\n")
print(final_str)


Output

Concatenating multiple values using Python '%s' operator:

Understanding integers at GeeksforGeeks =  [1, 2, 3]


Previous Article
Next Article

Similar Reads

What does the Double Star operator mean in Python?
Double Star or (**) is one of the Arithmetic Operator (Like +, -, *, **, /, //, %) in Python Language. It is also known as Power Operator. What is the Precedence of Arithmetic Operators? Arithmetic operators follow the same precedence rules as in mathematics, and they are: exponential is performed first, multiplication and division are performed ne
3 min read
What Does $ Mean in Python?
Python programming language has several operators that are used to perform a specific operation on objects. These operators are special or special characters that perform a specific task, such as arithmetic operators, logical operators, assignment operators, etc. In this article, we will learn about another operator in Python which is used on Strin
2 min read
What does inplace mean in Pandas?
In this article, we will see Inplace in pandas. Inplace is an argument used in different functions. Some functions in which inplace is used as an attributes like, set_index(), dropna(), fillna(), reset_index(), drop(), replace() and many more. The default value of this attribute is False and it returns the copy of the object. Here we are using fill
2 min read
What does -1 mean in numpy reshape?
While working with arrays many times we come across situations where we need to change the shape of that array but it is a very time-consuming process because first, we copy the data and then arrange it into the desired shape, but in Python, we have a function called reshape() for this purpose. What is numpy.reshape() in Python The numpy.reshape()
3 min read
Python String Formatting - How to format String?
String formatting allows you to create dynamic strings by combining variables and values. In this article, we will discuss about 5 ways to format a string. You will learn different methods of string formatting with examples for better understanding. Let's look at them now! How to Format Strings in PythonThere are five different ways to perform stri
8 min read
Python String format() Method
The format() method is a powerful tool that allows developers to create formatted strings by embedding variables and values into placeholders within a template string. This method offers a flexible and versatile way to construct textual output for a wide range of applications. Python string format() function has been introduced for handling complex
11 min read
Python - Validate String date format
Given a date format and a string date, the task is to write a python program to check if the date is valid and matches the format. Examples: Input : test_str = '04-01-1997', format = "%d-%m-%Y" Output : True Explanation : Formats match with date. Input : test_str = '04-14-1997', format = "%d-%m-%Y" Output : False Explanation : Month cannot be 14. M
3 min read
Convert datetime string to YYYY-MM-DD-HH:MM:SS format in Python
In this article, we are going to convert the DateTime string into the %Y-%m-%d-%H:%M:%S format. For this task strptime() and strftime() function is used. strptime() is used to convert the DateTime string to DateTime in the format of year-month-day hours minutes and seconds Syntax: datetime.strptime(my_date, "%d-%b-%Y-%H:%M:%S") strftime() is used t
2 min read
How to format a string using a dictionary in Python
In this article, we will discuss how to format a string using a dictionary in Python. Method 1: By using str.format() function The str.format() function is used to format a specified string using a dictionary. Syntax: str .format(value) Parameters: This function accepts a parameter which is illustrated below: value: This is the specified value that
2 min read
Convert the column type from string to datetime format in Pandas dataframe
While working with data in Pandas, it is not an unusual thing to encounter time series data, and we know Pandas is a very useful tool for working with time-series data in Python.Let's see how we can convert a dataframe column of strings (in dd/mm/yyyy format) to datetime format. We cannot perform any time series-based operation on the dates if they
4 min read
Article Tags :
Practice Tags :