Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New to Python
#2
Hi OptimusBri,

As it looks like you have gathered, both lines of code produce similar output.

The difference comes down to the fact that the line "formats" the word "snow" and it wil insert this formatted text between the "{}".

There are a number of reasons for fomatting text, the main two are:
1) To convert the data type of the variable. This can be to change from a numeric variable to a string variable. The reason for this can be to ensure that the variable can be accessed by string manipulation commands.
2) The second is to display the variable in a certain way or "format" e.g. width or alignment. You may wish to have you string variables ("text") display left justified and you numeric variables displayed right justified. Having numbers left justifies can cause problems:
1
100
1000
------
Trying to add these together would give you a "result" of 3000 when the actual result is 1101.
This can also cause issues when sorting the data as, in the above example, the values will all appear as the same. A little artistic license is taken with these examples, but hopefully will give you an idea.

The format "command" is a part of the Python system as opposed to a function developed to enhance the existing system. Further details of this format function can be seen at this link. Some of these options are "similar" to the concept of formatting cells in Excel.

https://docs.python.org/3.4/library/func...ml?#format

Hope this gives you some help to answering you question.

Good Luck

Bass

"The good thing about standards is that you have so many to choose from" Andy S. Tanenbaum
Reply


Messages In This Thread
New to Python - by OptimusBri - Jul-04-2017, 07:30 PM
RE: New to Python - by Bass - Jul-04-2017, 08:08 PM
RE: New to Python - by sparkz_alot - Jul-04-2017, 08:09 PM
RE: New to Python - by OptimusBri - Jul-04-2017, 09:09 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020