Lesson Contents
Before Python 3.6, you had two options to format your strings:
- % formatting.
- str.format().
The F-string formatting option has been available since Python 3.6.
In this lesson, I’ll show you why the older % formatting and str.format() options to format your strings are obsolete nowadays and how the F-string formatting option makes your life much easier.
% Formatting
The %-formatting option has been available since the beginning of Python. The % operator is built into string objects. You can use this to format strings. For example:
You can use the % operator multiple times, but you’ll need to use a tuple () to supply the values you want to use to format your string:
>>> router_hostname = "R1"
>>> switch_hostname = "SW1"
>>> print("Device hostnames are %s and %s." % (router_hostname, switch_hostname))
Will show this output:
Device hostnames are R1 and SW1.
If you only want to format your strings with one or two values, the % operator works fine. However, with many values to replace, your code becomes difficult to read:
>>> vendor = "Cisco"
>>> platform = "ISR 4311"
>>> hostname = "R1"
>>> uptime = "3600"
>>> ios_version = "Gibraltar-16.12.7"
>>> print("Router %s from vendor %s has hostname %s, runs OS %s and has uptime of %s seconds." % (platform, vendor, hostname, ios_version, uptime ))
This is the output:
Router ISR 4311 from vendor Cisco has hostname R1, runs OS Gibraltar-16.12.7 and has uptime of 3600 seconds.
In the code above, you can see the problem with the % operator. Your code becomes difficult to read because you can’t really tell which value replaces what % symbol.
str.format()
Python 3 has a newer way to format strings by using .format() on a string object. Here is a quick example that looks similar to the old %-formatting method.
>>> hostname = "SW1"
>>> uptime = "5436"
>>> print("Switch {} has an uptime of {} seconds.".format(hostname, uptime))
This is the output:
Switch SW1 has an uptime of 5436 seconds.
It is possible to specify the values you want to use to format your strings within the strings:
>>>sw1_hostname = "SW1"
>>> sw1_uptime = "5436"
>>> print("Switch {hostname} has an uptime of {uptime} seconds.".format(hostname=sw1_hostname, uptime=sw1_uptime))
This is the output:
Switch SW1 has an uptime of 5436 seconds.
This makes your code easier to read than the % formatting method. However, if you have many things in your string that you want to format, your code becomes messy:
>>> r1_hostname = "R1"
>>> r1_location = "Amsterdam"
>>> r1_status = "Online"
>>> r1_uptime = "7451"
>>> print("Router {hostname} located at {location} is {status} has an uptime of {uptime} seconds.".format(hostname=r1_hostname, location=r1_location, status=r1_status, uptime=r1_uptime))
This is the output:
Router R1 located at Amsterdam is Online has an uptime of 7451 seconds.
F-Strings
F-strings are easy to work with and keep your code readable. Here is a quick example:
>>> hostname = "SW1"
>>> vendor = "Cisco"
>>> platform = "Catalyst"
>>> print(f"Switch {hostname} from {vendor} is a {platform} switch.")
This is the output:
Switch SW1 from Cisco is a Catalyst switch.
Before the string, you add an “f”. This can be a small or capital letter. You can then add whatever you want to format in between {}. A huge advantage of this way of formatting is that your code remains easy to read, even when you want to format many things in your string. For example:
>>> vendor = "Cisco"
>>> platform = "8100"
>>> hostname = "R2"
>>> uptime = "7456"
>>> ios_version = "IOS XR"
>>> print(f"Router {hostname} from vendor {vendor} called {hostname} runs {ios_version} and has uptime of {uptime} seconds.")
This is the output:
Router R2 from vendor Cisco called R2 runs IOS XR and has uptime of 7456 seconds.
Python Expressions
I used variables like “vendor” or “platform” with assigned strings in the examples above. Python evaluates f-strings at runtime, allowing you to run any valid Python expressions in a string. Here is an example:
>>> uptime = 28800
>>> print(f"Router has an uptime of {uptime / 3600} hours.")
This is the output:
Router has an uptime of 8.0 hours.
In the example above, I use an arithmetic operator (division) in the f-string.
>>> vendor = "CiScO"
>>> print(f"Router is from vendor: {vendor.title()}.")
This is the output:
Router is from vendor: Cisco.
Here is one more example where I use the title() method:
Hello,
kindly, this code doesn’t work could you check:
Hello @ahmedamrici ,
This is working for me:
On Python 3.8.6.
titleis a string method.Rene
Thanks dear I had below error, I did correct it
AttributeError: ‘str’ object has no attribute ‘litle’. Did you mean: ‘title’?
but why don’t you show the print output in the lessons ?
We use trinket for lessons so it’s interactive:
https://cdn-forum.networklessons.com/uploads/default/original/2X/d/da061ff022e865166762f7707d1221fed7e8fc15.png
For local testing, it’s best to use vscode.
Hello Ahmedlmad
Using the Trinket applet, you can see the output in the pane on the right in the lessons. For the particular code you mentioned, I get this:
https://cdn-forum.networklessons.com/uploads/default/original/2X/d/db630971cda20c0a6989cb9e000036cf345ed331.png
You can see the output on the right. You can also display the console output by clicking the “Console” option as shown below:
https://cdn-forum.networklessons.com/uploads/default/original/2X/d/d06d89a6baeaccc49e54b451bed468b1343cee9e.png
Does that make sense?
I hope this has been helpful!
Laz