'What does 'f' mean before a string in Python?

I'm new here and also new to Python. I wonder what f in print(f'Column names are {"-".join(row)}') does. I tried deleting it and then Column names are {"-".join(row)} become normal string.

Could you please tell me what f calls, so I can google to learn more about it? Thanks guys.

import csv

with open('CSV_test.txt') as csv_file: 
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    for row in csv_reader:
        if line_count == 0:
            print(f'Column names are {"-".join(row)}')
            line_count += 1
        else:
            print(f'\t{row[0]} works in the {row[1]} '
                  f'department, and was born in {row[2]}.')
            line_count += 1
    print(f'Processed {line_count} lines.')


Solution 1:[1]

This is called f-strings and are quite straightforward : when using an "f" in front of a string, all the variables inside curly brackets are read and replaced by there value. For example :

    age = 18
    message = f"You are {age} years old"
    print(message)

Will return "You are 18 years old"

This is similar to str.format (https://docs.python.org/3/library/stdtypes.html#str.format) but in a more concise way.

Solution 2:[2]

String starting with f are formatted string literals.

Suppose you have a variable:

pi = 3.14

To concatenate it to a string you'd do:

s = "pi = " + str(pi)

Formatted strings come in handy here. Using them you can use this do the same:

s = f"pi = {pi}"

{pi} is simply replaced by the value in the pi

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rowin
Solution 2 questionto42standswithUkraine