What is print(f“…”)

后端 未结 4 1087
梦毁少年i
梦毁少年i 2020-12-09 04:33

I am reading through a python script that takes an input of XML files and outputs an XML file. However, I do not understand the printing syntax. Can someone please explain w

4条回答
  •  天涯浪人
    2020-12-09 05:29

    the f string is also known as the literal string to insert a variable into the string and make it part so instead of doing

    x = 12
    y = 10
    
    word_string = x + ' plus ' + y + 'equals: ' + (x+y)
    

    instead, you can do

    x = 12
    y = 10
    
    word_string = f'{x} plus {y} equals: {x+y}
    output: 12 plus 10 equals: 22
    

    this will also help with spacing due to it will do exactly as the string is written

提交回复
热议问题