|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "## Escape Characters:\n", |
| 8 | + " - To insert characters that are illegal in a string, use an escape character.\n", |
| 9 | + " - An escape character is a backslash \\ followed by the character you want to insert.\n", |
| 10 | + " - An example of an illegal character is a double quote inside a string that is surrounded by double quotes:" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": 5, |
| 16 | + "metadata": {}, |
| 17 | + "outputs": [ |
| 18 | + { |
| 19 | + "name": "stdout", |
| 20 | + "output_type": "stream", |
| 21 | + "text": [ |
| 22 | + "We are the so-called \"AVENGERS\" from MARVEL.\n" |
| 23 | + ] |
| 24 | + } |
| 25 | + ], |
| 26 | + "source": [ |
| 27 | + "## if there is a character repeatation use \\\n", |
| 28 | + "txt = \"We are the so-called \\\"AVENGERS\\\" from MARVEL.\"\n", |
| 29 | + "print(txt)" |
| 30 | + ] |
| 31 | + }, |
| 32 | + { |
| 33 | + "cell_type": "code", |
| 34 | + "execution_count": 6, |
| 35 | + "metadata": {}, |
| 36 | + "outputs": [ |
| 37 | + { |
| 38 | + "name": "stdout", |
| 39 | + "output_type": "stream", |
| 40 | + "text": [ |
| 41 | + "Hey, what's up?\n" |
| 42 | + ] |
| 43 | + } |
| 44 | + ], |
| 45 | + "source": [ |
| 46 | + "## Another example of escape character\n", |
| 47 | + "greet = 'Hey, what\\'s up?'\n", |
| 48 | + "print(greet)" |
| 49 | + ] |
| 50 | + }, |
| 51 | + { |
| 52 | + "cell_type": "code", |
| 53 | + "execution_count": 8, |
| 54 | + "metadata": {}, |
| 55 | + "outputs": [ |
| 56 | + { |
| 57 | + "name": "stdout", |
| 58 | + "output_type": "stream", |
| 59 | + "text": [ |
| 60 | + "C:\\Users\\aborgall\\Desktop\n" |
| 61 | + ] |
| 62 | + } |
| 63 | + ], |
| 64 | + "source": [ |
| 65 | + "## Use case of backslash\n", |
| 66 | + "## to print a directory path in Windows, you'll need to escape each backslash in the string\n", |
| 67 | + "print(\"C:\\\\Users\\\\aborgall\\\\Desktop\")" |
| 68 | + ] |
| 69 | + }, |
| 70 | + { |
| 71 | + "cell_type": "markdown", |
| 72 | + "metadata": {}, |
| 73 | + "source": [ |
| 74 | + "## \\n is a newline\n", |
| 75 | + "## \\t is a tab" |
| 76 | + ] |
| 77 | + }, |
| 78 | + { |
| 79 | + "cell_type": "code", |
| 80 | + "execution_count": 11, |
| 81 | + "metadata": {}, |
| 82 | + "outputs": [ |
| 83 | + { |
| 84 | + "name": "stdout", |
| 85 | + "output_type": "stream", |
| 86 | + "text": [ |
| 87 | + "Name:Akash Borgalli\n", |
| 88 | + "Hobby:Coding\n", |
| 89 | + "Email:akash.borgalli@gmail.com\n" |
| 90 | + ] |
| 91 | + } |
| 92 | + ], |
| 93 | + "source": [ |
| 94 | + "## newline\n", |
| 95 | + "print(\"Name:Akash Borgalli\\nHobby:Coding\\nEmail:akash.borgalli@gmail.com\")" |
| 96 | + ] |
| 97 | + }, |
| 98 | + { |
| 99 | + "cell_type": "code", |
| 100 | + "execution_count": 13, |
| 101 | + "metadata": {}, |
| 102 | + "outputs": [ |
| 103 | + { |
| 104 | + "name": "stdout", |
| 105 | + "output_type": "stream", |
| 106 | + "text": [ |
| 107 | + "Alex\t\tMercer!\n" |
| 108 | + ] |
| 109 | + } |
| 110 | + ], |
| 111 | + "source": [ |
| 112 | + "## tab \n", |
| 113 | + "print(\"Alex\\t\\tMercer!\") " |
| 114 | + ] |
| 115 | + }, |
| 116 | + { |
| 117 | + "cell_type": "markdown", |
| 118 | + "metadata": {}, |
| 119 | + "source": [ |
| 120 | + "## The \\\\ escape character will represent a backslash character.\n" |
| 121 | + ] |
| 122 | + }, |
| 123 | + { |
| 124 | + "cell_type": "code", |
| 125 | + "execution_count": 16, |
| 126 | + "metadata": {}, |
| 127 | + "outputs": [ |
| 128 | + { |
| 129 | + "name": "stdout", |
| 130 | + "output_type": "stream", |
| 131 | + "text": [ |
| 132 | + "Howl's Moving Castle\n" |
| 133 | + ] |
| 134 | + } |
| 135 | + ], |
| 136 | + "source": [ |
| 137 | + "## This works because we are using double quotes to wrap string but we wrap with single quotes than it will give syntax error\n", |
| 138 | + "a = \"Howl's Moving Castle\"\n", |
| 139 | + "print(a)" |
| 140 | + ] |
| 141 | + }, |
| 142 | + { |
| 143 | + "cell_type": "code", |
| 144 | + "execution_count": 17, |
| 145 | + "metadata": {}, |
| 146 | + "outputs": [ |
| 147 | + { |
| 148 | + "name": "stdout", |
| 149 | + "output_type": "stream", |
| 150 | + "text": [ |
| 151 | + "hello,\n", |
| 152 | + "My name is Akash Borgalli,\n", |
| 153 | + "I love coding\n", |
| 154 | + "My favourite language is Python .\n" |
| 155 | + ] |
| 156 | + } |
| 157 | + ], |
| 158 | + "source": [ |
| 159 | + "## Multiline strings allow you to use newlines in strings without the \\n escape character it can be double quotes*3 or singlequotes*3\n", |
| 160 | + "m = \"\"\"hello,\n", |
| 161 | + "My name is Akash Borgalli,\n", |
| 162 | + "I love coding\n", |
| 163 | + "My favourite language is Python .\"\"\"\n", |
| 164 | + "print(m)" |
| 165 | + ] |
| 166 | + }, |
| 167 | + { |
| 168 | + "cell_type": "code", |
| 169 | + "execution_count": 19, |
| 170 | + "metadata": {}, |
| 171 | + "outputs": [ |
| 172 | + { |
| 173 | + "name": "stdout", |
| 174 | + "output_type": "stream", |
| 175 | + "text": [ |
| 176 | + "e\n", |
| 177 | + "Hello\n", |
| 178 | + "Hello\n", |
| 179 | + "lo, world!\n" |
| 180 | + ] |
| 181 | + } |
| 182 | + ], |
| 183 | + "source": [ |
| 184 | + "print('Hello, world!'[1])\n", |
| 185 | + "print('Hello, world!'[0:5])\n", |
| 186 | + "print('Hello, world!'[:5])\n", |
| 187 | + "print('Hello, world!'[3:])" |
| 188 | + ] |
| 189 | + }, |
| 190 | + { |
| 191 | + "cell_type": "code", |
| 192 | + "execution_count": 20, |
| 193 | + "metadata": {}, |
| 194 | + "outputs": [ |
| 195 | + { |
| 196 | + "name": "stdout", |
| 197 | + "output_type": "stream", |
| 198 | + "text": [ |
| 199 | + "HELLO\n", |
| 200 | + "True\n", |
| 201 | + "hello\n" |
| 202 | + ] |
| 203 | + } |
| 204 | + ], |
| 205 | + "source": [ |
| 206 | + "print('Hello'.upper())\n", |
| 207 | + "print('Hello'.upper().isupper())\n", |
| 208 | + "print('Hello'.upper().lower())" |
| 209 | + ] |
| 210 | + }, |
| 211 | + { |
| 212 | + "cell_type": "code", |
| 213 | + "execution_count": 22, |
| 214 | + "metadata": {}, |
| 215 | + "outputs": [ |
| 216 | + { |
| 217 | + "name": "stdout", |
| 218 | + "output_type": "stream", |
| 219 | + "text": [ |
| 220 | + "['Remember,', 'remember,', 'the', 'fifth', 'of', 'July.']\n", |
| 221 | + "There-can-only-one.\n" |
| 222 | + ] |
| 223 | + } |
| 224 | + ], |
| 225 | + "source": [ |
| 226 | + "## splits the whole sentence into individual words\n", |
| 227 | + "print('Remember, remember, the fifth of July.'.split())\n", |
| 228 | + "## join is used to join with something\n", |
| 229 | + "print('-'.join('There can only one.'.split()))" |
| 230 | + ] |
| 231 | + }, |
| 232 | + { |
| 233 | + "cell_type": "markdown", |
| 234 | + "metadata": {}, |
| 235 | + "source": [ |
| 236 | + "## The rjust(), ljust(), and center() string methods, respectively" |
| 237 | + ] |
| 238 | + }, |
| 239 | + { |
| 240 | + "cell_type": "markdown", |
| 241 | + "metadata": {}, |
| 242 | + "source": [ |
| 243 | + "## The lstrip() and rstrip() methods remove whitespace from the left and right ends of a string, respectively." |
| 244 | + ] |
| 245 | + } |
| 246 | + ], |
| 247 | + "metadata": { |
| 248 | + "kernelspec": { |
| 249 | + "display_name": "Python 3", |
| 250 | + "language": "python", |
| 251 | + "name": "python3" |
| 252 | + }, |
| 253 | + "language_info": { |
| 254 | + "codemirror_mode": { |
| 255 | + "name": "ipython", |
| 256 | + "version": 3 |
| 257 | + }, |
| 258 | + "file_extension": ".py", |
| 259 | + "mimetype": "text/x-python", |
| 260 | + "name": "python", |
| 261 | + "nbconvert_exporter": "python", |
| 262 | + "pygments_lexer": "ipython3", |
| 263 | + "version": "3.8.5" |
| 264 | + } |
| 265 | + }, |
| 266 | + "nbformat": 4, |
| 267 | + "nbformat_minor": 4 |
| 268 | +} |
0 commit comments