In Python, you have a few options to format your strings. In this article, I will go over "Hello, my name is {}. I am a {} turned {}."3, formatted string literals, and template strings.
But first, let's take a look at what is considered to be the "old way" of formatting strings.
What is % string formatting in Python?
One of the older ways to format strings in Python was to use the "Hello, my name is {}. I am a {} turned {}."4 operator.
Here is the basic syntax:
"This is a string %s" % "string value goes here"You can create strings and use "Hello, my name is {}. I am a {} turned {}."5 inside that string which acts like a placeholder. Then you can write "Hello, my name is {}. I am a {} turned {}."4 followed be the actual string value you want to use.
Here is a basic example using "Hello, my name is {}. I am a {} turned {}."4 string formatting.
print("Hi, my name is %s" % "Jessica")This method is often referred to as the "older" way because Python 3 introduced "Hello, my name is {}. I am a {} turned {}."3 along with formatted string literals.
What is the "Hello, my name is {}. I am a {} turned {}."3 method in Python?
Here is the basic syntax for the "Hello, my name is {}. I am a {} turned {}."3 method:
"template string {}".format(arguments)Inside the template string, we can use .format("Jessica", "musician", "programmer")1 which act as placeholders for the arguments. The arguments are values that will be displayed in the string.
In this example, we want to print .format("Jessica", "musician", "programmer")2
In the string, we are going to have a total of three .format("Jessica", "musician", "programmer")1 which will act as placeholders for the values of Jessica, musician, and programmer. These are called format fields.
"Hello, my name is {}. I am a {} turned {}."Inside these parenthesis for the "Hello, my name is {}. I am a {} turned {}."3, we will use the values of "Jessica", "musician", and "programmer".
.format("Jessica", "musician", "programmer")Here is the complete code and printed sentence:
print("Hello, my name is {}. I am a {} turned {}.".format("Jessica", "musician", "programmer"))Positional arguments
You can access the value of these arguments using an index number inside the .format("Jessica", "musician", "programmer")1.
In this example, we have two arguments of .format("Jessica", "musician", "programmer")6 and .format("Jessica", "musician", "programmer")7 inside the .format("Jessica", "musician", "programmer")8.
.format("trumpet", "drums")We can access those values inside the string by referring to the index numbers. .format("Jessica", "musician", "programmer")9 refers to the first argument of .format("Jessica", "musician", "programmer")6 and print("Hello, my name is {}. I am a {} turned {}.".format("Jessica", "musician", "programmer"))1 refers to the second argument of .format("Jessica", "musician", "programmer")7.
"Steve plays {0} and {1}."Here is the complete code and printed sentence:
print("Steve plays {0} and {1}.".format("trumpet", "drums"))We can modify this example and switch the index numbers in the string. You will notice that the sentence has changed and the placement of the arguments is switched.
print("Steve plays {1} and {0}.".format("trumpet", "drums"))Keyword arguments
These arguments consist of a print("Hello, my name is {}. I am a {} turned {}.".format("Jessica", "musician", "programmer"))3 print("Hello, my name is {}. I am a {} turned {}.".format("Jessica", "musician", "programmer"))4 pair. We can access the print("Hello, my name is {}. I am a {} turned {}.".format("Jessica", "musician", "programmer"))4 of the argument by using the print("Hello, my name is {}. I am a {} turned {}.".format("Jessica", "musician", "programmer"))3 inside the .format("Jessica", "musician", "programmer")1.
In this example, we have two keys called print("Hello, my name is {}. I am a {} turned {}.".format("Jessica", "musician", "programmer"))8 and print("Hello, my name is {}. I am a {} turned {}.".format("Jessica", "musician", "programmer"))9. We are going to use those keys inside the string.
print("Hi, my name is %s" % "Jessica")0Inside the .format("Jessica", "musician", "programmer")8, we have the print("Hello, my name is {}. I am a {} turned {}.".format("Jessica", "musician", "programmer"))3 print("Hello, my name is {}. I am a {} turned {}.".format("Jessica", "musician", "programmer"))4 pairs.
print("Hi, my name is %s" % "Jessica")1Here is the complete code and printed sentence.
print("Hi, my name is %s" % "Jessica")2How to Mix Keyword and Positional arguments
In the "Hello, my name is {}. I am a {} turned {}."3 you can mix keyword and positional arguments.
In this example, we are going to create a short story about going to Disneyland.
We are first going to create a few variables for name, number, adjective and a Disneyland ride.
print("Hi, my name is %s" % "Jessica")3We then want to create our string using keyword and positional arguments. I am going to add the .format("trumpet", "drums")4 to tell the computer to create a new line after each sentence.
print("Hi, my name is %s" % "Jessica")4Inside the parenthesis for the "Hello, my name is {}. I am a {} turned {}."3, we will assign our variables to the keys of .format("trumpet", "drums")6, print("Hello, my name is {}. I am a {} turned {}.".format("Jessica", "musician", "programmer"))9, .format("trumpet", "drums")8 and .format("trumpet", "drums")9. .format("Jessica", "musician", "programmer")9 will have the value of "Steve plays {0} and {1}."1.
print("Hi, my name is %s" % "Jessica")5Here is the complete code and printed sentence:
print("Hi, my name is %s" % "Jessica")6What are formatted string literals?
Formatted string literals (or f-strings) allow you to include expressions inside your strings. Just before the string you place an "Steve plays {0} and {1}."2 or "Steve plays {0} and {1}."3 which tells the computer you want to use an "Steve plays {0} and {1}."4.
Here is the basic syntax:
print("Hi, my name is %s" % "Jessica")7Here is a basic example that prints the sentence "Steve plays {0} and {1}."5
print("Hi, my name is %s" % "Jessica")8It works just the same if I use a capital "Steve plays {0} and {1}."3 before the string.
print("Hi, my name is %s" % "Jessica")9You can also use an "Steve plays {0} and {1}."4 to format data from a dictionary.
In this example, we have a dictionary which represents the top rankings for men's college basketball teams and how many games they won out of 32.
"template string {}".format(arguments)0We can use a "Steve plays {0} and {1}."8 and the "Steve plays {0} and {1}."9 method to go through each of the print("Steve plays {0} and {1}.".format("trumpet", "drums")) 0 pairs of the print("Steve plays {0} and {1}.".format("trumpet", "drums")) 1 dictionary.
"template string {}".format(arguments)1Inside the "Steve plays {0} and {1}."8, we can use an "Steve plays {0} and {1}."4 to format the printed results.
The use of the print("Steve plays {0} and {1}.".format("trumpet", "drums")) 4 for print("Steve plays {0} and {1}.".format("trumpet", "drums")) 5 and print("Steve plays {0} and {1}.".format("trumpet", "drums")) 6 tells the computer to create a field that is 10 characters wide. This will create even columns for the data.
The print("Steve plays {0} and {1}.".format("trumpet", "drums")) 7 inside here print("Steve plays {0} and {1}.".format("trumpet", "drums")) 6 refers to a decimal integer.
Here is the full code and the printed output:
"template string {}".format(arguments)3What are template strings?
Template strings are Python strings that use placeholders for the real values.
Here is the basic syntax:
"template string {}".format(arguments)4Let's take a look at an example to better understand how it works.
In this example, we want to print print("Steve plays {0} and {1}.".format("trumpet", "drums")) 9 using template strings.
In order to use template strings, you will first have to import the print("Steve plays {1} and {0}.".format("trumpet", "drums")) 0 class from the standard library.
"template string {}".format(arguments)5You can then use the print("Steve plays {1} and {0}.".format("trumpet", "drums")) 0 class and provide a string inside the parenthesis. We are going to place a print("Steve plays {1} and {0}.".format("trumpet", "drums")) 2 in front of .format("trumpet", "drums")6 which will later be replaced by the real value.
"template string {}".format(arguments)6We then add print("Steve plays {1} and {0}.".format("trumpet", "drums")) 4 to the template and assign the value of print("Steve plays {1} and {0}.".format("trumpet", "drums")) 5 to .format("trumpet", "drums")6.
"template string {}".format(arguments)7Here is the full code and the printed output:
"template string {}".format(arguments)8Conclusion
There are many ways to format your strings in Python.
The older way of formatting your strings would be to use the "Hello, my name is {}. I am a {} turned {}."4 operator.
"template string {}".format(arguments)9"Hello, my name is {}. I am a {} turned {}."5 acts as a placeholder for the real value. You place the real value after the "Hello, my name is {}. I am a {} turned {}."4 operator.
This method is often referred to as the "older" way because Python 3 introduced "Hello, my name is {}. I am a {} turned {}."3 and formatted string literals (f-strings).
In the "Hello, my name is {}. I am a {} turned {}."3 method, you use .format("Jessica", "musician", "programmer")1 for placeholders and place the real values inside the parenthesis. This method can take in positional and keyword arguments.
"template string {}".format(arguments)Formatted string literals (or f-strings) allow you to include expressions inside your strings. Just before the string you place an "Steve plays {0} and {1}."2 or "Steve plays {0} and {1}."3 which tells the computer you want to use an "Steve plays {0} and {1}."4.
print("Hi, my name is %s" % "Jessica")7You can also use Template strings by importing the print("Steve plays {1} and {0}.".format("trumpet", "drums")) 0 class from the standard library. Template strings are Python strings that use placeholders for the real values.
"template string {}".format(arguments)4I hope you found this article helpful and best of luck on your Python journey.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
I am a musician and a programmer.
If you read this far, tweet to the author to show them you care. Tweet a thanks
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started