{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# f-strings\n", "\n", "[f-strings](https://www.python.org/dev/peps/pep-0498/)\n", "are\n", "[new in Python 3.6](https://docs.python.org/3.6/whatsnew/3.6.html#pep-498-formatted-string-literals).\n", "\n", "f-strings look like an ordinary strings with an f in front.\n", "They look like an ordinary (immutable) string that does not change,\n", "but they are not immutable strings.\n", "\n", "# **They are dynamic expressions!!!**\n", "\n", "One can put arbitrary expressions within curly braces in an f-string,\n", "and the arbitrary expressions will be evaluated each time,\n", "and then converted to a string.\n", "The most common expressions used are just variable names.\n", "\n", "It is a more\n", "[DRY](https://en.wikipedia.org/wiki/Don't_repeat_yourself)\n", "way of doing formatting than str.format." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "\n", "First let's look at how format is often done with the str.format method." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "stuff = {\n", " 'apple': 1.97,\n", " 'banana': 2.99,\n", " 'cherry': 3.99,\n", "}" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The price of apple is 1.97\n", "The price of banana is 2.99\n", "The price of cherry is 3.99\n" ] } ], "source": [ "# Common pattern of .format use:\n", "\n", "for name, price in stuff.items():\n", " print(\n", " 'The price of {name} is {price}'.\n", " format(name=name, price=price))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Something that sucks about the above print,\n", "is that name and price appear three times each.\n", "I.e., It is not\n", "[DRY](https://en.wikipedia.org/wiki/Don't_repeat_yourself).\n", "\n", "With f-strings, name and price only have to appear once,\n", "making the code easier to read and maintain\n", "as in the following example." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The price of apple is 1.97\n", "The price of banana is 2.99\n", "The price of cherry is 3.99\n" ] } ], "source": [ "for name, price in stuff.items():\n", " print(f'The price of {name} is {price}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It reminds me of shell syntax. For example,\n", "\n", " echo \"The price of ${name} is ${price}\"\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It gets better. One may put arbitrary expressions\n", "within the curly braces in an f-string.\n", "One is not limited to using just variable names." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The total price of apple is 2.96\n", "The total price of banana is 4.49\n", "The total price of cherry is 5.99\n" ] } ], "source": [ "tax = 0.50\n", "\n", "for name, price in stuff.items():\n", " print(f'The total price of {name} is {round(price * (1+tax), 2)}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then some crazy whacko guy named\n", "[zak](https://github.com/y2kbugger)\n", "showed a crazy whacko lambda with no arguments\n", "that had an f-string for the body.\n", "\n", "This makes my head wobble.\n", "I am wondering what good uses (if any) there are for this." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ ">" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "template = lambda: f'tes{k}'\n", "template" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'tes9'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "k = 9\n", "template()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'tes8'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "k = 8\n", "template()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2017-02-25 afterthought:\n", "\n", "Since it is saved in a variable,\n", "the lambda above is a poor use of a lambda.\n", "It would be better to just make it a regular (named) function\n", "as shown below.\n", "\n", "I still do not know what good uses (if any) there are for the f-string below." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def template():\n", " return f'tes{k}'" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'tes9'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "k = 9\n", "template()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'tes8'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "k = 8\n", "template()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.0" } }, "nbformat": 4, "nbformat_minor": 0 }