{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#Strings" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Strings are used in Python to record text information, such as name. Strings in Python are actually a *sequence*, which basically means Python keeps track of every element in the string as a sequence. For example, Python understands the string \"hello' to be a sequence of letters in a specific order. This means we will be able to use indexing to grab particular letters (like the first letter, or the last letter).\n", "\n", "This idea of a sequence is an important one in Python and we will touch upon it later on in the future.\n", "\n", "In this lecture we'll learn about the following:\n", "\n", " 1.) Creating Strings\n", " 2.) Printing Strings\n", " 3.) Differences in Printing in Python 2 vs 3\n", " 4.) String Indexing and Slicing\n", " 5.) String Properties\n", " 6.) String Methods\n", " 7.) Print Formatting" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating a String\n", "To create a string in Python you need to use either single quotes or double quotes. For example:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'hello'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Single word\n", "'hello'" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'This is also a string'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Entire phrase \n", "'This is also a string'" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'String built with double quotes'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# We can also use double quote\n", "\"String built with double quotes\"" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 2)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m ' I'm using single quotes, but will create an error'\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "# Be careful with quotes!\n", "' I'm using single quotes, but will create an error'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The reason for the error above is because the single quote in I'm stopped the string. You can use combinations of double and single quotes to get the complete statement." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "\"Now I'm ready to use the single quotes inside a string!\"" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Now I'm ready to use the single quotes inside a string!\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's learn about printing strings!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Printing a String\n", "\n", "Using Jupyter notebook with just a string in a cell will automatically output strings, but the correct way to display strings in your output is by using a print function." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'Hello World'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# We can simply declare a string\n", "'Hello World'" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'Hello World 2'" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# note that we can't output multiple strings this way\n", "'Hello World 1'\n", "'Hello World 2'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can use a print statement to print a string." ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World 1\n", "Hello World 2\n", "Use \n", " to print a new line\n", "\n", "\n", "See what I mean?\n" ] } ], "source": [ "print 'Hello World 1'\n", "print 'Hello World 2'\n", "print 'Use \\n to print a new line'\n", "print '\\n'\n", "print 'See what I mean?'" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "### Python 3 Alert!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Something to note. In Python 3, print is a function, not a statement. So you would print statements like this:\n", "print('Hello World')\n", "\n", "If you want to use this functionality in Python2, you can import form the __future__ module. \n", "\n", "**A word of caution, after importing this you won't be able to choose the print statement method anymore. So pick whichever one you prefer depending on your Python installation and continue on with it.**" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World\n" ] } ], "source": [ "# To use print function from Python 3 in Python 2\n", "from __future__ import print_function\n", "\n", "print('Hello World')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## String Basics" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also use a function called len() to check the length of a string!" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len('Hello World')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## String Indexing\n", "We know strings are a sequence, which means Python can use indexes to call parts of the sequence. Let's learn how this works.\n", "\n", "In Python, we use brackets [] after an object to call its index. We should also note that indexing starts at 0 for Python. Let's create a new object called s and the walk through a few examples of indexing." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Assign s as a string\n", "s = 'Hello World'" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'Hello World'" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Check\n", "s" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World\n" ] } ], "source": [ "# Print the object\n", "print(s) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's start indexing!" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'H'" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Show first element (in this case a letter)\n", "s[0]" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'e'" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[1]" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'l'" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can use a : to perform *slicing* which grabs everything up to a designated point. For example:" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'ello World'" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Grab everything past the first term all the way to the length of s which is len(s)\n", "s[1:]" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'Hello World'" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Note that there is no change to the original s\n", "s" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'Hel'" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Grab everything UP TO the 3rd index\n", "s[:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note the above slicing. Here we're telling Python to grab everything from 0 up to 3. It doesn't include the 3rd index. You'll notice this a lot in Python, where statements and are usually in the context of \"up to, but not including\"." ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'Hello World'" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Everything\n", "s[:]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also use negative indexing to go backwards." ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'d'" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Last letter (one index behind 0 so it loops back around)\n", "s[-1]" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'Hello Worl'" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Grab everything but the last letter\n", "s[:-1]" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "We can also use index and slice notation to grab elements of a sequence by a specified step size (the default is 1). For instance we can use two colons in a row and then a number specifying the frequency to grab elements. For example:" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'Hello World'" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Grab everything, but go in steps size of 1\n", "s[::1]" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'HloWrd'" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Grab everything, but go in step sizes of 2\n", "s[::2]" ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'dlroW olleH'" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# We can use this to print a string backwards\n", "s[::-1]" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## String Properties\n", "Its important to note that strings have an important property known as immutability. This means that once a string is created, the elements within it can not be changed or replaced. For example:" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'Hello World'" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "collapsed": false }, "outputs": [ { "ename": "TypeError", "evalue": "'str' object does not support item assignment", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Let's try to change the first letter to 'x'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0ms\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'x'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment" ] } ], "source": [ "# Let's try to change the first letter to 'x'\n", "s[0] = 'x'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice how the error tells us directly what we can't do, change the item assignment!\n", "\n", "Something we can do is concatenate strings!" ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'Hello World'" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'Hello World concatenate me!'" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Concatenate strings!\n", "s + ' concatenate me!'" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# We can reassign s completely though!\n", "s = s + ' concatenate me!'" ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World concatenate me!\n" ] } ], "source": [ "print(s)" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'Hello World concatenate me!'" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can use the multiplication symbol to create repetition!" ] }, { "cell_type": "code", "execution_count": 59, "metadata": { "collapsed": true }, "outputs": [], "source": [ "letter = 'z'" ] }, { "cell_type": "code", "execution_count": 60, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'zzzzzzzzzz'" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letter*10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic Built-in String methods\n", "\n", "Objects in Python usually have built-in methods. These methods are functions inside the object (we will learn about these in much more depth later) that can perform actions or commands on the object itself.\n", "\n", "We call methods with a period and then the method name. Methods are in the form:\n", "\n", "object.method(parameters)\n", "\n", "Where parameters are extra arguments we can pass into the method. Don't worry if the details don't make 100% sense right now. Later on we will be creating our own objects and functions!\n", "\n", "Here are some examples of built-in methods in strings:" ] }, { "cell_type": "code", "execution_count": 61, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'Hello World concatenate me!'" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 62, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'HELLO WORLD CONCATENATE ME!'" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Upper Case a string\n", "s.upper()" ] }, { "cell_type": "code", "execution_count": 65, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'hello world concatenate me!'" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Lower case\n", "s.lower()" ] }, { "cell_type": "code", "execution_count": 67, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['Hello', 'World', 'concatenate', 'me!']" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Split a string by blank space (this is the default)\n", "s.split()" ] }, { "cell_type": "code", "execution_count": 68, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['Hello ', 'orld concatenate me!']" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Split by a specific element (doesn't include the element that was split on)\n", "s.split('W')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are many more methods than the ones covered here. Visit the advanced String section to find out more!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Print Formatting\n", "\n", "We can use the .format() method to add formatted objects to printed string statements. \n", "\n", "The easiest way to show this is through an example:" ] }, { "cell_type": "code", "execution_count": 79, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'Insert another string with curly brackets: The inserted string'" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Insert another string with curly brackets: {}'.format('The inserted string')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will revisit this string formatting topic in later sections when we are building our projects!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Next up: Lists!" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 0 }