{ "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. Let's explore some of them in this lecture:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "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, or change the case of the entire string." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello world'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Capitalize first word in string\n", "s.capitalize()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'HELLO WORLD'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.upper()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hello world'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.lower()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Remember, strings are immutable. None of the above methods change the string in place, they only return modified copies of the original string." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hello world'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To change a string requires reassignment:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'HELLO WORLD'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = s.upper()\n", "s" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hello world'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = s.lower()\n", "s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Location and Counting" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.count('o') # returns the number of occurrences, without overlap" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.find('o') # returns the starting index position of the first occurence" ] }, { "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": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'zzzzhello worldzzzzz'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.center(20,'z')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The expandtabs() method will expand tab notations \\t into spaces:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hello hi'" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'hello\\thi'.expandtabs()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## is check methods\n", "These various methods below check if the string is some case. Let's explore them:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "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": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.isalnum()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "isalpha() will return True if all characters in **s** are alphabetic" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 15, "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": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 16, "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": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 17, "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. It returns False otherwise." ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 18, "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": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 19, "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": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 20, "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 results.\n", "We can use partition() to return a tuple that includes the first occurrence of the separator sandwiched between the first half and the end half." ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['h', 'llo']" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.split('e')" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('he', 'l', 'lo')" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.partition('l')" ] }, { "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 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.6.2" } }, "nbformat": 4, "nbformat_minor": 1 }