Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New to Python
#3
Really?  When I run:

print ("Its fleece was white as {}.".format(snow))
I get:

Error:
Traceback (most recent call last):  File "<stdin>", line 1, in <module> NameError: name 'snow' is not defined
In your first example, the '{}' acts as a place holder for the elements in the '.format()' segment, in this case it is looking for the variable named snow.

snow = "snow"
print("It's fleece was white as {}".format(snow))
Output:
Its fleece was white as snow.
In your second example you are simply printing the string "Its fleece was white as snow."

Please see String Formatting
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
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