{ "metadata": { "name": "", "signature": "sha256:7c4ed515b4622bf7783c6ea31e6344d812245c986c659354f542a0564b747ab5" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# String Indexing\n", "\n", "- **Author:** [Chris Albon](http://www.chrisalbon.com/), [@ChrisAlbon](https://twitter.com/chrisalbon)\n", "- **Date:** -\n", "- **Repo:** [Python 3 code snippets for data science](https://github.com/chrisalbon/code_py)\n", "- **Note:**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a string" ] }, { "cell_type": "code", "collapsed": false, "input": [ "string = 'Strings are defined as ordered collections of characters.'" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Print the entire string" ] }, { "cell_type": "code", "collapsed": false, "input": [ "string[:]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ "'Strings are defined as ordered collections of characters.'" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Print the first three characters" ] }, { "cell_type": "code", "collapsed": false, "input": [ "string[0:2]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "'St'" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Print the first three characters" ] }, { "cell_type": "code", "collapsed": false, "input": [ "string[:2]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": [ "'St'" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Print the last three characters" ] }, { "cell_type": "code", "collapsed": false, "input": [ "string[-3:]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 6, "text": [ "'rs.'" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Print the third to fifth character" ] }, { "cell_type": "code", "collapsed": false, "input": [ "string[2:4]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 7, "text": [ "'ri'" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Print the first to the tenth character, skipping every other character" ] }, { "cell_type": "code", "collapsed": false, "input": [ "string[0:9:2]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 8, "text": [ "'Srnsa'" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Print the string in reverse" ] }, { "cell_type": "code", "collapsed": false, "input": [ "string[::-1]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": [ "'.sretcarahc fo snoitcelloc deredro sa denifed era sgnirtS'" ] } ], "prompt_number": 9 } ], "metadata": {} } ] }