{ "metadata": { "name": "", "signature": "sha256:21f2f4e801e29d8ff85712bc9f03665672c8c4da3ed899baef8507dee2216b80" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "String formatting" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "We have talked about strings before, and you know that it is possible to construct strings containing values, e.g." ] }, { "cell_type": "code", "collapsed": false, "input": [ "'x=' + str(43.2) + ', y=' + str(1./3.)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "However, one may want to format the values in more detail, for example forcing values to be a certain length, or have a certain number of decimal places. This is called *string formatting*." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "The syntax for formatting strings looks like this:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"{0} {1} {2}\".format(1./3., 2./7., 2.)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "In the above example, the ``0``, ``1``, and ``2`` refer to the position of the argument in the parentheses, so one could also do:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"The quantity {0} that could also be called {0} {1}\".format(1./3., 2./7.)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "By default, the value looks the same as if one had used ``str()``, but you can also specify the format and number of decimal places:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"{0:10.3f}\".format(1./3.)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The ``f`` stands for floating-point, the 10 is the total length of the string, and the 3 is the nuber of decimal places." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "We can do something similar for integers (without the number of decimal places):" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"{0:10d}\".format(4)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "There are a number of options for string formatting - for instance, one can add leading zeros:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"{0:010d}\".format(4)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "or align to the left:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"{0:<10d}\".format(4)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Instead of using ``0``, ``1``, etc. it is also possible to pass the values by name:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"{day:02d}\".format(day=3)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Here is an example of string formatting for a date:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"{year:04d}{month:02d}{day:02d}\".format(year=2013, month=7, day=8)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Exercise 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write a function that takes year, month, day, hour, minutes, and seconds and converts them to a string with format ``2006-03-22 13:12:55``:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# your solution here" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Exercise 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write a function that takes a string like ``2006-03-22 13:12:55`` and return the year, month, day, hour, minutes, and seconds as integers:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# your solution here" ], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }