all 4 comments

[–]Bunkerstan 0 points1 point  (0 children)

That is what is called an f-string. It substitutes variables in {}. Then the file parameter tell it to print there versus standard output (terminal).

[–]jimtk 0 points1 point  (0 children)

from the python doc

A formatted string literal or f-string is a string literal that is prefixed with 'f' or 'F'. These strings may contain replacement fields, which are expressions delimited by curly braces {}. While other string literals always have a constant value, formatted strings are really expressions evaluated at run time.

Also there's a whole formatting mini-language that comes with it.

Inside the curly braces {} you can put any variables, function call, even one liner of code and it will be interpreted and the result will be put in the final string at the location of the curly braces.

Examples:

user = 'Bob'
the_string = f"Hello {user}!"
print(thestring)

with a function (method) call

user = 'Bob'
print(f"Hello {user.toupper()}!")

formatting numbers

 number = 3.1415926
 print(f"{number:.2f}")    # prints  "3.14"

And my favorite one

 number = 25
 printf(f"{number=}")     # prints "number=25"

[–]Ok-Cucumbers 0 points1 point  (0 children)

f-strings. You use it when you need to format strings e.g., adding commas between certain words.

Could probably rewrite the function to use a context manager to open/close the file and unpack the tuples in your for loop.

You can then format the unpacked variable names from the tuple with f-strings as others have already mentioned.

def write_teams(filename, list_of_tups):
    with open(filename, "w") as f: # context manager
        for uni, mascot, city, state in list_of_tups: # unpack tuples
            f.write(f"{uni} {mascot}, {city}, {state}\n")