{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Strings in python\n", "\n", "Import numpy etc. first. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Strings can be created and concatenated." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This is the first part of a sentence and this is the second.\n" ] } ], "source": [ "s1 = \"This is the first part of a sentence\"\n", "s2 = \"and this is the second.\"\n", "s = s1 + \" \" + s2\n", "print(s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The method str() does it's best to convert things into strings. The method format() is useful for formatting output from programs. The brackets {} in a string are interpreted as placeholders for the quantities that appear in an appended format() statement. Quatnitites can be reformatted if the brackets contain a colon followed by a formatting command. E.g. :.5f means format as floating point number with 5 decimal places." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The value of pi is 3.141592653589793\n", "The value of pi is 3.14159\n" ] } ], "source": [ "print(\"The value of pi is \" + str(np.pi))\n", "print(\"The value of {} is {:.5f}\".format(\"pi\",np.pi))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Can also explicitly refer to the position of the elements in the format statement. Note that you have to use 0 as the first index, as always!" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This is the first time I have written this.\n" ] } ], "source": [ "print(\"This is the {2} time I have written {3}{0}\".format(\".\",\"second\",\"first\",\"this\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The ways of formatting numbers include :b for binary, :d for decimal, :o for octal and :x for hexadecimal." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Ten is 1010 in binary.\n", "Ten is a in hexadecimal.\n" ] } ], "source": [ "print(\"Ten is {:b} in binary.\".format(10))\n", "print(\"Ten is {:x} in hexadecimal.\".format(10))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another way of formatting things is using the % sign. You have to indicate the type of the object to be inserted in the string, e.g. %s is a string, %.4f is a floating point number with 4 decimal places." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The value of pi is 3.1416, but some people use 3. \n" ] } ], "source": [ "print(\"The value of %s is %.4f, \" %(\"pi\",np.pi) + \"but some people use %.d. \" %(3.0))" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [default]", "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.5.2" } }, "nbformat": 4, "nbformat_minor": 0 }