Python String Format

Let say, we want to write a program with Python that is dynamic and static. This means an eCommerce website.  We are working on the login page and when a user login to his profile while we want to display his name or what they have in the cart. In that case we don’t want to just hard code and write every single’s username like ‘Muhammad’, ‘Ahmad’ or every single name in the world. This article is all about Python String Format.

Ideally we can do is have something dynamically where let’s say it’s a profile page and we simply use the name variable and we display that on the page. The name variable should be equal to the name of the user that logs into his profile. Lets understand Python String format by using two examples.

Example 1:

Example_1.png

In here we want to display the name “Muhammad” to the profile page. We can simple print the name like above code instead of writing every single name; we can store the name of user into a variable and print that variable. In this way we will get the name of the user that logs in. We also greeted the user with “hi”.

What if we also have the age of the user? Like this:

In here we added the age of the user with the “age” variable then printed the user’s name and age with some greetings but see we have used string conversion at “str(age)”.  If we don’t use the string conversion or type conversion then we will get an error. So, we can write this code with Python String Format by just adding “f” keyword at the beginning instead of using these plus and using type conversion. We can just simple write it like this:

Example 2:

Example_2.png

I have written the above code with formatted string. The “f” keyword is going to tell python that this is formatted string. The use of “f” will also avoid that error of type conversion.  The result of the both examples will be same but the different is one written with formatted string and other is without it.

Note: The python string formatting is only available in python 3 and it’s not available in the older versions of python like version 2. So, before python 3 you didn’t have the “f” keyword.

READ MORE Top 7 Python Jobs for Career

Example 3:

In python 2 we have the idea of “.format()”. Instead of using “f” at the beginning we only use the .format() at the end of the string like this:

Python String Format

In here we just remove the “f” keyword from the beginning and also removed the variables from the curly {} bracket and add .format() with the values of name and age variables.

Remember that .format() works on the strings. If you did it outside of the string, it will do the format on entire expression but you know the “print” is a function and it’s not a string. So, this piece of code will gives you the error:

Python String Format

This will produce an error because we are doing .format() on  the print function. So, print is not a string and you will get the error. You have to perform the .format() only on the string like the Example 3. You can also put the names of variables instead of putting their values in the .format() like this:

Python String Format

Conclusion

So, this is the simple use of Python String Format. You have to remember the common rules that er talke here to avoid the errors like type conversion error and performing .format() on the print function instead of performing on the string. The “f” keyword is only available in python 3 and .format() is available in the python 3.

If you link this article please share it with your friends on Facebook, Twitter etc.

Muhammad Nauman

Muhammad Nauman a Linguistics Scholar, Content Writer, and Teacher of English as a Second Language is a passionate and dedicated person to his interests in language. He has been teaching for three years in various institutes and has a keen interest in language learning and teaching and is a researcher as well.

Leave a Reply

Your email address will not be published. Required fields are marked *