So I have been browsing through online sites to read a file line by line and I come to this part of this code:
print("Line {}: {}".format(linecount, line))
I am quite confused as to what is happening here. I know that it is printing something, but it shows:
"Line{}"
I do not understand what this means. I know that you could write this:
foo = "hi"
print(f"{foo} bob")
But I don't get why there are empty brackets.
Empty braces are equivalent to numeric braces numbered from 0
:
>>> '{}: {}'.format(1,2)
'1: 2'
>>> '{0}: {1}'.format(1,2)
'1: 2'
Just a shortcut.
But if you use numerals you can control the order:
>>> '{1}: {0}'.format(1,2)
'2: 1'
Or the number of times something is used:
>>> '{0}: {0}, {1}: {1}'.format(1,2)
'1: 1, 2: 2'
Which you cannot do with empty braces.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With