|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Python Everything:\n", |
| 8 | + "6) *Strings* in Python: Introduction.\n", |
| 9 | + "\n", |
| 10 | + "# رشته ها در پایتون: آشنایی \n", |
| 11 | + "By Hamed Shah-Hosseini<br>\n", |
| 12 | + "The code is at: https://github.com/ostad-ai/Python-Everything" |
| 13 | + ] |
| 14 | + }, |
| 15 | + { |
| 16 | + "cell_type": "markdown", |
| 17 | + "metadata": {}, |
| 18 | + "source": [ |
| 19 | + "Strings in Python are a sequence of characters\n", |
| 20 | + "- enclosed by single or double quotation marks:\n", |
| 21 | + "\n", |
| 22 | + "رشته ها در پایتون، دنبالهای از نویسه ها هستند\n", |
| 23 | + "- که با ماریک های بازگفت تک یا دوگانه، دربرگرفته شده" |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "markdown", |
| 28 | + "metadata": {}, |
| 29 | + "source": [ |
| 30 | + "Strings encloded with ' and \" are the same <br>\n", |
| 31 | + "رشته ها دربرگرفته شده با ' یا \" ، یکسان هستند" |
| 32 | + ] |
| 33 | + }, |
| 34 | + { |
| 35 | + "cell_type": "code", |
| 36 | + "execution_count": 3, |
| 37 | + "metadata": {}, |
| 38 | + "outputs": [ |
| 39 | + { |
| 40 | + "data": { |
| 41 | + "text/plain": [ |
| 42 | + "True" |
| 43 | + ] |
| 44 | + }, |
| 45 | + "execution_count": 3, |
| 46 | + "metadata": {}, |
| 47 | + "output_type": "execute_result" |
| 48 | + } |
| 49 | + ], |
| 50 | + "source": [ |
| 51 | + "s='hello world'\n", |
| 52 | + "s2=\"hello world\"\n", |
| 53 | + "s==s2" |
| 54 | + ] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "markdown", |
| 58 | + "metadata": {}, |
| 59 | + "source": [ |
| 60 | + "Accessing characters of a string:\n", |
| 61 | + "<br> دسترسی به نویسه های یک رشته" |
| 62 | + ] |
| 63 | + }, |
| 64 | + { |
| 65 | + "cell_type": "code", |
| 66 | + "execution_count": 32, |
| 67 | + "metadata": {}, |
| 68 | + "outputs": [ |
| 69 | + { |
| 70 | + "name": "stdout", |
| 71 | + "output_type": "stream", |
| 72 | + "text": [ |
| 73 | + "h, e, l, l, o, , w, o, r, l, d, \n", |
| 74 | + "\n", |
| 75 | + "h; e; l; l; o; ; w; o; r; l; d; " |
| 76 | + ] |
| 77 | + } |
| 78 | + ], |
| 79 | + "source": [ |
| 80 | + "for char in s:\n", |
| 81 | + " print(char,end=', ')\n", |
| 82 | + " \n", |
| 83 | + "print('\\n') # new line\n", |
| 84 | + "\n", |
| 85 | + "for i in range(len(s)):\n", |
| 86 | + " print(s[i],end='; ')" |
| 87 | + ] |
| 88 | + }, |
| 89 | + { |
| 90 | + "cell_type": "markdown", |
| 91 | + "metadata": {}, |
| 92 | + "source": [ |
| 93 | + "Replacing old substrings with new one:\n", |
| 94 | + "<br> جایگزینی زیررشته های کهنه با نو" |
| 95 | + ] |
| 96 | + }, |
| 97 | + { |
| 98 | + "cell_type": "code", |
| 99 | + "execution_count": 9, |
| 100 | + "metadata": {}, |
| 101 | + "outputs": [ |
| 102 | + { |
| 103 | + "name": "stdout", |
| 104 | + "output_type": "stream", |
| 105 | + "text": [ |
| 106 | + "original: hello world\n", |
| 107 | + "new one: helloworld\n" |
| 108 | + ] |
| 109 | + } |
| 110 | + ], |
| 111 | + "source": [ |
| 112 | + "s_no_space=s.replace(' ', '')\n", |
| 113 | + "print('original:',s)\n", |
| 114 | + "print('new one:', s_no_space)" |
| 115 | + ] |
| 116 | + }, |
| 117 | + { |
| 118 | + "cell_type": "markdown", |
| 119 | + "metadata": {}, |
| 120 | + "source": [ |
| 121 | + "Spliting a string based on a separator:\n", |
| 122 | + "<br>شکاندن یک رشته برپایه یک جداگر" |
| 123 | + ] |
| 124 | + }, |
| 125 | + { |
| 126 | + "cell_type": "code", |
| 127 | + "execution_count": 25, |
| 128 | + "metadata": {}, |
| 129 | + "outputs": [ |
| 130 | + { |
| 131 | + "name": "stdout", |
| 132 | + "output_type": "stream", |
| 133 | + "text": [ |
| 134 | + "['hello', 'world']\n" |
| 135 | + ] |
| 136 | + } |
| 137 | + ], |
| 138 | + "source": [ |
| 139 | + "separator=' '\n", |
| 140 | + "print(s.split(separator))" |
| 141 | + ] |
| 142 | + }, |
| 143 | + { |
| 144 | + "cell_type": "code", |
| 145 | + "execution_count": 14, |
| 146 | + "metadata": {}, |
| 147 | + "outputs": [ |
| 148 | + { |
| 149 | + "name": "stdout", |
| 150 | + "output_type": "stream", |
| 151 | + "text": [ |
| 152 | + "Original: hello world\n", |
| 153 | + "To upper: HELLO WORLD\n", |
| 154 | + "To lower: hello world\n", |
| 155 | + "To title: Hello World\n", |
| 156 | + "To capitalize: Hello world\n" |
| 157 | + ] |
| 158 | + } |
| 159 | + ], |
| 160 | + "source": [ |
| 161 | + "print('Original:',s)\n", |
| 162 | + "print('To upper:', s.upper())\n", |
| 163 | + "print('To lower: ',s.lower())\n", |
| 164 | + "print('To title:',s.title())\n", |
| 165 | + "print('To capitalize:',s.capitalize())" |
| 166 | + ] |
| 167 | + }, |
| 168 | + { |
| 169 | + "cell_type": "markdown", |
| 170 | + "metadata": {}, |
| 171 | + "source": [ |
| 172 | + "Concatenating strings:\n", |
| 173 | + "<br>بهم چسباندن رشته ها" |
| 174 | + ] |
| 175 | + }, |
| 176 | + { |
| 177 | + "cell_type": "code", |
| 178 | + "execution_count": 33, |
| 179 | + "metadata": {}, |
| 180 | + "outputs": [ |
| 181 | + { |
| 182 | + "name": "stdout", |
| 183 | + "output_type": "stream", |
| 184 | + "text": [ |
| 185 | + "To concatenate: hello world, it's me, bye\n" |
| 186 | + ] |
| 187 | + } |
| 188 | + ], |
| 189 | + "source": [ |
| 190 | + "s3=', it\\'s me'\n", |
| 191 | + "s4=', bye'\n", |
| 192 | + "print('To concatenate: ',s+s3+s4)" |
| 193 | + ] |
| 194 | + }, |
| 195 | + { |
| 196 | + "cell_type": "code", |
| 197 | + "execution_count": null, |
| 198 | + "metadata": {}, |
| 199 | + "outputs": [], |
| 200 | + "source": [] |
| 201 | + } |
| 202 | + ], |
| 203 | + "metadata": { |
| 204 | + "kernelspec": { |
| 205 | + "display_name": "Python 3", |
| 206 | + "language": "python", |
| 207 | + "name": "python3" |
| 208 | + }, |
| 209 | + "language_info": { |
| 210 | + "codemirror_mode": { |
| 211 | + "name": "ipython", |
| 212 | + "version": 3 |
| 213 | + }, |
| 214 | + "file_extension": ".py", |
| 215 | + "mimetype": "text/x-python", |
| 216 | + "name": "python", |
| 217 | + "nbconvert_exporter": "python", |
| 218 | + "pygments_lexer": "ipython3", |
| 219 | + "version": "3.8.3" |
| 220 | + } |
| 221 | + }, |
| 222 | + "nbformat": 4, |
| 223 | + "nbformat_minor": 4 |
| 224 | +} |
0 commit comments