|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [ |
| 8 | + { |
| 9 | + "name": "stdout", |
| 10 | + "output_type": "stream", |
| 11 | + "text": [ |
| 12 | + "Hello world!\n", |
| 13 | + "Hello world!\n", |
| 14 | + "Hello world!\n", |
| 15 | + "12\n", |
| 16 | + "el\n", |
| 17 | + "False\n" |
| 18 | + ] |
| 19 | + } |
| 20 | + ], |
| 21 | + "source": [ |
| 22 | + "'''\n", |
| 23 | + "Variables are used to store values. A string is a series of characters, surrounded by single or double quotes.\n", |
| 24 | + "'''\n", |
| 25 | + "\n", |
| 26 | + "# Hello world \n", |
| 27 | + "print('Hello world!')\n", |
| 28 | + "\n", |
| 29 | + "# Hello world with a variable \n", |
| 30 | + "greeting = 'Hello world!'\n", |
| 31 | + "print(greeting)\n", |
| 32 | + "print(greeting)\n", |
| 33 | + "print(len(greeting))\n", |
| 34 | + "print(greeting[1:3])\n", |
| 35 | + "print('t' in greeting)" |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "code", |
| 40 | + "execution_count": 4, |
| 41 | + "metadata": {}, |
| 42 | + "outputs": [ |
| 43 | + { |
| 44 | + "name": "stdout", |
| 45 | + "output_type": "stream", |
| 46 | + "text": [ |
| 47 | + " this string is not start with other because there are couple of old values needs to replaced by new ones but this string ends with other\n", |
| 48 | + " THIS STRING IS NOT START WITH OTHER BECAUSE THERE ARE COUPLE OF OLD VALUES NEEDS TO REPLACED BY NEW ONES BUT THIS STRING ENDS WITH OTHER\n", |
| 49 | + "This string is not start with other because there are couple of old values needs to replaced by new ones but this string ends with other\n", |
| 50 | + "False\n", |
| 51 | + "False\n", |
| 52 | + "False\n", |
| 53 | + "False\n", |
| 54 | + "True\n", |
| 55 | + "47\n", |
| 56 | + " This string is not start with other because there are couple of new values needs to replaced by new ones but this string ends with other\n", |
| 57 | + "['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'This', 'string', 'is', 'not', 'start', 'with', 'other', 'because', 'there', 'are', 'couple', 'of', 'old', 'values', 'needs', 'to', 'replaced', 'by', 'new', 'ones', 'but', 'this', 'string', 'ends', 'with', 'other']\n", |
| 58 | + "l i s t\n" |
| 59 | + ] |
| 60 | + } |
| 61 | + ], |
| 62 | + "source": [ |
| 63 | + "# String Methods\n", |
| 64 | + "string_methods = ' This string is not start with other because there are couple of old values needs to replaced by new ones but this string ends with other'\n", |
| 65 | + "print(string_methods.lower()) \t\t\t\t\t# returns the lowercase version of the string\n", |
| 66 | + "print(string_methods.upper()) \t\t\t\t\t# returns the uppercase version of the string\n", |
| 67 | + "print(string_methods.strip()) \t\t\t\t\t# returns a string with whitespace removed from the start and end\n", |
| 68 | + "print(string_methods.isalpha()) \t\t\t\t# tests if all the string chars are in the various character classes\n", |
| 69 | + "print(string_methods.isdigit()) \t\t\t\t# tests if all the string chars are in the various character classes\n", |
| 70 | + "print(string_methods.isspace()) \t\t\t\t# tests if all the string chars are in the various character classes\n", |
| 71 | + "print(string_methods.startswith('other')) \t\t# tests if the string starts with the given other string\n", |
| 72 | + "print(string_methods.endswith('other')) \t\t# tests if the string ends with the given other string\n", |
| 73 | + "print(string_methods.find('other')) \t\t\t# searches for the given other string (not a regular expression) within string_value, and returns the first index where it begins or -1 if not found\n", |
| 74 | + "print(string_methods.replace('old', 'new')) \t# returns a string where all occurrences of 'old' have been replaced by 'new'\n", |
| 75 | + "delim = ' ';\n", |
| 76 | + "print(string_methods.split(delim)) \t\t\t\t# returns a list of substrings separated by the given delimiter\n", |
| 77 | + "print(delim.join(['l', 'i', 's', 't'])) \t\t# opposite of split(), joins the elements in the given list together using the string as the delimiter" |
| 78 | + ] |
| 79 | + }, |
| 80 | + { |
| 81 | + "cell_type": "code", |
| 82 | + "execution_count": 14, |
| 83 | + "metadata": {}, |
| 84 | + "outputs": [ |
| 85 | + { |
| 86 | + "name": "stdout", |
| 87 | + "output_type": "stream", |
| 88 | + "text": [ |
| 89 | + "First, thou shalt count to {0}\n", |
| 90 | + "Bring me a {}\n", |
| 91 | + "From {} to {}\n", |
| 92 | + "My name is Adhikari\n", |
| 93 | + "Weight in tons {0.weight}\n", |
| 94 | + "Units destroyed: {players[0]}\n", |
| 95 | + "a, b, c\n", |
| 96 | + "a, b, c\n", |
| 97 | + "c, b, a\n", |
| 98 | + "c, b, a\n", |
| 99 | + "abracadabra\n", |
| 100 | + "Welcome to Python\n", |
| 101 | + "My name is Annie and my age is 22\n" |
| 102 | + ] |
| 103 | + } |
| 104 | + ], |
| 105 | + "source": [ |
| 106 | + "# String formating using % operator\n", |
| 107 | + "text = \"%d little pigs come out or I'll %s and %s and %s\" % (3, 'huff', 'puff', 'blow down');\n", |
| 108 | + "name = 'Adhikari'\n", |
| 109 | + "# String formating using Format String Syntax\n", |
| 110 | + "print(\"First, thou shalt count to {0}\") # References first positional argument\n", |
| 111 | + "print(\"Bring me a {}\") # Implicitly references the first positional argument\n", |
| 112 | + "print(\"From {} to {}\") # Same as \"From {0} to {1}\"\n", |
| 113 | + "print(f\"My name is {name}\") # References keyword argument 'name'\n", |
| 114 | + "print(\"Weight in tons {0.weight}\") # 'weight' attribute of first positional arg\n", |
| 115 | + "print(\"Units destroyed: {players[0]}\") # First element of keyword argument 'players'.\n", |
| 116 | + "print('{0}, {1}, {2}'.format('a', 'b', 'c'))\n", |
| 117 | + "print('{}, {}, {}'.format('a', 'b', 'c'))\n", |
| 118 | + "print('{2}, {1}, {0}'.format('a', 'b', 'c'))\n", |
| 119 | + "print('{2}, {1}, {0}'.format(*'abc'))\n", |
| 120 | + "print('{0}{1}{0}'.format('abra', 'cad')) # arguments' indices can be repeated\n", |
| 121 | + "print(\"Welcome to %s\"%(\"Python\"))\n", |
| 122 | + "print(\"My name is %s and my age is %d\"%(\"Annie\",22))" |
| 123 | + ] |
| 124 | + }, |
| 125 | + { |
| 126 | + "cell_type": "code", |
| 127 | + "execution_count": 36, |
| 128 | + "metadata": {}, |
| 129 | + "outputs": [ |
| 130 | + { |
| 131 | + "name": "stdout", |
| 132 | + "output_type": "stream", |
| 133 | + "text": [ |
| 134 | + " this string is not start with other because there are couple of old values needs to replaced by new ones but this string ends with other\n", |
| 135 | + " THIS STRING IS NOT START WITH OTHER BECAUSE THERE ARE COUPLE OF OLD VALUES NEEDS TO REPLACED BY NEW ONES BUT THIS STRING ENDS WITH OTHER\n", |
| 136 | + "This string is not start with other because there are couple of old values needs to replaced by new ones but this string ends with other\n", |
| 137 | + "False\n", |
| 138 | + "False\n", |
| 139 | + "False\n", |
| 140 | + "False\n", |
| 141 | + "True\n", |
| 142 | + "33\n", |
| 143 | + " This string is not start with other because there are couple of new values needs to replaced by new ones but this string ends with other\n", |
| 144 | + "This string is not start with other because there are couple of old values needs to replaced by new ones but this string ends with other\n", |
| 145 | + "This string is not start with other because there are couple of old values needs to replaced by new ones but this string ends with other\n", |
| 146 | + "0\n", |
| 147 | + "b'This string is not start with other because there are couple of old values needs to replaced by new ones but this string ends with other'\n", |
| 148 | + "y\n", |
| 149 | + " \n", |
| 150 | + "This string is not start with oth--E--r because there are couple of old values needs to replaced by new ones but this string ends with other\n", |
| 151 | + "21\n", |
| 152 | + "['This', 'string', 'is', 'not', 'start', 'with', 'other', 'because', 'there', 'are', 'couple', 'of', 'old', 'values', 'needs', 'to', 'replaced', 'by', 'new', 'ones', 'but', 'this', 'string', 'ends', 'with', 'other']\n", |
| 153 | + "l i s t\n" |
| 154 | + ] |
| 155 | + } |
| 156 | + ], |
| 157 | + "source": [ |
| 158 | + "# String Methods\n", |
| 159 | + "string_methods = ' This string is not start with other because there are couple of old values needs to replaced by new ones but this string ends with other'\n", |
| 160 | + "print(string_methods.lower()) \t\t\t\t\t# returns the lowercase version of the string\n", |
| 161 | + "print(string_methods.upper()) \t\t\t\t\t# returns the uppercase version of the string\n", |
| 162 | + "print(string_methods.strip()) \t\t\t\t\t# returns a string with whitespace removed from the start and end\n", |
| 163 | + "print(string_methods.isalpha()) \t\t\t\t# tests if all the string chars are in the various character classes\n", |
| 164 | + "print(string_methods.isdigit()) \t\t\t\t# tests if all the string chars are in the various character classes\n", |
| 165 | + "print(string_methods.isspace()) \t\t\t\t# tests if all the string chars are in the various character classes\n", |
| 166 | + "print(string_methods.startswith('other')) \t\t# tests if the string starts with the given other string\n", |
| 167 | + "print(string_methods.endswith('other')) \t\t# tests if the string ends with the given other string\n", |
| 168 | + "print(string_methods.find('other')) \t\t\t# searches for the given other string (not a regular expression) within string_value, and returns the first index where it begins or -1 if not found\n", |
| 169 | + "print(string_methods.replace('old', 'new')) \t# returns a string where all occurrences of 'old' have been replaced by 'new'\n", |
| 170 | + "\n", |
| 171 | + "string_methods = string_methods.strip()\n", |
| 172 | + "print(string_methods)\n", |
| 173 | + "print(string_methods.capitalize())\n", |
| 174 | + "print(string_methods.count(\"ka\", 0, len(string_methods))) # Count number of occurrences of a given substring using start and end\n", |
| 175 | + "\n", |
| 176 | + "encoded_string_methods = string_methods.encode('utf-8','strict')\n", |
| 177 | + "print(encoded_string_methods)\n", |
| 178 | + "\n", |
| 179 | + "print(max(string_methods))\n", |
| 180 | + "print(min(string_methods))\n", |
| 181 | + "print(string_methods.replace('e','--E--',1))\n", |
| 182 | + "print(string_methods.index('a')) #Return index if it finds orthewise throws error\n", |
| 183 | + "\n", |
| 184 | + "delim = ' ';\n", |
| 185 | + "print(string_methods.split(delim)) \t\t\t\t# returns a list of substrings separated by the given delimiter\n", |
| 186 | + "print(delim.join(['l', 'i', 's', 't'])) \t\t# opposite of split(), joins the elements in the given list together using the string as the delimiter" |
| 187 | + ] |
| 188 | + }, |
| 189 | + { |
| 190 | + "cell_type": "code", |
| 191 | + "execution_count": 47, |
| 192 | + "metadata": {}, |
| 193 | + "outputs": [ |
| 194 | + { |
| 195 | + "name": "stdout", |
| 196 | + "output_type": "stream", |
| 197 | + "text": [ |
| 198 | + "Below line is to demonsrate character's position in string\n", |
| 199 | + " H e l l o\n", |
| 200 | + " 0 1 2 3 4\n", |
| 201 | + "-5 -4 -3 -2 -1\n", |
| 202 | + "02468\n", |
| 203 | + "gnirts ni noitisop s'retcarahc etarsnomed ot si enil woleB\n", |
| 204 | + "low line\n", |
| 205 | + "17\n" |
| 206 | + ] |
| 207 | + } |
| 208 | + ], |
| 209 | + "source": [ |
| 210 | + "# String Slices\n", |
| 211 | + "string_slices = ' H e l l o';\n", |
| 212 | + "test_string = 'Below line is to demonsrate character\\'s position in string'\n", |
| 213 | + "print(test_string)\n", |
| 214 | + "print(string_slices)\n", |
| 215 | + "print(' 0 1 2 3 4') \t# range(0, 5)\n", |
| 216 | + "print('-5 -4 -3 -2 -1') \t# range(-5, 0)\n", |
| 217 | + "\n", |
| 218 | + "# Hint for slicing is s[start:end:step]\n", |
| 219 | + "print('0123456789'[::2]) # Every 2nd char in string\n", |
| 220 | + "print(test_string[::-1]) # Reverse the string\n", |
| 221 | + "print(test_string[2:10])\n", |
| 222 | + "print(test_string.find('d'))\n" |
| 223 | + ] |
| 224 | + }, |
| 225 | + { |
| 226 | + "cell_type": "code", |
| 227 | + "execution_count": 62, |
| 228 | + "metadata": {}, |
| 229 | + "outputs": [ |
| 230 | + { |
| 231 | + "name": "stdout", |
| 232 | + "output_type": "stream", |
| 233 | + "text": [ |
| 234 | + "My full name is Albert Einstein\n", |
| 235 | + "I am Albert Einstein and I born in 1879\n" |
| 236 | + ] |
| 237 | + } |
| 238 | + ], |
| 239 | + "source": [ |
| 240 | + "# Concatenation (combining strings) \n", |
| 241 | + "first_name = 'Albert'\n", |
| 242 | + "last_name = 'Einstein';\n", |
| 243 | + "year_of_birth = 1879\n", |
| 244 | + "full_name = 'My full name is ' + first_name + ' ' + last_name\n", |
| 245 | + "print(full_name)\n", |
| 246 | + "# print('I am ' + first_name + ' ' + last_name + ' and I born in ' + str(year_of_birth))\n", |
| 247 | + "print('I am {0} {1} and I born in {2}'.format(first_name, last_name, year_of_birth))" |
| 248 | + ] |
| 249 | + } |
| 250 | + ], |
| 251 | + "metadata": { |
| 252 | + "kernelspec": { |
| 253 | + "display_name": "Python 3", |
| 254 | + "language": "python", |
| 255 | + "name": "python3" |
| 256 | + }, |
| 257 | + "language_info": { |
| 258 | + "codemirror_mode": { |
| 259 | + "name": "ipython", |
| 260 | + "version": 3 |
| 261 | + }, |
| 262 | + "file_extension": ".py", |
| 263 | + "mimetype": "text/x-python", |
| 264 | + "name": "python", |
| 265 | + "nbconvert_exporter": "python", |
| 266 | + "pygments_lexer": "ipython3", |
| 267 | + "version": "3.7.3" |
| 268 | + } |
| 269 | + }, |
| 270 | + "nbformat": 4, |
| 271 | + "nbformat_minor": 2 |
| 272 | +} |
0 commit comments