{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "

Strings in Python

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Day Obejctives\n", "\n", "- Strings\n", " - Slicing in Python\n", " - String Methods\n", "- Lists\n", " - Accessing the Elements from the list\n", " - List Methods\n", "- Tuple\n", " - Accessing the Elements from the Tuple\n", " - Tuple Methods" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "s1 = \"\"\"Python is an interpreted high-level general-purpose programming language. Python's design philosophy emphasizes code readability with its notable use of significant indentation. Wikipedia\n", "Developer: Python Software Foundation\n", "Stable release: 3.9.5 / 3 May 2021; 19 days ago\n", "Preview release: 3.10.0b1 / 3 May 2021; 19 days ago\n", "Typing discipline: Duck, dynamic, strong typing; gradual (since 3.5, but ignored in CPython)\n", "First appeared: February 1991; 30 years ago\n", "Paradigm: Multi-paradigm: object-oriented, procedural (imperative), functional, structured, reflective\"\"\"" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "P g r\n" ] } ], "source": [ "print(s1[0], s1[55], s1[-10])" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "565\n" ] } ], "source": [ "print(len(s1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### String Slicing\n", "\n", "#### Syntax\n", "\n", "string_var[startingIndex: EndingIndex]\n", "\n", "- Default SI -> 0\n", "- Default EI -> Length if string -1" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python\n" ] } ], "source": [ "print(s1[0: 6])" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python is an interpreted high-level general-purpose programming language. Python's design philosophy\n" ] } ], "source": [ "print(s1[0: 100])" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "radigm: Multi-paradigm: object-oriented, procedural (imperative), functional, structured, reflective\n" ] } ], "source": [ "print(s1[-100: ])" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " \n" ] } ], "source": [ "print(s1[6])" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python is an in Python is an in\n" ] } ], "source": [ "print(s1[: 15], s1[0: 15])" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python is an interpreted high-level general-purpose programming language. Python's design philosophy emphasizes code readability with its notable use of significant indentation. Wikipedia\n", "Developer: Python Software Foundation\n", "Stable release: 3.9.5 / 3 May 2021; 19 days ago\n", "Preview release: 3.10.0b1 / 3 May 2021; 19 days ago\n", "Typing discipline: Duck, dynamic, strong typing; gradual (since 3.5, but ignored in CPython)\n", "First appeared: February 1991; 30 years ago\n", "Paradigm: Multi-paradigm: object-oriented, procedural (imperative), functional, structured, reflective\n" ] } ], "source": [ "print(s1[:])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "string_var[startingIndex: EndingIndex: stepSize]" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Pto s\n" ] } ], "source": [ "print(s1[0: 10: 2])" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Ph \n" ] } ], "source": [ "print(s1[0: 10: 3])" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "r,eucrs,aotnf,eiaem(lrdcr dter-cjo:gdrpilM:gdrPoasay0 19 rubF:eapatrF)otP idrnitb,. ci(luag;nptgot cmnd,cD:nlisdgiy\n", "g yd9 10 a b.13:see evr\n", "g yd9 10 a .. ealrebt\n", "otduFeato otP:eoee\n", "ieii nianditaiigsf s lao t twyiiaareo eiapeypslh gsdsnhy eaga nmagr spu-aee ee-ghdtrrtin inhy\n" ] } ], "source": [ "print(s1[-10: :-2])" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "rfetv\n" ] } ], "source": [ "print(s1[-10: :2])" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "evitcelfer ,derutcurts ,lanoitcnuf ,)evitarepmi( larudecorp ,detneiro-tcejbo :mgidarap-itluM :mgidaraP\n", "oga sraey 03 ;1991 yraurbeF :deraeppa tsriF\n", ")nohtyPC ni derongi tub ,5.3 ecnis( laudarg ;gnipyt gnorts ,cimanyd ,kcuD :enilpicsid gnipyT\n", "oga syad 91 ;1202 yaM 3 / 1b0.01.3 :esaeler weiverP\n", "oga syad 91 ;1202 yaM 3 / 5.9.3 :esaeler elbatS\n", "noitadnuoF erawtfoS nohtyP :repoleveD\n", "aidepikiW .noitatnedni tnacifingis fo esu elbaton sti htiw ytilibadaer edoc sezisahpme yhposolihp ngised s'nohtyP .egaugnal gnimmargorp esoprup-lareneg level-hgih deterpretni na si nohtyP\n" ] } ], "source": [ "print(s1[ : : -1])" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python is an interpreted high-level general-purpose programming language. Python's design philosophy emphasizes code readability with its notable use of significant indentation. Wikipedia\n", "Developer: Python Software Foundation\n", "Stable release: 3.9.5 / 3 May 2021; 19 days ago\n", "Preview release: 3.10.0b1 / 3 May 2021; 19 days ago\n", "Typing discipline: Duck, dynamic, strong typing; gradual (since 3.5, but ignored in CPython)\n", "First appeared: February 1991; 30 years ago\n", "Paradigm: Multi-paradigm: object-oriented, procedural (imperative), functional, structured, reflective\n" ] } ], "source": [ "print(s1[ : : 1])" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "eicle drtut lnicu )vtrpi aueop,enioteb miaa-tu miaa\n", "g re 3;91yare drep si\n", "nhyCn eog u 53ens adr giy nrs,iay ku eipci npToasa 1;22yM3/100. ealrwiePoasa 1;22yM3/593:see laSniano rwfSnhy rplvDadpkW.otten ncfni oeuebtnsihi tlbde dcszshm hooipnie 'otP.gunlgimropeorplrnglvlhi eepen as otP\n" ] } ], "source": [ "print(s1[: : -2])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### String Methods" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "APSSDC DATA SCIENCE TRAINING PROGRAM\n" ] } ], "source": [ "s = 'Apssdc Data Science training program'\n", "\n", "print(s.upper())" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Apssdc Data Science training program\n" ] } ], "source": [ "print(s)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "apssdc data science training program\n" ] } ], "source": [ "print(s.lower())" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "aPSSDC dATA sCIENCE TRAINING PROGRAM\n" ] } ], "source": [ "print(s.swapcase())" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True False\n" ] } ], "source": [ "su = 'APSSDC'\n", "sl = 'python'\n", "sn = '9876543210'\n", "\n", "\n", "print(su.isupper(), sl.isupper())" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True False\n" ] } ], "source": [ "print(sl.islower(), su.islower())" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False True\n" ] } ], "source": [ "print(su.isdigit(), sn.isdigit())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Validating indian mobile Number\n", "\n", "\n", "- +91 -> \n", "- 10 digits -> len()\n", "- 9,8,7,6" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "+91987654321o is not valid indian mobile number\n" ] } ], "source": [ "n1 = '+919876543210'\n", "n3 = '9876543210'\n", "n2 = '+91563214565'\n", "n4 = '+5632147890'\n", "n1 = '+91987654321o'\n", "\n", "if n1[0] == '+':\n", " if n1[1:3] == '91' and len(n1[3: ]) == 10 and n1[3] in '9876' and n1[1: ].isdigit():\n", " print(n1, 'is valid indian mobile number')\n", " else:\n", " print(n1, 'is not valid indian mobile number')\n", "elif len(n1) == 10 and n1[0] in '9876' and n1.isdigit():\n", " print(n1, 'is valid indian mobile number')\n", "else:\n", " print(n1, 'is not valid indian mobile number')" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "rollNo = '19KQ1A0237'\n", "\n", "print(rollNo.startswith('19'))" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": [ "email = 'anilkumar_t@apssdc.in'\n", "email2 = 'anil@gmail.com'" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "print(email2.endswith('apssdc.in'))" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "11\n" ] } ], "source": [ "email = 'anilkumar_t@apssdc.in'\n", "email2 = 'swamy@apssdc.in'\n", "\n", "print(email.index('@'))" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "anilkumar_t\n" ] } ], "source": [ "print(email[0: email.index('@')])" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "swamy\n" ] } ], "source": [ "print(email2[0: email2.index('@')])" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "apssdc\n" ] } ], "source": [ "email = 'anilkumar_t@apssdc.in'\n", "email2 = 'swamy@apssdc.in'\n", "email3 = 'anil@gmail.com'\n", "\n", "\n", "print(email2[email2.index('@') + 1: email2.index('.')])" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'gmail'" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "email3[email3.index('@') + 1: email3.index('.')]" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on method_descriptor:\n", "\n", "index(...)\n", " S.index(sub[, start[, end]]) -> int\n", " \n", " Return the lowest index in S where substring sub is found,\n", " such that sub is contained within S[start:end]. Optional\n", " arguments start and end are interpreted as in slice notation.\n", " \n", " Raises ValueError when the substring is not found.\n", "\n" ] } ], "source": [ "help(str.index)" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n" ] } ], "source": [ "email = 'anil@apssdc@in'\n", "\n", "print(email.index('@'))" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "11\n" ] } ], "source": [ "email = 'anil@apssdc@in'\n", "\n", "print(email.index('@', 5))" ] }, { "cell_type": "code", "execution_count": 63, "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\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0memail\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'anil@apssdc@in'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0memail\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'@'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m12\u001b[0m\u001b[1;33m)\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": [ "email = 'anil@apssdc@in'\n", "\n", "print(email.index('@', 12))" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n" ] } ], "source": [ "print(email.count('a'))" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n" ] } ], "source": [ "print(email.count('@'))" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "print(email.count('ss'))" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "APSSDC python APSSDC python APSSDC python\n" ] } ], "source": [ "s = ' APSSDC python '\n", "\n", "\n", "print(s.lstrip(), s.rstrip(), s.strip())" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "APSSDC python))))))) ((((APSSDC python APSSDC python\n" ] } ], "source": [ "s = '((((APSSDC python)))))))'\n", "\n", "\n", "print(s.lstrip('('), s.rstrip(')'), s.strip('()'))" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Cleaned data\n" ] } ], "source": [ "s = '((((()))))[[[[]]]]]]]{{{{{{}}}}}}Cleaned data{{{{}}}}}}}}}}]]]])))))'\n", "\n", "\n", "print(s.strip('([{}])'))" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python is an interpreted high-level general-purpose programming language. Python's design philosophy emphasizes code readability with its notable use of significant indentation. Wikipedia\n", "Developer: Python Software Foundation\n", "Stable release: 3.9.5 / 3 May 2021; 19 days ago\n", "Preview release: 3.10.0b1 / 3 May 2021; 19 days ago\n", "Typing discipline: Duck, dynamic, strong typing; gradual (since 3.5, but ignored in CPython)\n", "First appeared: February 1991; 30 years ago\n", "Paradigm: Multi-paradigm: object-oriented, procedural (imperative), functional, structured, reflective\n" ] } ], "source": [ "print(s1)" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Python', 'is', 'an', 'interpreted', 'high-level', 'general-purpose', 'programming', 'language.', \"Python's\", 'design', 'philosophy', 'emphasizes', 'code', 'readability', 'with', 'its', 'notable', 'use', 'of', 'significant', 'indentation.', 'Wikipedia\\nDeveloper:', 'Python', 'Software', 'Foundation\\nStable', 'release:', '3.9.5', '/', '3', 'May', '2021;', '19', 'days', 'ago\\nPreview', 'release:', '3.10.0b1', '/', '3', 'May', '2021;', '19', 'days', 'ago\\nTyping', 'discipline:', 'Duck,', 'dynamic,', 'strong', 'typing;', 'gradual', '(since', '3.5,', 'but', 'ignored', 'in', 'CPython)\\nFirst', 'appeared:', 'February', '1991;', '30', 'years', 'ago\\nParadigm:', 'Multi-paradigm:', 'object-oriented,', 'procedural', '(imperative),', 'functional,', 'structured,', 'reflective']\n" ] } ], "source": [ "print(s1.split(' '))" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[\"Python is an interpreted high-level general-purpose programming language. Python's design philosophy emphasizes code readability with its notable use of significant indentation. Wikipedia\", 'Developer: Python Software Foundation', 'Stable release: 3.9.5 / 3 May 2021; 19 days ago', 'Preview release: 3.10.0b1 / 3 May 2021; 19 days ago', 'Typing discipline: Duck, dynamic, strong typing; gradual (since 3.5, but ignored in CPython)', 'First appeared: February 1991; 30 years ago', 'Paradigm: Multi-paradigm: object-oriented, procedural (imperative), functional, structured, reflective']\n" ] } ], "source": [ "print(s1.split('\\n'))" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Python is an interpreted high-level general-purpose programming language', \" Python's design philosophy emphasizes code readability with its notable use of significant indentation\", ' Wikipedia\\nDeveloper: Python Software Foundation\\nStable release: 3', '9', '5 / 3 May 2021; 19 days ago\\nPreview release: 3', '10', '0b1 / 3 May 2021; 19 days ago\\nTyping discipline: Duck, dynamic, strong typing; gradual (since 3', '5, but ignored in CPython)\\nFirst appeared: February 1991; 30 years ago\\nParadigm: Multi-paradigm: object-oriented, procedural (imperative), functional, structured, reflective']\n" ] } ], "source": [ "print(s1.split('.'))\n", "\n", "sen = s1.split('.')" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"Python is an interpreted high-level general-purpose programming language. Python's design philosophy emphasizes code readability with its notable use of significant indentation. Wikipedia\\nDeveloper: Python Software Foundation\\nStable release: 3.9.5 / 3 May 2021; 19 days ago\\nPreview release: 3.10.0b1 / 3 May 2021; 19 days ago\\nTyping discipline: Duck, dynamic, strong typing; gradual (since 3.5, but ignored in CPython)\\nFirst appeared: February 1991; 30 years ago\\nParadigm: Multi-paradigm: object-oriented, procedural (imperative), functional, structured, reflective\"" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'.'.join(sen)" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"Python is an interpreted high-level general-purpose programming language********** Python's design philosophy emphasizes code readability with its notable use of significant indentation********** Wikipedia\\nDeveloper: Python Software Foundation\\nStable release: 3**********9**********5 / 3 May 2021; 19 days ago\\nPreview release: 3**********10**********0b1 / 3 May 2021; 19 days ago\\nTyping discipline: Duck, dynamic, strong typing; gradual (since 3**********5, but ignored in CPython)\\nFirst appeared: February 1991; 30 years ago\\nParadigm: Multi-paradigm: object-oriented, procedural (imperative), functional, structured, reflective\"" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'**********'.join(sen)" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python-is-an-interpreted-high-level-general-purpose-programming-language.-Python's-design-philosophy-emphasizes-code-readability-with-its-notable-use-of-significant-indentation.-Wikipedia\n", "Developer:-Python-Software-Foundation\n", "Stable-release:-3.9.5-/-3-May-2021;-19-days-ago\n", "Preview-release:-3.10.0b1-/-3-May-2021;-19-days-ago\n", "Typing-discipline:-Duck,-dynamic,-strong-typing;-gradual-(since-3.5,-but-ignored-in-CPython)\n", "First-appeared:-February-1991;-30-years-ago\n", "Paradigm:-Multi-paradigm:-object-oriented,-procedural-(imperative),-functional,-structured,-reflective\n" ] } ], "source": [ "print(s1.replace(' ', '-'))" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python Is An Interpreted High-Level General-Purpose Programming Language. Python'S Design Philosophy Emphasizes Code Readability With Its Notable Use Of Significant Indentation. Wikipedia\n", "Developer: Python Software Foundation\n", "Stable Release: 3.9.5 / 3 May 2021; 19 Days Ago\n", "Preview Release: 3.10.0B1 / 3 May 2021; 19 Days Ago\n", "Typing Discipline: Duck, Dynamic, Strong Typing; Gradual (Since 3.5, But Ignored In Cpython)\n", "First Appeared: February 1991; 30 Years Ago\n", "Paradigm: Multi-Paradigm: Object-Oriented, Procedural (Imperative), Functional, Structured, Reflective\n" ] } ], "source": [ "print(s1.title())" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python is an interpreted high-level general-purpose programming language. python's design philosophy emphasizes code readability with its notable use of significant indentation. wikipedia\n", "developer: python software foundation\n", "stable release: 3.9.5 / 3 may 2021; 19 days ago\n", "preview release: 3.10.0b1 / 3 may 2021; 19 days ago\n", "typing discipline: duck, dynamic, strong typing; gradual (since 3.5, but ignored in cpython)\n", "first appeared: february 1991; 30 years ago\n", "paradigm: multi-paradigm: object-oriented, procedural (imperative), functional, structured, reflective\n" ] } ], "source": [ "print(s1.capitalize())" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "print(s1.istitle())" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "python is an interpreted high-level general-purpose programming language. python's design philosophy emphasizes code readability with its notable use of significant indentation. wikipedia\n", "developer: python software foundation\n", "stable release: 3.9.5 / 3 may 2021; 19 days ago\n", "preview release: 3.10.0b1 / 3 may 2021; 19 days ago\n", "typing discipline: duck, dynamic, strong typing; gradual (since 3.5, but ignored in cpython)\n", "first appeared: february 1991; 30 years ago\n", "paradigm: multi-paradigm: object-oriented, procedural (imperative), functional, structured, reflective\n" ] } ], "source": [ "print(s1.casefold())" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "s = 'Python Programming'\n", "\n", "\n", "print(s.istitle())" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I\"m certify that Anil from Institute, on Program, form Date\n" ] } ], "source": [ "certificate = 'I\"m certify that {} from Institute, on Program, form Date'.format('Anil')\n", "\n", "print(certificate)" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I\"m certify that Anil from Institute, on Program, form 01-June\n" ] } ], "source": [ "print('I\"m certify that {} from Institute, on Program, form {}'.format('Anil', '01-June'))" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I\"m certify that Anil from Institute, on Program, form 01-June, Anil performance is excellant\n" ] } ], "source": [ "print('I\"m certify that {0} from Institute, on Program, form {1}, {0} performance is excellant'.format('Anil', '01-June'))" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True True\n" ] } ], "source": [ "s = 'asdfg'\n", "s3 = 'asd456'\n", "\n", "print(s.isalpha(), s3.isalnum())" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "s4 = 'kubksvad wkek'\n", "\n", "print(s4.isalnum())" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [], "source": [ "s1 = \"\"\"Python is an interpreted high-level general-purpose programming language. Python's design philosophy emphasizes code readability with its notable use of significant indentation. Wikipedia\n", "Developer: Python Software Foundation\n", "Stable release: 3.9.5 / 3 May 2021; 19 days ago\n", "Preview release: 3.10.0b1 / 3 May 2021; 19 days ago\n", "Typing discipline: Duck, dynamic, strong typing; gradual (since 3.5, but ignored in CPython)\n", "First appeared: February 1991; 30 years ago\n", "Paradigm: Multi-paradigm: object-oriented, procedural (imperative), functional, structured, reflective\"\"\"" ] }, { "cell_type": "code", "execution_count": 98, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "P\ty\tt\th\to\tn\t \ti\ts\t \ta\tn\t \ti\tn\tt\te\tr\tp\tr\te\tt\te\td\t \th\ti\tg\th\t-\tl\te\tv\te\tl\t \tg\te\tn\te\tr\ta\tl\t-\tp\tu\tr\tp\to\ts\te\t \tp\tr\to\tg\tr\ta\tm\tm\ti\tn\tg\t \tl\ta\tn\tg\tu\ta\tg\te\t.\t \tP\ty\tt\th\to\tn\t'\ts\t \td\te\ts\ti\tg\tn\t \tp\th\ti\tl\to\ts\to\tp\th\ty\t \te\tm\tp\th\ta\ts\ti\tz\te\ts\t \tc\to\td\te\t \tr\te\ta\td\ta\tb\ti\tl\ti\tt\ty\t \tw\ti\tt\th\t \ti\tt\ts\t \tn\to\tt\ta\tb\tl\te\t \tu\ts\te\t \to\tf\t \ts\ti\tg\tn\ti\tf\ti\tc\ta\tn\tt\t \ti\tn\td\te\tn\tt\ta\tt\ti\to\tn\t.\t \tW\ti\tk\ti\tp\te\td\ti\ta\t\n", "\tD\te\tv\te\tl\to\tp\te\tr\t:\t \tP\ty\tt\th\to\tn\t \tS\to\tf\tt\tw\ta\tr\te\t \tF\to\tu\tn\td\ta\tt\ti\to\tn\t\n", "\tS\tt\ta\tb\tl\te\t \tr\te\tl\te\ta\ts\te\t:\t \t3\t.\t9\t.\t5\t \t/\t \t3\t \tM\ta\ty\t \t2\t0\t2\t1\t;\t \t1\t9\t \td\ta\ty\ts\t \ta\tg\to\t\n", "\tP\tr\te\tv\ti\te\tw\t \tr\te\tl\te\ta\ts\te\t:\t \t3\t.\t1\t0\t.\t0\tb\t1\t \t/\t \t3\t \tM\ta\ty\t \t2\t0\t2\t1\t;\t \t1\t9\t \td\ta\ty\ts\t \ta\tg\to\t\n", "\tT\ty\tp\ti\tn\tg\t \td\ti\ts\tc\ti\tp\tl\ti\tn\te\t:\t \tD\tu\tc\tk\t,\t \td\ty\tn\ta\tm\ti\tc\t,\t \ts\tt\tr\to\tn\tg\t \tt\ty\tp\ti\tn\tg\t;\t \tg\tr\ta\td\tu\ta\tl\t \t(\ts\ti\tn\tc\te\t \t3\t.\t5\t,\t \tb\tu\tt\t \ti\tg\tn\to\tr\te\td\t \ti\tn\t \tC\tP\ty\tt\th\to\tn\t)\t\n", "\tF\ti\tr\ts\tt\t \ta\tp\tp\te\ta\tr\te\td\t:\t \tF\te\tb\tr\tu\ta\tr\ty\t \t1\t9\t9\t1\t;\t \t3\t0\t \ty\te\ta\tr\ts\t \ta\tg\to\t\n", "\tP\ta\tr\ta\td\ti\tg\tm\t:\t \tM\tu\tl\tt\ti\t-\tp\ta\tr\ta\td\ti\tg\tm\t:\t \to\tb\tj\te\tc\tt\t-\to\tr\ti\te\tn\tt\te\td\t,\t \tp\tr\to\tc\te\td\tu\tr\ta\tl\t \t(\ti\tm\tp\te\tr\ta\tt\ti\tv\te\t)\t,\t \tf\tu\tn\tc\tt\ti\to\tn\ta\tl\t,\t \ts\tt\tr\tu\tc\tt\tu\tr\te\td\t,\t \tr\te\tf\tl\te\tc\tt\ti\tv\te\t" ] } ], "source": [ "for char in s1:\n", " print(char, end = '\\t')" ] }, { "cell_type": "code", "execution_count": 100, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "P\ty\tt\th\to\tn\ti\ts\ta\tn\ti\tn\tt\te\tr\tp\tr\te\tt\te\td\th\ti\tg\th\tl\te\tv\te\tl\tg\te\tn\te\tr\ta\tl\tp\tu\tr\tp\to\ts\te\tp\tr\to\tg\tr\ta\tm\tm\ti\tn\tg\tl\ta\tn\tg\tu\ta\tg\te\tP\ty\tt\th\to\tn\ts\td\te\ts\ti\tg\tn\tp\th\ti\tl\to\ts\to\tp\th\ty\te\tm\tp\th\ta\ts\ti\tz\te\ts\tc\to\td\te\tr\te\ta\td\ta\tb\ti\tl\ti\tt\ty\tw\ti\tt\th\ti\tt\ts\tn\to\tt\ta\tb\tl\te\tu\ts\te\to\tf\ts\ti\tg\tn\ti\tf\ti\tc\ta\tn\tt\ti\tn\td\te\tn\tt\ta\tt\ti\to\tn\tW\ti\tk\ti\tp\te\td\ti\ta\tD\te\tv\te\tl\to\tp\te\tr\tP\ty\tt\th\to\tn\tS\to\tf\tt\tw\ta\tr\te\tF\to\tu\tn\td\ta\tt\ti\to\tn\tS\tt\ta\tb\tl\te\tr\te\tl\te\ta\ts\te\tM\ta\ty\td\ta\ty\ts\ta\tg\to\tP\tr\te\tv\ti\te\tw\tr\te\tl\te\ta\ts\te\tb\tM\ta\ty\td\ta\ty\ts\ta\tg\to\tT\ty\tp\ti\tn\tg\td\ti\ts\tc\ti\tp\tl\ti\tn\te\tD\tu\tc\tk\td\ty\tn\ta\tm\ti\tc\ts\tt\tr\to\tn\tg\tt\ty\tp\ti\tn\tg\tg\tr\ta\td\tu\ta\tl\ts\ti\tn\tc\te\tb\tu\tt\ti\tg\tn\to\tr\te\td\ti\tn\tC\tP\ty\tt\th\to\tn\tF\ti\tr\ts\tt\ta\tp\tp\te\ta\tr\te\td\tF\te\tb\tr\tu\ta\tr\ty\ty\te\ta\tr\ts\ta\tg\to\tP\ta\tr\ta\td\ti\tg\tm\tM\tu\tl\tt\ti\tp\ta\tr\ta\td\ti\tg\tm\to\tb\tj\te\tc\tt\to\tr\ti\te\tn\tt\te\td\tp\tr\to\tc\te\td\tu\tr\ta\tl\ti\tm\tp\te\tr\ta\tt\ti\tv\te\tf\tu\tn\tc\tt\ti\to\tn\ta\tl\ts\tt\tr\tu\tc\tt\tu\tr\te\td\tr\te\tf\tl\te\tc\tt\ti\tv\te\t" ] } ], "source": [ "for char in s1:\n", " if char.isalpha():\n", " print(char, end = '\\t')" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\t9\t5\t3\t2\t0\t2\t1\t1\t9\t3\t1\t0\t0\t1\t3\t2\t0\t2\t1\t1\t9\t3\t5\t1\t9\t9\t1\t3\t0\t" ] } ], "source": [ "for char in s1:\n", " if char.isdigit():\n", " print(char, end = '\\t')" ] }, { "cell_type": "code", "execution_count": 103, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "o\ti\ta\ti\te\te\te\ti\te\te\te\te\ta\tu\to\te\to\ta\ti\ta\tu\ta\te\to\te\ti\ti\to\to\te\ta\ti\te\to\te\te\ta\ta\ti\ti\ti\ti\to\ta\te\tu\te\to\ti\ti\ti\ta\ti\te\ta\ti\to\ti\ti\te\ti\ta\te\te\to\te\to\to\ta\te\to\tu\ta\ti\to\ta\te\te\te\ta\te\ta\ta\ta\to\te\ti\te\te\te\ta\te\ta\ta\ta\to\ti\ti\ti\ti\te\tu\ta\ti\to\ti\ta\tu\ta\ti\te\tu\ti\to\te\ti\to\ti\ta\te\ta\te\te\tu\ta\te\ta\ta\to\ta\ta\ti\tu\ti\ta\ta\ti\to\te\to\ti\te\te\to\te\tu\ta\ti\te\ta\ti\te\tu\ti\to\ta\tu\tu\te\te\te\ti\te\t" ] } ], "source": [ "for char in s1:\n", " if char in 'AEIOUaeiou':\n", " print(char, end = '\\t')" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Alpha 426 Digits 30 Special Charac 109\n" ] } ], "source": [ "ac = 0\n", "nc = 0\n", "sc = 0\n", "\n", "for char in s1:\n", " if char.isalpha():\n", " ac += 1\n", " elif char.isdigit():\n", " nc += 1\n", " else:\n", " sc += 1\n", " \n", "print('Alpha', ac, 'Digits', nc, \"Special Charac\", sc)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problems\n", "\n", "- sum of the digits in the string\n", "- sum of the even digits and odd digits in the string" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Lists\n", "\n", "\n", "It is used to store the non-homogenous group of valid datatypes in python\n", "\n", "\n", "#### Properties\n", "\n", "- it is declared as `[]`\n", "- List mutable dataType\n", "- List is an ordered data\n", "- It is iterable" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [], "source": [ "li = []\n", "\n", "li2 = list()" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " \n" ] } ], "source": [ "print(type(li), type(li2))" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 5.5, (5+6j), 'Python', [1, 2, 3]]\n" ] } ], "source": [ "li = [1, 5.5, 5 + 6j, 'Python', [1, 2, 3]]\n", "\n", "print(li)" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "19KQ1A0237\n" ] } ], "source": [ "print(rollNo)" ] }, { "cell_type": "code", "execution_count": 109, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 109, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rollNo[-4:-2] == '02'" ] }, { "cell_type": "code", "execution_count": 110, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 110, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rollNo[-4:-2].endswith('02')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Day Outcomes\n", "\n", "- Strings\n", " - Slicing in Python\n", " - String Methods\n", "- Lists\n", " - Declaring a List" ] } ], "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.3" } }, "nbformat": 4, "nbformat_minor": 4 }