f-strings

6b6e72d297aa0270654a0d4575f1287e?s=47 Mariatta
January 22, 2018

 f-strings

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

6b6e72d297aa0270654a0d4575f1287e?s=128

Mariatta

January 22, 2018
Tweet

Transcript

  1. F-strings @mariatta

  2. F-strings?

  3. the new way to format strings

  4. starting in Python 3.6

  5. How?

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

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

    % (name, age)
  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)
  9. Alternate usage

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

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

  12. Call a function >>> def to_uppercase(input): ... return input.upper() ...

    >>> name = “bart simpson” >>> print(f"Hi {to_uppercase(name)}!") Hi BART SIMPSON!
  13. Format specifiers >>> pycascades = datetime(year=2018, month=1, day=22) >>> print(f"{pycascades:%b

    %d, %Y}") Jan 22, 2018
  14. F-strings

  15. Faster than str.format

  16. Python 3.6

  17. Free download www.python.org

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

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

    ubuntu 17.10
  20. None