The exercise “String Formatting with % - Part 1” mentions that using the % operator is a better method than string concatenation when you want to print a variable with a string. Since the exercise doesn’t really say why or how it is a better method, I tried looking online. The only thing that I found, that I could understand as a beginner, was that using the % operator is faster. Does anyone have any insight beyond this?
its certainly a lot cleaner and more readable in my opinion, all those
+ of string concatenation, you might easily miss one. In particular if you have integers as well, given with string concatenation you need to convert those to string.
It definitely sounds like it would be easier to use %, thank you for the insight
I read a little about that in the exercise, and it mentioned that it would be fazed out in later versions of python. Do you currently use the newest version of python and is this still something that is used?
Also, you typed .format() and it reminded me of something I saw in a previous exercise. Why do you have to include the dot? For example, one of the instructions in a previous exercise said to use .len(), whereas I have only seen len() used up until that point. Is the dot required for certain uses?
It is because the “Dot Notation” refers to using methods of a defined class. In this case, you are using the method .format() associated with the string class that allow you to use nice formatting features.
You can check those features at the link provided by stetim94
I sincerely appreciate the reply kingdarboja!
That just went way over my head hehe. I am familiar with defined strings but have no clue what a class is, or a defined class for that matter. Is this something that will be explained in later modules or would you advise that I look into this independently?
If you check the syllabus of the Python course, you will see there is going to be Object Oriented Programming [OOP] exercises that will explain such things like class, instance, method and so on.
If you need a quick understanding of such topic, you can check this at Object Oriented Programming or google it.
Thank you kindly!
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.