{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 2. String\n", "\n", "Get ready to master strings in Python! This section will teach you how to create, manipulate, and work with text data effectively.\n", "\n", "**Strings** are used to store text data in Python. They're considered sequences, meaning Python keeps track of each character in order. This allows us to access specific characters using indexing. Strings are like ordered lists of characters in Python. This means we can refer to individual characters by their position.\n", "\n", "We'll learn about the following topics:\n", "\n", " - [2.1. Creating Strings](#Creating_Strings)\n", " - [2.2. Printing Strings](#Printing_Strings)\n", " - [2.3. Built-in String Functions](#Builtin_String_Functions)\n", " - [2.4. String Indexing](#String_Indexing)\n", " - [2.5. String Properties](#String_Properties)\n", " - [2.6. String Operators](#String_Operators)\n", " - [2.7. Built-in String Methods](#Builtin_String_Methods)\n", " - [2.8. String Formatting](#String_Formatting)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<p align=\"center\">\n", " <img width=\"550\" height=\"300\" src=\"https://realpython.com/cdn-cgi/image/width=960,format=auto/https://files.realpython.com/media/Strings-and-Character-Data-in-Python_Watermarked.797803948b10.jpg\">\n", "</p>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<table>\n", " <thead>\n", " <tr>\n", " <th>Name</th>\n", " <th>Type in Python</th>\n", " <th>Description</th>\n", " <th>Example</th>\n", " </tr>\n", " </thead>\n", " <tbody>\n", " <tr>\n", " <td>Strings</td>\n", " <td>str</td>\n", " <td>Ordered sequence of characters, using the syntax of either single quotes or double quotes.</td>\n", " <td>'hello' \"Hello\" \"i don't do that\" \"2000f\"</td> \n", " </tr>\n", " </tbody>\n", "</table>" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type('hello')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<a name='Creating_Strings'></a>\n", "\n", "## 2.1. Creating Strings:\n", "\n", "Strings in Python are created by enclosing text within either single or double quotation marks. This allows Python to differentiate between text and code." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hello'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Single Quote\n", "'hello'" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'beauty is in the eye of the beholder'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#phrase \n", "'beauty is in the eye of the beholder'" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'beauty is in the eye of the beholder'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Double Quotes\n", "\"beauty is in the eye of the beholder\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Please note that if you use ' you also have to use `'` at the end of string not `\"`. Moreover, if your text contains `'` in itself, you have to use `\"` for determining the string." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "\"I'm happy\"" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"I'm happy\"" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (3217421119.py, line 1)", "output_type": "error", "traceback": [ "\u001b[1;36m File \u001b[1;32m\"C:\\Users\\Dear User\\AppData\\Local\\Temp\\ipykernel_4676\\3217421119.py\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m 'I'm happy'\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "'I'm happy'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This error occurred because `'` in the I'm stopped the string. Python will treat the second `'` as an ending character." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<a name='Printing_Strings'></a>\n", "\n", "## 2.2. Printing Strings:\n", "\n", "In Jupyter Notebook, strings are automatically displayed when you enter them into a cell. However, the proper way to print strings in your output is to use the `print()` function." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that we can't output multiple strings without print function. only the last string in a cell will be displayed." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello World 2'" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Hello World 1'\n", "'Hello World 2'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We should use a print statement to print a string." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World 1\n", "Hello World 2\n" ] } ], "source": [ "#Using Print\n", "print('Hello World 1')\n", "print('Hello World 2')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<table>\n", "<tr>\n", " <th>Special Escape Character</th> \n", " <th>Result</th>\n", "</tr>\n", "\n", "<tr>\n", " <td>\\n</td>\n", " <td>New Line</td> \n", "</tr>\n", "\n", "<tr>\n", " <td>\\t</td> \n", " <td>Tab</td> \n", "</tr>\n", " </table>" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "beauty \t is in the eye of the beholder\n" ] } ], "source": [ "#\\t\n", "print('beauty \\t is in the eye of the beholder')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<a name='Builtin_String_Functions'></a>\n", "\n", "## 2.3. Built-in String Functions:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**`len()`**\n", "\n", "Python's built-in len() function counts all of the characters in the string, including spaces and punctuation." ] }, { "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": [ "**`str()`**\n", "\n", "Returns a string representation of an object." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'25.6'" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str(25.6)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<a name='String_Indexing'></a>\n", "\n", "## 2.4. String Indexing:\n", "\n", "Strings are sequences, so we can access specific parts using indexes. Python uses square brackets `[]` for indexing, and it starts from **0**.\n", "\n", "<p align=\"center\">\n", " <img width=\"550\" height=\"300\" src=\"https://files.realpython.com/media/t.f398a9e25af0.png\">\n", "</p>" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'beauty is in the eye of the beholder'" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Assign a as a string\n", "a = 'beauty is in the eye of the beholder'\n", "\n", "a" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "beauty is in the eye of the beholder\n" ] } ], "source": [ "#Print the object\n", "print(a) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In general, if you want nth character of a string<code>name_of_string[n-1]<code>" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'b'" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Show first element\n", "a[0]" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'e'" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Show second element\n", "a[1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To get a portion of a string, we can use slicing with the colon `:`.\n", "\n", "In general if you want to get everything from nth index to mth index use <code>name_of_string[n:m+1]<code>" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'auty '" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[2:7]" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'eauty is in the eye of the beholder'" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Grab everything past the first term all the way to the length of a which is len(a)\n", "a[1:]" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "'bea'" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Grab every characters up to the 3rd index\n", "a[:3]" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'beauty is in the eye of the beholder'" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Grab Everything\n", "a[:]" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'r'" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[len(a)-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "String indices can also be specified with negative numbers, in which case indexing occurs from the end of the string backward: -1 refers to the last character, -2 the second-to-last character, and so on.\n", "\n", "<p align=\"center\">\n", " <img width=\"550\" height=\"300\" src=\"https://files.realpython.com/media/t.5089888b3d9f.png\">\n", "</p>" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'r'" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Last Chatacter (one index behind 0 so it loops back around)\n", "a[-1]" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'d'" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[-3]" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'b'" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[-len(a)]" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'beauty is in the eye of the beholde'" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Grab everything but the last Character\n", "a[:-1]" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'beauty is in the eye of the beholder'" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "old\n", " of\n" ] }, { "data": { "text/plain": [ "False" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print(a[-5:-2])\n", "print(a[20:23])\n", "a[-5:-2] == a[20:23]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Slicing can also have a third elememnt.\n", "\n", "`[start:stop:step]`\n", "\n", "- start: numerical index for the slice index\n", "- stop: index you will go up to but not include\n", "- step: the size of jump you take" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'beauty is in the eye of the beholder'" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[::1]" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'bat si h y ftebhle'" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Grab everything, but go in step sizes of 2\n", "a[::2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can specify a negative step value as well, in which case Python steps backward through the string. In that case, the starting/first index should be greater than the ending/second index." ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' ta'" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[6:0:-2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is a common paradigm for reversing a string." ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'redloheb eht fo eye eht ni si ytuaeb'" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[::-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<a name='String_Properties'></a>\n", "\n", "## 2.5. String Properties:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- **immutability**: Strings in Python are immutable, meaning you can't change individual characters within them once they're created." ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'str' object does not support item assignment", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_4676\\3686050719.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m#Let's try to change the first letter to 'c'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'c'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: 'str' object does not support item assignment" ] } ], "source": [ "#Let's try to change the first letter to 'c'\n", "a[0] = 'c'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In truth, there really isn’t much need to modify strings. You can usually easily accomplish what you want by generating a copy of the original string that has the desired change in place. There are very many ways to do this in Python. Here is one possibility:" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ceauty is in the eye of the beholder'" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'c' + a[1:]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- **concatenate**: Concatenation refers to the process of joining two or more strings together to form a new, longer string. In Python, you can concatenate strings using the addition operator `+`." ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'beauty is in the eye of the beholderbeauty is in the eye of the beholder'" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a + a" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'beauty is in the eye of the beholder beauty is in the eye of the beholder'" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a + ' ' + a" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'beauty is in the eye of the beholder New Sentence'" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a + ' New Sentence'" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'beauty is in the eye of the beholder'" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[:3] + a[3:]" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'beauty is in the eye of the beholder'" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The original a string is unchanged until you reassign a." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- **Reassignment**" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [], "source": [ "a = a + ' New Sentence!'" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "beauty is in the eye of the beholder New Sentence!\n" ] } ], "source": [ "print(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- **Repetition**: In Python, strings support the repetition property, which allows you to create new strings by repeating an existing string multiple times. This is achieved using the multiplication operator `*`." ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'beauty is in the eye of the beholder New Sentence!beauty is in the eye of the beholder New Sentence!'" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a * 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<a name='String_Operators'></a>\n", "\n", "## 2.6. String Operators:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- **`in`**: Python also provides a membership operator that can be used with strings. The in operator returns True if the first operand is contained within the second, and False otherwise." ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'W' in a" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'b' in a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- **`not in`**: The not in operator returns True if the first operand is not contained within the second, and False otherwise." ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'W' not in a" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'b' not in a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are also comparison operators which will be discussed later." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<a name='Builtin_String_Methods'></a>\n", "\n", "## 2.7. Built-in String Methods:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Objects in Python have built-in functions called methods. These methods are functions inside the object (we will learn about these in much more depth later) that can perform actions or commands on the object itself.\n", "\n", "We call methods with a period and then the method name. Methods are in the form:\n", "\n", "`object.method(parameters)`\n", "\n", "Where parameters are extra arguments we can pass into the method." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<table>\n", " <thead>\n", " <tr>\n", " <th>Method</th>\n", " <th>Description</th>\n", " </tr>\n", " </thead>\n", " <tbody>\n", " <tr>\n", " <td>upper()</td>\n", " <td>upper case a string</td>\n", " </tr>\n", " <tr>\n", " <td>lower()</td>\n", " <td>lower case a string</td>\n", " </tr>\n", " <tr>\n", " <td>split()</td>\n", " <td>Split a string by blank space</td>\n", " </tr> \n", " <tr>\n", " <td>split(m)</td>\n", " <td>Split a string by the element m (doesn't include the element that was split on)</td>\n", " </tr> \n", " <tr>\n", " <td>replace(oldvalue,newvalue,count(optional))</td>\n", " <td>replaces a specified phrase with another specified phrase</td>\n", " </tr> \n", " <tr>\n", " <td>capitalize()</td>\n", " <td>first character converted to uppercase and all other characters converted to lowercase</td>\n", " </tr>\n", " <tr>\n", " <td>swapcase()</td>\n", " <td>uppercase alphabetic characters converted to lowercase and vice versa</td>\n", " </tr>\n", " <tr>\n", " <td>title()</td>\n", " <td>first letter of each word is converted to uppercase and remaining letters are lowercase</td>\n", " </tr> \n", " <tr>\n", " <td>count(m,a(optional),b(optional))</td>\n", " <td>number of occurrences of m within the substring indicated by a and b</td>\n", " </tr> \n", " <tr>\n", " <td>endswith(m,a(optional),b(optional))</td>\n", " <td>returns True if an string ends with m within the substring indicated by a and b</td>\n", " </tr>\n", " <tr>\n", " <td>find(m,a(optional),b(optional))</td>\n", " <td>see if a string contains a m within the substring indicated by a and b and returns the lowest index</td>\n", " </tr>\n", " <tr>\n", " <td>rfind(m,a(optional),b(optional))</td>\n", " <td>see if a string contains a m starting at the end within the substring indicated by a and b and returns the lowest index</td>\n", " </tr> \n", " <tr>\n", " <td>index(m,a(optional),b(optional))</td>\n", " <td>the index of first occurrence of m for a given substring indicated by a and b</td>\n", " </tr> \n", " <tr>\n", " <td>rindex(m,a(optional),b(optional))</td>\n", " <td>the index of first occurrence of m starting at the end for a given substring indicated by a and b</td>\n", " </tr> \n", " </tbody>\n", "</table>" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'beauty is in the eye of the beholder New Sentence!'" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'BEAUTY IS IN THE EYE OF THE BEHOLDER NEW SENTENCE!'" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.upper()" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'beauty is in the eye of the beholder new sentence!'" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.lower()" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['beauty',\n", " 'is',\n", " 'in',\n", " 'the',\n", " 'eye',\n", " 'of',\n", " 'the',\n", " 'beholder',\n", " 'New',\n", " 'Sentence!']" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.split()" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['', 'eauty is in the eye of the ', 'eholder New Sentence!']" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.split('b')" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'beauty is in the eye of the beholder New Sentence!'" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ceauty is in the eye of the ceholder New Sentence!'" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.replace('b','c')" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ceauty is in the eye of the beholder New Sentence!'" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.replace('b','c',1)" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Beauty is in the eye of the beholder new sentence!'" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.capitalize()" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'BEAUTY IS IN THE EYE OF THE BEHOLDER nEW sENTENCE!'" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.swapcase()" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Beauty Is In The Eye Of The Beholder New Sentence!'" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.title()" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.count('b')" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'beauty is in the eye of the beholder New Sentence!'" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.count('b',0,3)" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.endswith('!')" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.endswith('!',0,3)" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "41" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.find('Sentence!')" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.find('b')" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "28" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.rfind('b')" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.index('b')" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "28" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.rindex('b')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`.rindex()` is identical to `.rfind()`, except that it raises an exception if m is not found rather than returning -1." ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "substring not found", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_4676\\1048334928.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'x'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mValueError\u001b[0m: substring not found" ] } ], "source": [ "a.rindex('x')" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.rfind('x')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<a name='String_Formatting'></a>\n", "\n", "## 2.8. String Formatting:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**`.format()`**: We can use the `.format()` method to add formatted objects to printed string statements. " ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Insert another string with curly brackets: The inserted string'" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Insert another string with curly brackets: {}'.format('The inserted string')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**f-string**" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The product of 23 and 25 is 575\n" ] } ], "source": [ "n = 23\n", "m = 25\n", "prod = n * m\n", "print(f'The product of {n} and {m} is {prod}')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.13" } }, "nbformat": 4, "nbformat_minor": 4 }