{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "All the IPython Notebooks in this lecture series are available at https://github.com/rajathkumarmp/Python-Lectures" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##Strings" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Strings are ordered text based data which are represented by enclosing the same in single/double/triple quotes." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "String0 = 'Taj Mahal is beautiful'\n", "String1 = \"Taj Mahal is beautiful\"\n", "String2 = '''Taj Mahal\n", "is\n", "beautiful'''" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Taj Mahal is beautiful \n", "Taj Mahal is beautiful \n", "Taj Mahal\n", "is\n", "beautiful \n" ] } ], "source": [ "print String0 , type(String0)\n", "print String1, type(String1)\n", "print String2, type(String2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "String Indexing and Slicing are similar to Lists which was explained in detail earlier." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "M\n", "Mahal is beautiful\n" ] } ], "source": [ "print String0[4]\n", "print String0[4:]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###Built-in Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**find( )** function returns the index value of the given data that is to found in the string. If it is not found it returns **-1**. Remember to not confuse the returned -1 for reverse indexing value." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7\n", "-1\n" ] } ], "source": [ "print String0.find('al')\n", "print String0.find('am')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The index value returned is the index of the first element in the input data." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a\n" ] } ], "source": [ "print String0[7]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One can also input **find( )** function between which index values it has to search." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "2\n" ] } ], "source": [ "print String0.find('j',1)\n", "print String0.find('j',1,3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**capitalize( )** is used to capitalize the first element in the string." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Observe the first letter in this sentence.\n" ] } ], "source": [ "String3 = 'observe the first letter in this sentence.'\n", "print String3.capitalize()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**center( )** is used to center align the string by specifying the field width." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "' Taj Mahal is beautiful '" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "String0.center(70)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One can also fill the left out spaces with any other character." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'------------------------Taj Mahal is beautiful------------------------'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "String0.center(70,'-')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**zfill( )** is used for zero padding by specifying the field width." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'00000000Taj Mahal is beautiful'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "String0.zfill(30)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**expandtabs( )** allows you to change the spacing of the tab character. '\\t' which is by default set to 8 spaces." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "h\te\tl\tl\to\n", "h e l l o\n", "h e l l o\n" ] } ], "source": [ "s = 'h\\te\\tl\\tl\\to'\n", "print s\n", "print s.expandtabs(1)\n", "print s.expandtabs()" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "**index( )** works the same way as **find( )** function the only difference is find returns '-1' when the input element is not found in the string but **index( )** function throws a ValueError" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "4\n" ] }, { "ename": "ValueError", "evalue": "substring not found", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\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;32mprint\u001b[0m \u001b[0mString0\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Taj'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0mString0\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Mahal'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0mString0\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Mahal'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m20\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mValueError\u001b[0m: substring not found" ] } ], "source": [ "print String0.index('Taj')\n", "print String0.index('Mahal',0)\n", "print String0.index('Mahal',10,20)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "**endswith( )** function is used to check if the given string ends with the particular char which is given as input." ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "print String0.endswith('y')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The start and stop index values can also be specified." ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "True\n" ] } ], "source": [ "print String0.endswith('l',0)\n", "print String0.endswith('M',0,5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**count( )** function counts the number of char in the given string. The start and the stop index can also be specified or left blank. (These are Implicit arguments which will be dealt in functions)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "2\n" ] } ], "source": [ "print String0.count('a',0)\n", "print String0.count('a',5,10)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "**join( )** function is used add a char in between the elements of the input string." ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'*a_a-'" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'a'.join('*_-')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "'*_-' is the input string and char 'a' is added in between each element" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**join( )** function can also be used to convert a list into a string." ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['T', 'a', 'j', ' ', 'M', 'a', 'h', 'a', 'l', ' ', 'i', 's', ' ', 'b', 'e', 'a', 'u', 't', 'i', 'f', 'u', 'l']\n", "Taj Mahal is beautiful\n" ] } ], "source": [ "a = list(String0)\n", "print a\n", "b = ''.join(a)\n", "print b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Before converting it into a string **join( )** function can be used to insert any char in between the list elements." ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " /i/s/ /b/e/a/u/t/i/f/u/l\n" ] } ], "source": [ "c = '/'.join(a)[18:]\n", "print c" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "**split( )** function is used to convert a string back to a list. Think of it as the opposite of the **join()** function." ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[' ', 'i', 's', ' ', 'b', 'e', 'a', 'u', 't', 'i', 'f', 'u', 'l']\n" ] } ], "source": [ "d = c.split('/')\n", "print d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In **split( )** function one can also specify the number of times you want to split the string or the number of elements the new returned list should conatin. The number of elements is always one more than the specified number this is because it is split the number of times specified." ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[' ', 'i', 's', ' /b/e/a/u/t/i/f/u/l']\n", "4\n" ] } ], "source": [ "e = c.split('/',3)\n", "print e\n", "print len(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**lower( )** converts any capital letter to small letter." ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Taj Mahal is beautiful\n", "taj mahal is beautiful\n" ] } ], "source": [ "print String0\n", "print String0.lower()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**upper( )** converts any small letter to capital letter." ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'TAJ MAHAL IS BEAUTIFUL'" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "String0.upper()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**replace( )** function replaces the element with another element." ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'Bengaluru is beautiful'" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "String0.replace('Taj Mahal','Bengaluru')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**strip( )** function is used to delete elements from the right end and the left end which is not required." ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": true }, "outputs": [], "source": [ "f = ' hello '" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If no char is specified then it will delete all the spaces that is present in the right and left hand side of the data." ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'hello'" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.strip()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**strip( )** function, when a char is specified then it deletes that char if it is present in the two ends of the specified string." ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": true }, "outputs": [], "source": [ "f = ' ***----hello---******* '" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "' ***----hello---******* '" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.strip('*')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The asterisk had to be deleted but is not. This is because there is a space in both the right and left hand side. So in strip function. The characters need to be inputted in the specific order in which they are present." ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "----hello---\n", "hello\n" ] } ], "source": [ "print f.strip(' *')\n", "print f.strip(' *-')" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "**lstrip( )** and **rstrip( )** function have the same functionality as strip function but the only difference is **lstrip( )** deletes only towards the left side and **rstrip( )** towards the right." ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "----hello---******* \n", " ***----hello---\n" ] } ], "source": [ "print f.lstrip(' *')\n", "print f.rstrip(' *')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##Dictionaries" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Dictionaries are more used like a database because here you can index a particular sequence with your user defined string." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To define a dictionary, equate a variable to { } or dict()" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " \n" ] } ], "source": [ "d0 = {}\n", "d1 = dict()\n", "print type(d0), type(d1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Dictionary works somewhat like a list but with an added capability of assigning it's own index style." ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'OneTwo': 12, 'One': 1}\n" ] } ], "source": [ "d0['One'] = 1\n", "d0['OneTwo'] = 12 \n", "print d0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That is how a dictionary looks like. Now you are able to access '1' by the index value set at 'One'" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "print d0['One']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Two lists which are related can be merged to form a dictionary." ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false }, "outputs": [], "source": [ "names = ['One', 'Two', 'Three', 'Four', 'Five']\n", "numbers = [1, 2, 3, 4, 5]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**zip( )** function is used to combine two lists" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[('One', 1), ('Two', 2), ('Three', 3), ('Four', 4), ('Five', 5)]\n" ] } ], "source": [ "d2 = zip(names,numbers)\n", "print d2" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "The two lists are combined to form a single list and each elements are clubbed with their respective elements from the other list inside a tuple. Tuples because that is what is assigned and the value should not change.\n", "\n", "Further, To convert the above into a dictionary. **dict( )** function is used." ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'Four': 4, 'Five': 5, 'Three': 3, 'Two': 2, 'One': 1}\n" ] } ], "source": [ "a1 = dict(d2)\n", "print a1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###Built-in Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**clear( )** function is used to erase the entire database that was created." ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{}\n" ] } ], "source": [ "a1.clear()\n", "print a1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Dictionary can also be built using loops." ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'Four': 4, 'Five': 5, 'Three': 3, 'Two': 2, 'One': 1}\n" ] } ], "source": [ "for i in range(len(names)):\n", " a1[names[i]] = numbers[i]\n", "print a1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**values( )** function returns a list with all the assigned values in the dictionary." ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[4, 5, 3, 2, 1]" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a1.values()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**keys( )** function returns all the index or the keys to which contains the values that it was assigned to." ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['Four', 'Five', 'Three', 'Two', 'One']" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a1.keys()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**items( )** is returns a list containing both the list but each element in the dictionary is inside a tuple. This is same as the result that was obtained when zip function was used." ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[('Four', 4), ('Five', 5), ('Three', 3), ('Two', 2), ('One', 1)]" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a1.items()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**pop( )** function is used to get the remove that particular element and this removed element can be assigned to a new variable. But remember only the value is stored and not the key. Because the is just a index value." ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'Five': 5, 'Three': 3, 'Two': 2, 'One': 1}\n", "4\n" ] } ], "source": [ "a2 = a1.pop('Four')\n", "print a1\n", "print a2" ] } ], "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.10" } }, "nbformat": 4, "nbformat_minor": 0 }