How do I use formatted strings in python?



  • Available from Python 3.6+

    Using f-strings or Formatted Strings, use a variable with an f then your string with variable names in curly brackets. Formatted strings make the code easier to read, the alternative is to use multiple concatenations.

    #!/usr/bin/python
    item1 = 'apples'
    item2 = 'carrots'
    quote = f'Order more {item1} and {item2}'
    print(quote)
    
    $ ./main.py
    Order more apples and carrots
    

Log in to reply
 

© Lightnetics 2024