As we have learned before, strings are pretty cool. Python utilizes a C-style string formatting to make new and formatted strings. The modulo operator (%) would be utilized to format a set of variables that are encased in a tuple, together with a string format, which happens to consist of normal/regular text fused with argument specifiers like “%s” or “%d.”
What is a tuple?
Before moving forward, one must recognize what a tuple is. A tuple is a collection of objects which ordered and immutable. Tuples are sequences, just like lists. A tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets. Just remember, tuples are used to store multiple items in a single variable; this is a collection that is ordered and not changeable. In a future guide, we will go over tuples in detail.
Example:
x = ("a","b","c")
print(x)
Also, when it comes to tuples, it can allow for items to have the same value since they are indexed.
Example:
x = ("a","b","c","a","c")
print(x)
Back to string formatting: in a hypothetical situation, say if you were to have a variable labeled as “name” with a first and last name pinned to it’s value, you can print a message to that transcribes a greeting (or whatever you like to say).
Example:
first_name = "John"
last_name = "Doe"
name = first_name + " " + last_name
print("Howdy, %s!" % name)
What if you were to have an urge to have multiple argument specifiers? You can utilize a tuple.
Example:
first_name = "John"
last_name = "Doe"
name = first_name + " " + last_name
age = 21
print("Howdy, %s!, I hear you are %d years of age!" % (name, age))
If an object is not a string, you can still use the “%s” operator to format it. The string will come back from the, “repr” method, formatted as a string (given it is from the object).
Note: The repr() function returns a printable representation of the given object.
Example:
x = [21,7,2,98]
print("My favorite numbers are: %s" % x)
You may still be confused on what some of those basic operators may possibly be, please take a look at the chart to have a better sense of what the argument specifiers do.
Character | Description |
%c | Character |
%s | String conversion via str() prior to formatting |
%i | Signed decimal integer |
%d | Signed decimal integer |
%u | Unsigned decimal integer |
%o | Octal integer |
%x | Hexadecimal integer using lowercase letters |
%X | Hexadecimal integer using uppercase letters |
%e | Exponential notation with lowercase e |
%E | Exponential notation with uppercase e |
%f | Floating-point real number |
%g | The shorter of %f and %e |
%G | The shorter of %f and %E |
Exercise
For this exercise, we are going to test to see if we know how to use a string operator in order to format a string. Modify the code so that the output reads: Greetings, Joe Schmoe! You have an upcoming payment of $420.69. Please take a look at the solution if you get stuck!
Exercise:
data = ("Joe", "Schmoe", 420.69)
message = "Greetings"
print(message % data)
Solution:
data = ("Joe", "Schmoe", 420.69)
message = "Greetings, %s %s. You have an upcoming payment of $%s."
print(message % data)
Output:
<script.py> output:
Greetings, Joe Schmoe. You have an upcoming payment of $420.69.
For more information
If you seek to learn more about Python, please visit Python’s official documentation.
Michael is an Information Technology consultant, with a focus on cybersecurity. Every day, Michael strives to learn something new, with an aim to share it with you all!