{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "#Advanced Strings\n", "String objects have a variety of methods we can use to save time and add functionality. Lets explore some of them in this lecture:" ] }, { "cell_type": "code", "execution_count": 75, "metadata": { "collapsed": false }, "outputs": [], "source": [ "s = 'hello world'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##Changing case\n", "We can use methods to capitalize the first word of a string, change cases to upper and lower case strings." ] }, { "cell_type": "code", "execution_count": 76, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'Hello world'" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Capitalize first word in string\n", "s.capitalize()" ] }, { "cell_type": "code", "execution_count": 77, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'HELLO WORLD'" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.upper()" ] }, { "cell_type": "code", "execution_count": 78, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'hello world'" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.lower()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Location and Counting" ] }, { "cell_type": "code", "execution_count": 80, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.count('o')" ] }, { "cell_type": "code", "execution_count": 81, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.find('o')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##Formatting\n", "The center() method allows you to place your string 'centered' between a provided string with a certain length. Personally, I've never actually used this in code as it seems pretty esoteric..." ] }, { "cell_type": "code", "execution_count": 83, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'zzzzhello worldzzzzz'" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.center(20,'z')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "expandtabs() will expand tab notations \\t into spaces:" ] }, { "cell_type": "code", "execution_count": 84, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'hello hi'" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'hello\\thi'.expandtabs()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## is check methods\n", "These various methods below check it the string is some case. Lets explore them:" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": true }, "outputs": [], "source": [ "s = 'hello'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "isalnum() will return True if all characters in S are alphanumeric" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.isalnum()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "isalpha() wil return True if all characters in S are alphabetic" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.isalpha()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "islower() will return True if all cased characters in S are lowercase and there is\n", "at least one cased character in S, False otherwise." ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.islower()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "isspace() will return True if all characters in S are whitespace." ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.isspace()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "istitle() will return True if S is a title cased string and there is at least one character in S, i.e. uppercase characters may only follow uncased characters and lowercase characters only cased ones. Return False otherwise." ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.istitle()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "isupper() will return True if all cased characters in S are uppercase and there is\n", "at least one cased character in S, False otherwise." ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.isupper()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another method is endswith() which is essentially the same as a boolean check on s[-1]" ] }, { "cell_type": "code", "execution_count": 69, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.endswith('o')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Built-in Reg. Expressions\n", "Strings have some built-in methods that can resemble regular expression operations.\n", "We can use split() to split the string at a certain element and return a list of the result.\n", "We can use partition to return a tuple that includes the separator (the first occurrence) and the first half and the end half." ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['h', 'llo']" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.split('e')" ] }, { "cell_type": "code", "execution_count": 72, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "('h', 'e', 'llo')" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.partition('e')" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'hello'" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Great! You should now feel comfortable using the variety of methods that are built-in string objects!**" ] } ], "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 }