{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "In Python, a string is a *sequence*, which means Python keeps track of every element in the string as a sequence." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hello'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# single word\n", "'hello'" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hello'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# double quote is also valid\n", "\"hello\"" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'This is also a string'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'This is also a string'" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "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-quote, but this will cause 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 single-quotes\n", "'I'm using single-quote, but this will cause an error'" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"I'm using single-quote, but this will cause an error\"" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"I'm using single-quote, but this will cause an error\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Print a string" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This is the correct way to print string\n" ] } ], "source": [ "print(\"This is the correct way to print string\")" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This \n", " is the correct way\n" ] } ], "source": [ "print(\"This \\n is the correct way\")" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This \t is the correct way\n" ] } ], "source": [ "print(\"This \\t is the correct way\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# String basics" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(\"hello world\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# String Indexing" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "# in Python we use [] after the object to call its index. Indexing starts at 0." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "s = \"Hello world\"" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello world\n" ] } ], "source": [ "print(s)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "H\n" ] } ], "source": [ "print(s[0])" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "l\n" ] } ], "source": [ "print(s[3])" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ello world'" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# grab everything after the first character\n", "s[1:]" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hel'" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# grab everything UPTO the 3rd index\n", "s[:3]" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello world'" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# grab everything\n", "s[:]" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello worl'" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# go backwards\n", "s[:-1]" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'d'" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[-1]" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'d'" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[-1:]" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello world'" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# grab everything, but go in steps size of 1\n", "s[::1]" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hlowrd'" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# step size = 2\n", "s[::2]" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'dlrow olleH'" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# use this to print a string backwards (common interview question)\n", "s[::-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# String properties" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello world'" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "# string are IMMUTABLE. Once a string created, the elements cannot be changed, or replaced." ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "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[0;32m----> 1\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[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment" ] } ], "source": [ "s[0] = 'x'" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello worldconcatenate me'" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# BUT WE CAN concatenate\n", "s + 'concatenate me'" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "letter = 'z'" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'zzzzzzzzzz'" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letter * 10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Built-in String methods" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'HELLO WORLD'" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# we call methods with a dot, and then the method name. Methods are in the form object.method(parameters)\n", "s.upper()" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hello world'" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.lower()" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Hello', 'world']" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# split a string by blank space (default)\n", "s.split()" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['I', 'am', 'Ajasra', 'Damani', '!']" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"I am Ajasra Damani !\"\n", "s.split()" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['I am Ajasra Damani ', '']" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# we can split by specific element (doesnt include the element that was split on)\n", "s.split('!')" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['I', ' am', '']" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"I! am!\"\n", "s.split(\"!\")" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m username my anme\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "username my anme" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['username', 'my', 'anme']" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"username my anme\".split()" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "['I! am!']" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.split('@')" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"\".split()" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['', 'j', 'sr', '']" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"ajasra\".split('a')" ] } ], "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.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }