{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Slicing Strings\n", "Manipulating strings is a powerful skill to learn. So far, we have focused on the **format** method. This involved formatting numbers and substituting values into a string. In a way, the *format* method is used for *combining* strings. String **slicing** is a useful way to *take apart* strings. The syntax for slicing a string is __\"myString\"[*start*:*end*]__ where *start* is the index of the string you want to start slicing from and *end* is the index you want to stop at. The starting index is *inclusive* and the ending index is *exclusive*. Also, don't forget indexes start counting at zero! You can specify either or both indexes in the **slice** operation." ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Happy Birthday\n" ] } ], "source": [ "test_string = 'Happy Birthday'\n", "print(test_string)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Happy\n" ] } ], "source": [ "print(test_string[0:5])" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Happ\n" ] } ], "source": [ "print(test_string[0:4])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The above examples show the basics string slice. Note how the ending index of the slice is *exclusive*. A very important note to make is that this **slice** operation is not destructive. It does NOT modify the string you are slicing in any way. It actually returns a completely new string as the result of the operation:" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Happy\n", "Happy Birthday\n" ] } ], "source": [ "print(test_string[0:5])\n", "print(test_string)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The above examples use both the starting and ending index in the slice. However, you don't have to specify one or the other, or both! If neither index is given, the whole string is copied as a result:" ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Happy Birthday\n" ] } ], "source": [ "print(test_string[:])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also specify one or the other. If no _starting_ index is given, it will default to index **0**." ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Happ\n" ] } ], "source": [ "print(test_string[:4])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If no _ending_ index is given, it will default to the number of characters in the string." ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "appy Birthday\n" ] } ], "source": [ "print(test_string[1:])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Remember that the ending index is exclusive!" ] }, { "cell_type": "code", "execution_count": 57, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Happy Birthda\n" ] } ], "source": [ "print(test_string[:13])" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Happy Birthday\n" ] } ], "source": [ "print(test_string[:14])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The **slice** operation is very powerful. We aren't limited to using only positive numbers for the _start_ and _end_ index. This is completely against many rules when accessing an index in arrays or lists in many programming languages. If a *negative* number is used for the _end_ index, it is essentially saying \"exclude the last **n** characters from the end.\"" ] }, { "cell_type": "code", "execution_count": 59, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Happy Birthda\n" ] } ], "source": [ "print(test_string[:-1])" ] }, { "cell_type": "code", "execution_count": 60, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Happy Birth\n" ] } ], "source": [ "print(test_string[:-3])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also use a negative number for the _start_ index. In this case, it gives the characters starting **n** characters from the end of the string." ] }, { "cell_type": "code", "execution_count": 61, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "y\n" ] } ], "source": [ "print(test_string[-1:])" ] }, { "cell_type": "code", "execution_count": 62, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "day\n" ] } ], "source": [ "print(test_string[-3:])" ] }, { "cell_type": "code", "execution_count": 63, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "da\n" ] } ], "source": [ "print(test_string[-3:-1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using negative indices in the **slice** operation is an easy way to manipulate a string without knowing how many characters it contains. Though, you can find the length of a string by using the [built-in **len** function](https://docs.python.org/3/library/functions.html#len). **len** will return the length of an object, this includes strings, dictionaries, and lists among other things." ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Happy Birthday\n" ] } ], "source": [ "len(test_string)\n", "print(test_string[:len(test_string)])" ] }, { "cell_type": "code", "execution_count": 65, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Happy B\n" ] } ], "source": [ "print(test_string[:len(test_string)//2])" ] }, { "cell_type": "code", "execution_count": 66, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "irthday\n" ] } ], "source": [ "print(test_string[len(test_string)//2:])" ] } ], "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.5.1" } }, "nbformat": 4, "nbformat_minor": 2 }