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

前端 未结 3 915
时光说笑
时光说笑 2021-01-25 08:16

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

3条回答
  •  忘掉有多难
    2021-01-25 09:08

    String starting with f are formatted string litrals.

    Suppose you have a variable:

    pi = 3.14

    To catenate 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

提交回复
热议问题