{ "metadata": { "name": "advanced_strings" }, "nbformat": 2, "worksheets": [ { "cells": [ { "cell_type": "markdown", "source": [ "#### Strings can do operations on themselves ####", "`.lower()`, `.upper()`,`.capitalize()`" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"funKY tOwn\".capitalize()" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 1, "text": [ "'Funky town'" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "\"funky tOwn\".lower()" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 2, "text": [ "'funky town'" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "source": [ "`.split([sep [,maxsplit]])`" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"funKY tOwn\".split()" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 3, "text": [ "['funKY', 'tOwn']" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "\"funKY tOwn\".capitalize().split()" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 4, "text": [ "['Funky', 'town']" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "[x.capitalize() for x in \"funKY tOwn\".split()]" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 5, "text": [ "['Funky', 'Town']" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "\"I want to take you to, funKY tOwn\".split(\"u\")" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 6, "text": [ "['I want to take yo', ' to, f', 'nKY tOwn']" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "\"I want to take you to, funKY tOwn\".split(\"you\")" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 7, "text": [ "['I want to take ', ' to, funKY tOwn']" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "source": [ "`.strip()`, `.join()`, `.replace()`" ] }, { "cell_type": "code", "collapsed": false, "input": [ "csv_string = 'Dog,Cat,Spam,Defenestrate,1, 3.1415 \\n\\t'", "csv_string.strip()" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 8, "text": [ "'Dog,Cat,Spam,Defenestrate,1, 3.1415'" ] } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "clean_list = [x.strip() for x in csv_string.split(\",\")]", "print clean_list" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "['Dog', 'Cat', 'Spam', 'Defenestrate', '1', '3.1415']" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "source": [ "`.join()` allows you to glue a list of strings together with a certain string" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \",\".join(clean_list)" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Dog,Cat,Spam,Defenestrate,1,3.1415" ] } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "print \"\\t\".join(clean_list)" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Dog\tCat\tSpam\tDefenestrate\t1\t3.1415" ] } ], "prompt_number": 11 }, { "cell_type": "markdown", "source": [ "`.replace()` strings in strings" ] }, { "cell_type": "code", "collapsed": false, "input": [ "csv_string = 'Dog,Cat,Spam,Defenestrate,1, 3.1415 \\n\\t'", "alt_csv = csv_string.strip().replace(' ','')", "print alt_csv" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Dog,Cat,Spam,Defenestrate,1,3.1415" ] } ], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [ "print csv_string.strip().replace(' ','').replace(',','\\t')" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Dog\tCat\tSpam\tDefenestrate\t1\t3.1415" ] } ], "prompt_number": 13 }, { "cell_type": "markdown", "source": [ "### `.find()` ###", "incredibly useful searching,", "returning the index of the search" ] }, { "cell_type": "code", "collapsed": false, "input": [ "s = 'My Funny Valentine'", "s.find(\"y\")" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 14, "text": [ "1" ] } ], "prompt_number": 14 }, { "cell_type": "code", "collapsed": false, "input": [ "s.find(\"y\",2)" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 15, "text": [ "7" ] } ], "prompt_number": 15 }, { "cell_type": "code", "collapsed": false, "input": [ "s[s.find(\"Funny\"):]" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 16, "text": [ "'Funny Valentine'" ] } ], "prompt_number": 16 }, { "cell_type": "code", "collapsed": false, "input": [ "s.find(\"z\")" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 17, "text": [ "-1" ] } ], "prompt_number": 17 }, { "cell_type": "code", "collapsed": false, "input": [ "ss = [s,\"Argentine\",\"American\",\"Quarentine\"]", "for thestring in ss:", " if thestring.find(\"tine\") != -1:", " print \"'\" + str(thestring) + \"' contains 'tine'.\"" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "'My Funny Valentine' contains 'tine'.", "'Argentine' contains 'tine'.", "'Quarentine' contains 'tine'." ] } ], "prompt_number": 18 }, { "cell_type": "markdown", "source": [ "#### `string` module ####", "exposes useful variables and functions " ] }, { "cell_type": "code", "collapsed": false, "input": [ "import string", "string.swapcase(\"fUNKY tOWN\")" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 19, "text": [ "'Funky Town'" ] } ], "prompt_number": 19 }, { "cell_type": "code", "collapsed": false, "input": [ "print string.ascii_letters", "print string.digits" ], "language": "python", "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", "0123456789" ] } ], "prompt_number": 21 }, { "cell_type": "markdown", "source": [ "[back]" ] }, { "cell_type": "markdown", "source": [ "### String Formatting ###", "", "the (new) preferred way", "", "is `string.format(value0,value1,....)`" ] }, { "cell_type": "code", "collapsed": false, "input": [ "'on {0}, I feel {1}'.format(\"saturday\",\"groovy\")" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 22, "text": [ "'on saturday, I feel groovy'" ] } ], "prompt_number": 22 }, { "cell_type": "code", "collapsed": false, "input": [ "'on {}, I feel {}'.format(\"saturday\",\"groovy\")" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 23, "text": [ "'on saturday, I feel groovy'" ] } ], "prompt_number": 23 }, { "cell_type": "code", "collapsed": false, "input": [ "'on {0}, I feel {1}'.format([\"saturday\",\"groovy\"])" ], "language": "python", "outputs": [ { "ename": "IndexError", "evalue": "tuple index out of range", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m/Users/jbloom/Classes/python-bootcamp/DataFiles_and_Notebooks/06_AdvancedStrings/\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;34m'on {0}, I feel {1}'\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"saturday\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"groovy\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mIndexError\u001b[0m: tuple index out of range" ] } ], "prompt_number": 24 }, { "cell_type": "code", "collapsed": false, "input": [ "'on {0}, I feel {0}'.format([\"saturday\",\"groovy\"])" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 25, "text": [ "\"on ['saturday', 'groovy'], I feel ['saturday', 'groovy']\"" ] } ], "prompt_number": 25 }, { "cell_type": "code", "collapsed": false, "input": [ "'on {0}, I feel {0}'.format(\"saturday\",\"groovy\")" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 26, "text": [ "'on saturday, I feel saturday'" ] } ], "prompt_number": 26 }, { "cell_type": "markdown", "source": [ "you can assign by argument position or by name" ] }, { "cell_type": "code", "collapsed": false, "input": [ "'{desire} to {place}'.format(desire='Fly me',\\", " place='The Moon')" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 28, "text": [ "'Fly me to The Moon'" ] } ], "prompt_number": 28 }, { "cell_type": "code", "collapsed": false, "input": [ "'{desire} to {place} or else I wont visit {place}.'.format( \\", " desire='Fly me',place='The Moon')" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 29, "text": [ "'Fly me to The Moon or else I wont visit The Moon.'" ] } ], "prompt_number": 29 }, { "cell_type": "code", "collapsed": true, "input": [ "f = {\"desire\": \"I want to take you\", \"place\": \"funky town\"}" ], "language": "python", "outputs": [], "prompt_number": 30 }, { "cell_type": "code", "collapsed": false, "input": [ "'{desire} to {place}'.format(**f)" ], "language": "python", "outputs": [ { "output_type": "pyout", "prompt_number": 31, "text": [ "'I want to take you to funky town'" ] } ], "prompt_number": 31 }, { "cell_type": "code", "collapsed": true, "input": [], "language": "python", "outputs": [] } ] } ] }