What is print(f“…”)

后端 未结 4 1082
梦毁少年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:10

    The f means Formatted string literals and it's new in Python 3.6.


    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.


    Some examples of formatted string literals:

    >>> name = "Fred"
    >>> f"He said his name is {name}."
    "He said his name is Fred."
    
    >>> name = "Fred"
    >>> f"He said his name is {name!r}."
    "He said his name is Fred."
    
    >>> f"He said his name is {repr(name)}." # repr() is equivalent to !r
    "He said his name is Fred."
    
    >>> width = 10
    >>> precision = 4
    >>> value = decimal.Decimal("12.34567")
    >>> f"result: {value:{width}.{precision}}" # nested fields
    result: 12.35
    
    >>> today = datetime(year=2017, month=1, day=27)
    >>> f"{today:%B %d, %Y}" # using date format specifier
    January 27, 2017
    
    >>> number = 1024
    >>> f"{number:#0x}" # using integer format specifier
    0x400
    
    0 讨论(0)
  • 2020-12-09 05:24

    In Python 3.6, the f-string was introduced(PEP 498). In short, it is a way to format your string that is more readable and fast.

    Example:

    agent_name = 'James Bond'
    kill_count = 9
    
    # old ways
    print('{0} has killed {1} enemies '.format(agent_name,kill_count))
    
    # f-strings way
    print(f'{agent_name} has killed {kill_count} enemies')
    

    The f or F in front of strings tell Python to look at the values inside {} and substitute them with the variables values if exists. The best thing about the is that you can do cool stuff in {}, e.g. {kill_count * 100}.

    Readings:

    • PEP 498 Literal String Interpolation
    • Python String Formatting
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-09 05:31
    args = parser.parser_args()
    
    print(f"Input directory: {args.input_directory}")
    print(f"Output directory: {args.output_directory}")
    

    is the same as

    print("Input directory: {}".format(args.input_directory))
    print("Output directory: {}".format(args.output_directory))
    

    it is also the same as

    print("Input directory: "+args.input_directory)
    print("Output directory: "+args.output_directory)
    
    0 讨论(0)
提交回复
热议问题