Advertisement
Guest User

Untitled

a guest
Jul 7th, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.82 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "#Accessing Values in Strings\n",
  8. "\n",
  9. "Python does not support a character type; these are treated as strings of length one, thus also considered a substring.\n",
  10. "\n",
  11. "To access substrings, use the square brackets for slicing along with the index or indices to obtain your substring. For example −"
  12. ]
  13. },
  14. {
  15. "cell_type": "code",
  16. "execution_count": 2,
  17. "metadata": {
  18. "collapsed": false
  19. },
  20. "outputs": [
  21. {
  22. "name": "stdout",
  23. "output_type": "stream",
  24. "text": [
  25. "('var1[0]: ', 'H')\n",
  26. "('var2[1:5]: ', 'ytho')\n"
  27. ]
  28. }
  29. ],
  30. "source": [
  31. "var1 = 'Hello World!'\n",
  32. "var2 = \"Python Programming\"\n",
  33. "\n",
  34. "print(\"var1[0]: \", var1[0])\n",
  35. "print(\"var2[1:5]: \", var2[1:5])"
  36. ]
  37. },
  38. {
  39. "cell_type": "markdown",
  40. "metadata": {},
  41. "source": [
  42. "#Updating Strings\n",
  43. "\n",
  44. "You can \"update\" an existing string by (re)assigning a variable to another string. The new value can be related to its previous value or to a completely different string altogether. For example −"
  45. ]
  46. },
  47. {
  48. "cell_type": "code",
  49. "execution_count": 6,
  50. "metadata": {
  51. "collapsed": false
  52. },
  53. "outputs": [
  54. {
  55. "name": "stdout",
  56. "output_type": "stream",
  57. "text": [
  58. "Updated String :- Hello Python\n"
  59. ]
  60. }
  61. ],
  62. "source": [
  63. "var1 = 'Hello World! '\n",
  64. "\n",
  65. "print(\"Updated String :- \" + var1[:6] + 'Python')"
  66. ]
  67. },
  68. {
  69. "cell_type": "markdown",
  70. "metadata": {},
  71. "source": [
  72. "#String Special Operators"
  73. ]
  74. },
  75. {
  76. "cell_type": "code",
  77. "execution_count": 7,
  78. "metadata": {
  79. "collapsed": false
  80. },
  81. "outputs": [
  82. {
  83. "data": {
  84. "text/plain": [
  85. "'Hello World! Hello World! Hello World! Hello World! '"
  86. ]
  87. },
  88. "execution_count": 7,
  89. "metadata": {},
  90. "output_type": "execute_result"
  91. }
  92. ],
  93. "source": [
  94. "var1 * 4"
  95. ]
  96. },
  97. {
  98. "cell_type": "code",
  99. "execution_count": 9,
  100. "metadata": {
  101. "collapsed": false
  102. },
  103. "outputs": [
  104. {
  105. "data": {
  106. "text/plain": [
  107. "False"
  108. ]
  109. },
  110. "execution_count": 9,
  111. "metadata": {},
  112. "output_type": "execute_result"
  113. }
  114. ],
  115. "source": [
  116. "'Python' in var1"
  117. ]
  118. },
  119. {
  120. "cell_type": "code",
  121. "execution_count": 10,
  122. "metadata": {
  123. "collapsed": false
  124. },
  125. "outputs": [
  126. {
  127. "data": {
  128. "text/plain": [
  129. "True"
  130. ]
  131. },
  132. "execution_count": 10,
  133. "metadata": {},
  134. "output_type": "execute_result"
  135. }
  136. ],
  137. "source": [
  138. "'World' in var1"
  139. ]
  140. },
  141. {
  142. "cell_type": "code",
  143. "execution_count": 11,
  144. "metadata": {
  145. "collapsed": false
  146. },
  147. "outputs": [
  148. {
  149. "data": {
  150. "text/plain": [
  151. "True"
  152. ]
  153. },
  154. "execution_count": 11,
  155. "metadata": {},
  156. "output_type": "execute_result"
  157. }
  158. ],
  159. "source": [
  160. "'world' not in var1"
  161. ]
  162. },
  163. {
  164. "cell_type": "markdown",
  165. "metadata": {},
  166. "source": [
  167. "#String Formatting Operator"
  168. ]
  169. },
  170. {
  171. "cell_type": "code",
  172. "execution_count": 14,
  173. "metadata": {
  174. "collapsed": false
  175. },
  176. "outputs": [
  177. {
  178. "name": "stdout",
  179. "output_type": "stream",
  180. "text": [
  181. "My name is Zara and weight is 21 kg!\n"
  182. ]
  183. }
  184. ],
  185. "source": [
  186. "print(\"My name is %s and weight is %d kg!\" % ('Zara', 21)) "
  187. ]
  188. },
  189. {
  190. "cell_type": "raw",
  191. "metadata": {},
  192. "source": [
  193. "Format Symbol\tConversion\n",
  194. "%c \tcharacter\n",
  195. "%s \tstring conversion via str() prior to formatting\n",
  196. "%i \tsigned decimal integer\n",
  197. "%d \tsigned decimal integer\n",
  198. "%u \tunsigned decimal integer\n",
  199. "%o \toctal integer\n",
  200. "%x \thexadecimal integer (lowercase letters)\n",
  201. "%X \thexadecimal integer (UPPERcase letters)\n",
  202. "%e \texponential notation (with lowercase 'e')\n",
  203. "%E \texponential notation (with UPPERcase 'E')\n",
  204. "%f \tfloating point real number\n",
  205. "%g \tthe shorter of %f and %e\n",
  206. "%G \tthe shorter of %f and %E"
  207. ]
  208. },
  209. {
  210. "cell_type": "markdown",
  211. "metadata": {},
  212. "source": [
  213. "#Triple Quotes\n",
  214. "\n",
  215. "Python's triple quotes comes to the rescue by allowing strings to span multiple lines, including verbatim NEWLINEs, TABs, and any other special characters."
  216. ]
  217. },
  218. {
  219. "cell_type": "code",
  220. "execution_count": 13,
  221. "metadata": {
  222. "collapsed": false
  223. },
  224. "outputs": [
  225. {
  226. "name": "stdout",
  227. "output_type": "stream",
  228. "text": [
  229. "this is a long string that is made up of\n",
  230. "several lines and non-printable characters such as\n",
  231. "TAB ( \t ) and they will show up that way when displayed.\n",
  232. "NEWLINEs within the string, whether explicitly given like\n",
  233. "this within the brackets [ \n",
  234. " ], or just a NEWLINE within\n",
  235. "the variable assignment will also show up.\n",
  236. "\n"
  237. ]
  238. }
  239. ],
  240. "source": [
  241. "para_str = \"\"\"this is a long string that is made up of\n",
  242. "several lines and non-printable characters such as\n",
  243. "TAB ( \\t ) and they will show up that way when displayed.\n",
  244. "NEWLINEs within the string, whether explicitly given like\n",
  245. "this within the brackets [ \\n ], or just a NEWLINE within\n",
  246. "the variable assignment will also show up.\n",
  247. "\"\"\"\n",
  248. "print(para_str);\n",
  249. "\n"
  250. ]
  251. },
  252. {
  253. "cell_type": "markdown",
  254. "metadata": {},
  255. "source": [
  256. "#Built-in String Methods"
  257. ]
  258. },
  259. {
  260. "cell_type": "code",
  261. "execution_count": 15,
  262. "metadata": {
  263. "collapsed": false
  264. },
  265. "outputs": [
  266. {
  267. "name": "stdout",
  268. "output_type": "stream",
  269. "text": [
  270. "3\n",
  271. "2\n"
  272. ]
  273. }
  274. ],
  275. "source": [
  276. "value = \"finnegans wake\"\n",
  277. "\n",
  278. "# Count this substring.\n",
  279. "print(value.count(\"n\"))\n",
  280. "\n",
  281. "# Count substring in indexes 0 to 6.\n",
  282. "print(value.count(\"n\", 0, 6))"
  283. ]
  284. },
  285. {
  286. "cell_type": "code",
  287. "execution_count": 17,
  288. "metadata": {
  289. "collapsed": false
  290. },
  291. "outputs": [
  292. {
  293. "name": "stdout",
  294. "output_type": "stream",
  295. "text": [
  296. "True\n",
  297. "True\n",
  298. "False\n"
  299. ]
  300. }
  301. ],
  302. "source": [
  303. "s = \"voorheesville\"\n",
  304. "\n",
  305. "print(s.startswith(\"voo\"))\n",
  306. " \n",
  307. "\n",
  308. "print(s.endswith(\"ville\"))\n",
  309. "\n",
  310. "\n",
  311. "print(s.startswith(\"stuy\"))\n",
  312. " # Not reached.\n",
  313. " "
  314. ]
  315. },
  316. {
  317. "cell_type": "code",
  318. "execution_count": 18,
  319. "metadata": {
  320. "collapsed": false
  321. },
  322. "outputs": [
  323. {
  324. "name": "stdout",
  325. "output_type": "stream",
  326. "text": [
  327. "Paris.....\n",
  328. " Paris\n"
  329. ]
  330. }
  331. ],
  332. "source": [
  333. "s = \"Paris\"\n",
  334. "\n",
  335. "# Justify to left, add periods.\n",
  336. "print(s.ljust(10, \".\"))\n",
  337. "\n",
  338. "# Justify to right.\n",
  339. "print(s.rjust(10))\n"
  340. ]
  341. },
  342. {
  343. "cell_type": "code",
  344. "execution_count": 19,
  345. "metadata": {
  346. "collapsed": false
  347. },
  348. "outputs": [
  349. {
  350. "name": "stdout",
  351. "output_type": "stream",
  352. "text": [
  353. "aayz\n",
  354. "xabc\n"
  355. ]
  356. }
  357. ],
  358. "source": [
  359. "value = \"aabc\"\n",
  360. "\n",
  361. "# Replace a substring with another.\n",
  362. "result = value.replace(\"bc\", \"yz\")\n",
  363. "print(result)\n",
  364. "\n",
  365. "# Replace the first occurrence with a substring.\n",
  366. "result = value.replace(\"a\", \"x\", 1)\n",
  367. "print(result)"
  368. ]
  369. },
  370. {
  371. "cell_type": "code",
  372. "execution_count": 20,
  373. "metadata": {
  374. "collapsed": false
  375. },
  376. "outputs": [
  377. {
  378. "name": "stdout",
  379. "output_type": "stream",
  380. "text": [
  381. "1,000\n",
  382. ":::::::cat\n"
  383. ]
  384. }
  385. ],
  386. "source": [
  387. "# Format this number with a comma.\n",
  388. "result = format(1000, \",\")\n",
  389. "print(result)\n",
  390. "\n",
  391. "# Align to the right of 10 chars, filling with \":\" and as a string.\n",
  392. "result = format(\"cat\", \":>10s\")\n",
  393. "print(result)"
  394. ]
  395. },
  396. {
  397. "cell_type": "code",
  398. "execution_count": 22,
  399. "metadata": {
  400. "collapsed": false
  401. },
  402. "outputs": [
  403. {
  404. "data": {
  405. "text/plain": [
  406. "['Hello', 'world!']"
  407. ]
  408. },
  409. "execution_count": 22,
  410. "metadata": {},
  411. "output_type": "execute_result"
  412. }
  413. ],
  414. "source": [
  415. "astring = \"Hello world!\"\n",
  416. "afewwords = astring.split(\" \")\n",
  417. "afewwords"
  418. ]
  419. },
  420. {
  421. "cell_type": "markdown",
  422. "metadata": {},
  423. "source": [
  424. "#Exercise\n",
  425. "Try to fix the code to print out the correct information by changing the string."
  426. ]
  427. },
  428. {
  429. "cell_type": "code",
  430. "execution_count": 23,
  431. "metadata": {
  432. "collapsed": false
  433. },
  434. "outputs": [
  435. {
  436. "name": "stdout",
  437. "output_type": "stream",
  438. "text": [
  439. "Length of s = 38\n",
  440. "The first occurrence of the letter a = 13\n",
  441. "a occurs 1 times\n"
  442. ]
  443. }
  444. ],
  445. "source": [
  446. "s = \"Hey there! what should this string be?\"\n",
  447. "\n",
  448. "# Length should be 20\n",
  449. "print \"Length of s = %d\" % len(s)\n",
  450. "\n",
  451. "# First occurrence of \"a\" should be at index 8\n",
  452. "print \"The first occurrence of the letter a = %d\" % s.index(\"a\")\n",
  453. "\n",
  454. "# Number of a's should be 2\n",
  455. "print \"a occurs %d times\" % s.count(\"a\")\n",
  456. "\n"
  457. ]
  458. },
  459. {
  460. "cell_type": "code",
  461. "execution_count": null,
  462. "metadata": {
  463. "collapsed": true
  464. },
  465. "outputs": [],
  466. "source": []
  467. }
  468. ],
  469. "metadata": {
  470. "kernelspec": {
  471. "display_name": "Python 2",
  472. "language": "python",
  473. "name": "python2"
  474. },
  475. "language_info": {
  476. "codemirror_mode": {
  477. "name": "ipython",
  478. "version": 2
  479. },
  480. "file_extension": ".py",
  481. "mimetype": "text/x-python",
  482. "name": "python",
  483. "nbconvert_exporter": "python",
  484. "pygments_lexer": "ipython2",
  485. "version": "2.7.6"
  486. }
  487. },
  488. "nbformat": 4,
  489. "nbformat_minor": 0
  490. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement