0

I am working on python 3.6 64 bit.

Here is my code:

days = "Mon Tue Wed Thu Fri Sat Sun"

print("Here are the days",format(days))

The output I got is

Here are the days Mon Tue Wed Thu Fri Sat Sun

I didn't add "{}" in my string. Also I used a comma "," instead of a dot "."

My understanding was that format() will replace {} in string with its arguments.

Question : How did format() worked without {} and . operator

2
  • 1
    Look at your code again. You're asking it to take a string (days) and format it with a string. Days isn't a dictionary or array. Commented Oct 18, 2016 at 2:52
  • The code should have been Commented Oct 18, 2016 at 3:18

3 Answers 3

2

I think you're thinking what's happening is similar to:

print("Here are the days {}".format(days))

However, what's actually happening is that you're passing in multiple arguments to print(). If you look at the docs for print(), it takes a couple of parameters:

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

The asterisk in front of objects indicates it can take multiple arguments. Right now you're passing in "Here are the days" as the first argument, and format(days) as the second, which results in:

Here are the days Mon Tue Wed Thu Fri Sat Sun
Sign up to request clarification or add additional context in comments.

Comments

0

The print function prints the arguments one right after the other, with a space in between. The format call, finding no replacements, is doing nothing and returning the original string.

Here are some examples that may help:

>>> colors = "Red Blue"
>>> print("Here are the colors", format(colors))
Here are the colors Red Blue
>>> print("", format(colors))
 Red Blue
>>> print(format("abc"), format("xyz"))
abc xyz
>>> print("Here are the colors", colors)
Here are the colors Red Blue

Comments

0

Some of the confusion here may be the difference between the format() function and the related "string".format() method. The function invokes the object's (first argument) __format__() method with a format specification (second argument). If the second argument is None, then it's just like str(). The format() method of str walks the string on which it is invoked, looking for substitution brackets which may, or may not contain format specifications to be applied similar to the format() function. An example:

>>> # format() function which invokes float.__format__()
>>> format(0.324, "+.4%")
'+32.4000%'
>>>
>>> # str format() method that invokes various object.__format__() methods
>>> # if a format specification is found in the substitution brackets:
>>> "{:+.4%} rise in {}".format(0.324, "temperature")
'+32.4000% rise in temperature'
>>> 

Question : How did format() worked without {} and . operator

It worked because without the '.' operator, you mistakenly invoked the format() function which, with no second argument, simply return the string. The format() function doesn't use {} brackets. It's the format() method of str, invoked by the '.' operator, that uses {} brackets to make substitutions, some of which may require formatting.

Perhaps it might have been less confusing if the str method had a different name like "{} string {}".interpolate(x, y) as formatting is only part of what it does.

1 Comment

Thanks for the explanantion

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.