Upgrade to Pro — share decks privately, control downloads, hide ads and more …

f-strings

 f-strings

Lightning talk about f-strings. Presented at PyCascades 2018 in Vancouver.

Mariatta
PRO

January 22, 2018
Tweet

More Decks by Mariatta

Other Decks in Programming

Transcript

  1. F-strings @mariatta

    View Slide

  2. F-strings?

    View Slide

  3. the new way to format strings

    View Slide

  4. starting in Python 3.6

    View Slide

  5. How?

    View Slide

  6. How?
    "Hello, %s. You’re %s." % (name, age)

    View Slide

  7. How?
    "Hello, {name}. You’re {age}.".format(name=name, age=age)
    "Hello, %s. You’re %s." % (name, age)

    View Slide

  8. How?
    "Hello, {name}. You’re {age}.".format(name=name, age=age)
    f"Hello, {name}. You’re {age}."
    "Hello, %s. You’re %s." % (name, age)

    View Slide

  9. Alternate usage

    View Slide

  10. Alternate usage
    f"Hello, {name}. You’re {age}."

    View Slide

  11. Alternate usage
    F"Hello, {name}. You’re {age}."
    f"Hello, {name}. You’re {age}."

    View Slide

  12. Call a function
    >>> def to_uppercase(input):
    ... return input.upper()
    ...
    >>> name = “bart simpson”
    >>> print(f"Hi {to_uppercase(name)}!")
    Hi BART SIMPSON!

    View Slide

  13. Format specifiers
    >>> pycascades = datetime(year=2018, month=1, day=22)
    >>> print(f"{pycascades:%b %d, %Y}")
    Jan 22, 2018

    View Slide

  14. F-strings

    View Slide

  15. Faster than str.format

    View Slide

  16. Python 3.6

    View Slide

  17. Free download
    www.python.org

    View Slide

  18. Build from source
    https://docs.python.org/3.6/using/unix.html

    View Slide

  19. Change your OS
    consider: MacOS X, Windows 10, fedora 26, ubuntu 17.10

    View Slide

  20. View Slide