1
hilarious = False

joke_evaluation = "Isn't that joke so funny?! {}"

print(joke_evaluation.format(hilarious))

For the following lines of Python code I'm failing to understand a key concept.

A string is assigned to the variable joke_evaluationand include {} to embed another variable within it.

The third line of code has got me stuck, we are saying print the variable joke_evaluation then using the .format() function and passing another variable to it - hilarious which is set as a boolean data type.

Are the {} effectively functioning as placeholders? How does the .format() function know to populate the {} with the variable hilarious?

Please explain in basic terms if possible to increase my understanding, I'm failing to understand how Python populates the curly braces {} as I've mentioned above.

1
  • yes, it's placeholders. and format is bound to str not to print Commented Jan 10, 2019 at 10:19

3 Answers 3

1

Here's my understanding of the the format method:

Any string with curly braces {} will be replaced with the variable you have provided. So, if I have a string say:

myStr = "hello {}"

then doing:

res = myStr.format("user")
print(res) #prints "hello user" without quotes.

Now, doing this:

res = myStr.format(123123)
print(res) #prints "hello 123123" without quotes.

As you might have guessed, the integer 123123 was implicitly converted to string before being included in the string.

Now, coming to the curly {} part:

  1. You can have multiple curly braces and must have the same number of parameters passed to format method. Eg: myStr = "hello {},{},{}, nice meeting you" res = myStr.format("abcd",123,"lol") print(res) #prints "hello abcd,123,lol, nice meeting you"
  2. You can even put indices in the {} to indicate position like {0} and {1}.
Sign up to request clarification or add additional context in comments.

Comments

1

Yes, {} acting as placeholders, which is treated by .format method in special way.

How does the .format() function know to populate the {} with the variable hilarious?

If you're providing {} only, it's substituted position-wise, i.e.

>>> 'first: {}, second: {}'.format(1, 2)
'first: 1, second: 2'

For more verbose or re-usable substitution you can use named arguments:

>>> "{actor1} tells {actor2} that he's {actor1}".format(actor1='Bob', actor2='Joel')
"Bob tells Joel that he's Bob"

More on awesome string formatting: pyformat.info

A bit more on formatting, when .format substituting placeholders with some objects, it calls __format__ method on it, which

  1. Accepts formatting spec — which gives you ability to control how it will be converted (for example, '{:.2f}'.format(3.1415)
  2. Return str, which will actually substitute placeholders

1 Comment

you're forgetting that arguments are converted to str, which explains why booleans are printed as strings.
0

Read Python documentation about string: https://docs.python.org/3.6/library/string.html?highlight=formatting

:) Everything you need to know. You can also change Python versions and see the behavior of formatting.

Scroll down to see examples and explanations.

2 Comments

Just to expand I found this useful link that explains quite well with the Returntype etc: geeksforgeeks.org/python-format-function Is this kind of documentation that explains not available on the official Python docs?
@user1554264 it is! Just read the link above, it is all in there with examples and explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.