Python %s - String Formatting
In this tutorial, we will learn about how we can implement and use %s in our Python program. We will also learn the uses of %s in the strings.
%s in a string format in Python
Basically, the % symbol is used with a large variety of data having many data types and configurations in Python.
Talking about %s, it is specifically used to perform concatenation of two or more strings together in Python. The %s allow us to format or place a string or numerical value within a given string. In simple language, the %s in Python is used to incorporate a given string within another string. This operator automatically provides type conversion of a given value to string data type.
We put the % operator, where we have to specify the string. The numbers of values that we want to append into the given string should be equal to the numbers of %s operators that have been specified in the parentheses between the strings.
The following Python code will explain the way by which we perform string formatting using %s operator:
Hello Python developers! Welcome to, JavaTpoint!
As we can see in the above code, we have declared a string variable, i.e., str, and appended it inside the given string in parenthesis. We appended the str string value using the %s operator.
Uses of %s operator in Python
In this section, we will discuss the highlighted uses of the %s operator. We will also learn the implementation of the %s operator by multiple approaches and how it is helpful to us.
We will discuss the following approaches of implementing %s:
1. Multiple %s in a string:
We can use the %s operator to append the given string variable inside a string by putting it where we want to add the value. Python will simply add the string variables where we have used the %s operator in the string. Let's go through an example to understand it.
Example: Look at the following code:
Hello Python developers, Welcome to the JavaTpoint! We hope you are having a good day.
We have appended multiple string variables (mkr1, mkr2, mkr3) inside a single string and printed the string. We have appended these variables by simply using the %s operator multiple times within the string.
2. Using %s for mapping string:
We can also use the %s operator for mapping the string within the given program. We can map multiple string variables into a single string by simply using %s operators multiple times. However, when we want to add multiple string variables in a single string, we have to match the number of occurrences of %s in the string with the number of strings to replace it. We have to add that number of strings after the % operator.
Resultant mapped string using '%s' operator: Hey Python Developers Welcome to JavaTpoint
Note: But, if we don't give the same number of string variables after % operator, as the %s operator we have used, then the code will through a TypeError in the output like the following program:
Example - 2
Traceback (most recent call last): File "C:\Users\Manish\Downloads\code.py", line 9, in
3. Using %s for a list as a string:
With the help of the %s operator, we can also map a given list data type variable with the string variables to form a single string. We have to follow the same set of instructions that we follow while mapping string variables. Let's go through an example to understand it.
4. Order dictionary with %s:
We can define multiple string variables inside a dictionary data type variable. Then, we can call out these string variables by the order we want to, just by using the %s operator with the string variable name.
Resultant mapped string with ordered variable from dictionary: Learning %s operator concept at JavaTpoint
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week