{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python Strings" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Python String is a collections of Unicode characters." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Python strings can be enclosed in single, double or triple quotes" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python\n", "Python\n", "Python\n" ] } ], "source": [ "str1 = 'Python'\n", "str2 = \"Python\"\n", "str3 = \"\"\"Python\"\"\"\n", "print(str1)\n", "print(str2)\n", "print(str3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### If there are characters like ' \" or \\ within a string, they can be ratained in two ways:\n", "1. Escape them by preceding them with a \\\n", "2. prepend the string with a 'r' indicating that it is a raw string" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "My name is 'Ganesh'\n" ] } ], "source": [ "msg = 'My name is \\'Ganesh\\''\n", "print(msg)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "My name is 'Ganesh'\n" ] } ], "source": [ "# O\n", "msg = r\"My name is 'Ganesh'\"\n", "print(msg)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Accessing String elements" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "String element can be accessed using the index value, starting with 0. Negative index is also allowed" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In case of negative indexing, The Last character is considered to be at index -1.\n", "\n", "In case of positive indexing, the first character is considered to be at index 0\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "H\n" ] } ], "source": [ "msg = 'Hello Ganesh'\n", "print(msg[0])" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "h\n" ] } ], "source": [ "print(msg[-1])" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "G\n" ] } ], "source": [ "print(msg[6])" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "n\n" ] } ], "source": [ "print(msg[-4])" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello\n" ] } ], "source": [ "# A Sub-string can be sliced out of a string\n", "\n", "s1 = msg[0:5] # extract from the 0 to 4\n", "print(s1)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Ganesh\n" ] } ], "source": [ "s1 = msg[6:] # extract from 6 to end\n", "print(s1)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello\n" ] } ], "source": [ "s1 = msg[:5] # extract from start to 4\n", "print(s1)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Ganesh\n" ] } ], "source": [ "s1 = msg[-6:] # extract from -6 to end\n", "print(s1)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello\n" ] } ], "source": [ "s1 = msg[:-7] # extract from start to -8\n", "print(s1)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Ganesh\n" ] } ], "source": [ "# Using too large an index reports an error, but using too large inxed while slicing is handled elegently\n", "mag = \"Hello Python\"\n", "print(msg[6:200]) # prints it successfully" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "ename": "IndexError", "evalue": "string index out of range", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\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[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmsg\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m100\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# produces an error\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mIndexError\u001b[0m: string index out of range" ] } ], "source": [ "print(msg[100]) # produces an error" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### String Properties" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "All strings are objects of built-in type str" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "msg = 'Python'\n", "print(type(msg))" ] }, { "cell_type": "code", "execution_count": 32, "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[1;32m 1\u001b[0m \u001b[0;31m# Python Strings are immutable,i.e. they cannot be changed\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0ms1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"Hello\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0ms1\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'K'\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": [ "# Python Strings are immutable,i.e. they cannot be changed\n", "s1 = \"Hello\"\n", "s1[0] = 'K'" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Bye\n" ] } ], "source": [ "# But we can do\n", "s1 = 'Bye'\n", "print(s1)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "GoodMorning\n" ] } ], "source": [ "# Strings can be concatenated using +\n", "s1 = \"Good\"\n", "s2 = \"Morning\"\n", "s3 = s1 + s2\n", "print(s3)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "********************\n" ] } ], "source": [ "# Strings can be repeated during the printing\n", "print(\"*\"*20) # Print 20 star" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "# whether one string is part of another can be found out using in operator\n", "\n", "print('e' in \"Hello\")" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "print('z' in \"Hello\")" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print('el' in \"Hello\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### String Operations" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "# Many built-in string functions are available. The syntax to use them is string.function() as shown below,\n", "\n", "name = \"Python3 is Open Source\"\n", "\n", "print(name.isalpha()) # checks if all the characters in string are alphabets" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n", "True\n" ] } ], "source": [ "print(name.isdigit()) # checks if all the characters in string are digits\n", "\n", "sam = \"1234\"\n", "print(sam.isdigit()) # prints True" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n", "True\n", "True\n", "False\n" ] } ], "source": [ "print(name.isalnum()) # checks if all the characters in the string are alphabets or digits\n", "\n", "sam = \"Sample\"\n", "print(sam.isalnum()) # print True\n", "\n", "sam = \"Sample123\"\n", "print(sam.isalnum()) # print True\n", "\n", "sam = \"Sample 123\" \n", "print(sam.isalnum()) # prints False because of space in the string" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n", "True\n", "False\n" ] } ], "source": [ "print(name.islower()) # checks if all the characters in the string are lowercase alphabets\n", "\n", "sam= \"sample\"\n", "print(sam.islower()) # print True\n", "\n", "sam = \"Sample\"\n", "print(sam.islower()) # print False because of capital S present in Sample" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n", "False\n", "False\n", "True\n" ] } ], "source": [ "print(name.isupper()) # checks if all the characters in the string are upppercase alphabets\n", "\n", "sam=\"sample\"\n", "print(sam.isupper()) # print False\n", "\n", "sam= \"Sample\"\n", "print(sam.isupper()) # print False\n", "\n", "sam= \"SAMPLE\"\n", "print(sam.isupper()) # print True because of all characters are uppercase" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "False\n" ] } ], "source": [ "name = \"Python3 is Open Source\"\n", "\n", "print(name.startswith('Python')) # checks if string starts with a value\n", "\n", "print(name.startswith(\"Good\")) # prints False" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "False\n" ] } ], "source": [ "print(name.endswith('Source')) # checks if string ends with a value\n", "\n", "print(name.endswith(\"Good\")) # prints False" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "PYTHON3 IS OPEN SOURCE\n", "Python3 is Open Source\n" ] } ], "source": [ "print(name.upper()) # converts string to uppercase, but given string remains unchanged\n", "\n", "print(name)" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "python3 is open source\n", "Python3 is Open Source\n" ] } ], "source": [ "print(name.lower()) # converts given string to lowercase, but given string remains unchaged\n", "\n", "print(name)" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python3 is open source\n", "Python3 is Open Source\n" ] } ], "source": [ "print(name.capitalize()) # converts only first character of string to uppercase, remaining becomes lowercase\n", "\n", "print(name) # original string remains unchanged" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "pYTHON3 IS oPEN sOURCE\n", "Python3 is Open Source\n" ] } ], "source": [ "print(name.swapcase()) # swap cases in the string\n", "\n", "print(name) # original string remains unchanged" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Python3 is Open Source \n" ] } ], "source": [ "name = \" Python3 is Open Source \"\n", "print(name) # prints with all spaces" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python3 is Open Source \n", " Python3 is Open Source \n" ] } ], "source": [ "print(name.lstrip()) # trim the string from the left. ie leading spaces get removed\n", "\n", "print(name) # original string remains unchanged" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Python3 is Open Source\n", " Python3 is Open Source \n" ] } ], "source": [ "print(name.rstrip()) # trim the string from the right. ie trailing spaces get removed\n", "\n", "print(name) # original string remains unchanged" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python3 is Open Source\n", " Python3 is Open Source \n" ] } ], "source": [ "print(name.strip()) # trim the string from the both side. ie leading and trailing spaces get removed\n", "\n", "print(name) # original string remains unchanged" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('Python3 ', 'is', ' Open Source')\n", "Python3 is Open Source\n" ] } ], "source": [ "name = \"Python3 is Open Source\"\n", "print(name.partition('is')) # partitions string into 3 parts at first occurance of a specified string\n", "\n", "print(name) # original string remains unchanged" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Python3', 'is', 'Open', 'Source']\n" ] } ], "source": [ "name = \"Python3 is Open Source\"\n", "list1 = name.split() # split the string at a specified seperator string, \n", " # In case of seperator is not given, space is considered a seperator\n", "\n", "print(list1)\n" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Ganesh Attarde', '36', 'gattarde@gmail.com']\n" ] } ], "source": [ "name = \"Ganesh Attarde,36,gattarde@gmail.com\"\n", "\n", "list1 = name.split(',')\n", "\n", "print(list1) # data seperated with seperator ',', mostly used in csv file" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I am 36 years old\n" ] } ], "source": [ "# str() function return a numeric string for its numeric arguments\n", "age = 36\n", "print('I am ' + str(age) + ' years old')" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "65\n", "A\n" ] } ], "source": [ "# chr() function return a string representing its Unicode value\n", "# ord() function does the reverse\n", "\n", "print(ord('A'))\n", "\n", "print(chr(65))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## BEST OF LUCK" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }