[Zero To Python] Day 7 - String Concatenation, Interpolation, and Formatting
Behold the joy of text formatting!
Today in our journey to building a journal program with Python, we are going to work on text formatting. So far we’ve done basic print statements and those work well enough for basic output. However, stuff gets real clunky when you want to output variables and format output to look the way you want.
To solve that I’m going to introduce the ideas of string concatenation, string interpolation, and doing a bit more with the formatting you already learned about. So, let’s write some code!
Let’s do some text formatting…
To begin with we are starting with the previous code.
One thing I noticed is that we need to add a newline to the journal_text
input. That will make the user experience match our design.
Okay, that looks good. Now let’s move on to cleaning up the output of our program.
String Concatenation Example
The next thing we are going to do is what is called string concatenation. That is a nice way to say combining strings together to form a longer string. That is achieved in Python by using a +
between two strings. It looks like this:
and the output…
As you notice we are changing the output to include an additional newline after our journal_date
output. In the video embedded above you can see more complex examples of combining multiple strings and variables together in different ways.
The big idea here is you can use the plus sign to combine strings in Python.
String Interpolation Example
Next we are going to do the same thing using Python’s formatted strings, also known as string interpolation. The idea here is instead of just gluing strings together you are injecting a formatted string with variables to create a new string.
String interpolation in some cases can be more elegant than concatenation. It can make your code more readable and easier to understand. Here is what the above example looks like using string interpolation.
As you can see the approach is slightly different. To use a formatted string in Python, you put an f
in front of the first double quote and that tells Python it’s a formatted string. Then you place variables inside of curly brackets where you would want them included. In the above example that is what {journal_date}
is doing.
So that is string interpolation via Python’s formatted strings.
NOTE: Formatted strings are a Python 3 specific feature and is not available in previous versions of Python.
Bonus Fun
If you want to get crazy with some bonus fun the entire output could be done in a single line with string interpolation. It ends up looking like this:
As you can tell that is some what of a space saver in some regards, but harder to understand in others. Be careful how you use string interpolation because it can get out of hand.
So that is a bit about formatting our output using string concatenation and string interpolation. Going forward you can expect to see more of both techniques in future lessons.
Assignment
Your assignment today is to use your choice of either string concatenation or interpolation to format the text output to mach the original designs we did in previous lessons.
For bonus you could play around with both concatenation and interpolation to see how you can use them in interesting ways.
Useful Links
Input + Text Formatting = Magic
You now have enough knowledge to write a lot of different types of programs and you might not even realize it. When I was first learning to code I wrote a lot of command line programs like simple number guessing programs, calculators, and so on. Most of it boiled down to taking user input, maybe doing some calculations, and then giving formatted text output.
In just the first week of this course you’ve learned all of those things. That is pretty cool, but we aren’t done yet. So, I’ll see you tomorrow for the next step forward!
-Brian