Python Forum
Confused by {} in Python code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Confused by {} in Python code
#1


I downloaded some Python code that has a line of code like this:

print('M = {}'.format(M))

where M is defined like this:

M = ((n - m) * np.log(np.linalg.det(Xp))) \
- (n_1 - 1) * (np.log(np.linalg.det(np.cov(X0)))) - (n_2 - 1) * (np.log(np.linalg.det(np.cov(X1))))

---

I'm confused what's going on with the "{}" inside the 'M = {}'.

What do the braces represent/do in this context?

Thanks very much in advance,

-O
Reply
#2
In your code snippet, The {} is a placeholder for formatted text, see: https://docs.python.org/3/tutorial/inputoutput.html
you will also see '{' and '}' as bounds for creating a dictionary.
Reply
#3
It is a way to format strings in Python. Similar to using % notation in Python 2, C language etc. So in your case {}  gets substituted with value of M, which is calculated with the formula you provided.

Here you can find documentation on it and a few simple examples:
https://docs.python.org/3.4/library/stri...t-examples
Reply
#4
In version 3.6.3 you can also use: print(f"M = {M}")
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
#5
It's called string formatting.
The evolution of string formatting.
>>> M = 100
>>> # old don't use
>>> print('M = %d' % M)
M = 100
>>> 
>>> # Python 2.6 we get format()
>>> print('M = {}'.format(M))
M = 100
>>> 
>>> # Python 3.6 we get f-string
>>> print(f'M = {M}')
M = 100
f-string is the future,but format() will be used for a long time.
f-string can take expressions here calculate and upper():
>>> a = 10
>>> b = 20
>>> s = 'correct'
>>> print(f'The sum of a {a} and b {b} is {a+b} {s.upper()}')
The sum of a 10 and b 20 is 30 CORRECT
More about format() PyForamt.
Reply
#6
(Dec-03-2017, 04:11 PM)snippsat Wrote: More about format() PyFormat.

Nice answer and awesome resource, thanks!
Reply
#7
(Dec-03-2017, 04:06 PM)j.crater Wrote: It is a way to format strings in Python. Similar to using % notation in Python 2, C language etc. So in your case {}  gets substituted with value of M, which is calculated with the formula you provided.

Here you can find documentation on it and a few simple examples:
https://docs.python.org/3.4/library/stri...t-examples

Thanks very much!

That was exactly what I needed.

Sorry, I should have RTFM first. :)

- O

(Dec-03-2017, 04:11 PM)snippsat Wrote: It's called string formatting.
The evolution of string formatting.
>>> M = 100
>>> # old don't use
>>> print('M = %d' % M)
M = 100
>>> 
>>> # Python 2.6 we get format()
>>> print('M = {}'.format(M))
M = 100
>>> 
>>> # Python 3.6 we get f-string
>>> print(f'M = {M}')
M = 100
f-string is the future,but format() will be used for a long time.
f-string can take expressions here calculate and upper():
>>> a = 10
>>> b = 20
>>> s = 'correct'
>>> print(f'The sum of a {a} and b {b} is {a+b} {s.upper()}')
The sum of a 10 and b 20 is 30 CORRECT
More about format() PyForamt.


Excellent! I've copied these for reference.

- O
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  C++ programmer confused about why Python isn't working the way I intend Radical 2 685 Sep-15-2023, 04:21 AM
Last Post: Radical
  String int confused janeik 7 1,016 Aug-02-2023, 01:26 AM
Last Post: deanhystad
  I am confused with the key and value thing james1019 3 912 Feb-22-2023, 10:43 PM
Last Post: deanhystad
  Confused about python execution jpezz 4 1,324 Oct-09-2022, 06:56 PM
Last Post: Gribouillis
  Pandas confused DPaul 6 2,466 Sep-19-2021, 06:45 AM
Last Post: DPaul
  is and '==' i'm confused hshivaraj 6 2,625 Sep-15-2021, 09:45 AM
Last Post: snippsat
  Confused with 'flags' tester_V 10 4,783 Apr-12-2021, 03:03 AM
Last Post: tester_V
  Simple Tic Tac Toe but I'm confused Izith 1 2,152 Sep-26-2020, 04:42 PM
Last Post: Larz60+
  I am really confused with this error. Runar 3 2,926 Sep-14-2020, 09:27 AM
Last Post: buran
  Confused on how to go about writing this or doing this... pythonforumuser 3 2,420 Feb-10-2020, 09:15 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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