SHARE
TWEET

Untitled

a guest Oct 19th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {
  2.  "cells": [
  3.   {
  4.    "cell_type": "code",
  5.    "execution_count": 2,
  6.    "metadata": {},
  7.    "outputs": [
  8.     {
  9.      "data": {
  10.       "text/plain": [
  11.        "'MICHEAL JACKSON'"
  12.       ]
  13.      },
  14.      "execution_count": 2,
  15.      "metadata": {},
  16.      "output_type": "execute_result"
  17.     }
  18.    ],
  19.    "source": [
  20.     "#Because indexing starts at 0, it means the first index is on the index 0.\n",
  21.     "Name = \"MICHEAL JACKSON\"\n",
  22.     "Name"
  23.    ]
  24.   },
  25.   {
  26.    "cell_type": "code",
  27.    "execution_count": 3,
  28.    "metadata": {},
  29.    "outputs": [
  30.     {
  31.      "name": "stdout",
  32.      "output_type": "stream",
  33.      "text": [
  34.       "M\n"
  35.      ]
  36.     }
  37.    ],
  38.    "source": [
  39.     "print (Name[0])\n",
  40.     "\n",
  41.     "# Positive Indexing"
  42.    ]
  43.   },
  44.   {
  45.    "cell_type": "code",
  46.    "execution_count": 4,
  47.    "metadata": {},
  48.    "outputs": [
  49.     {
  50.      "name": "stdout",
  51.      "output_type": "stream",
  52.      "text": [
  53.       " \n"
  54.      ]
  55.     }
  56.    ],
  57.    "source": [
  58.     "print (Name[7])"
  59.    ]
  60.   },
  61.   {
  62.    "cell_type": "code",
  63.    "execution_count": 5,
  64.    "metadata": {},
  65.    "outputs": [
  66.     {
  67.      "name": "stdout",
  68.      "output_type": "stream",
  69.      "text": [
  70.       "N\n"
  71.      ]
  72.     }
  73.    ],
  74.    "source": [
  75.     "print (Name[-1])\n",
  76.     "\n",
  77.     "# Negative Indexing"
  78.    ]
  79.   },
  80.   {
  81.    "cell_type": "code",
  82.    "execution_count": 6,
  83.    "metadata": {},
  84.    "outputs": [
  85.     {
  86.      "name": "stdout",
  87.      "output_type": "stream",
  88.      "text": [
  89.       "M\n"
  90.      ]
  91.     }
  92.    ],
  93.    "source": [
  94.     "print (Name[-15])"
  95.    ]
  96.   },
  97.   {
  98.    "cell_type": "code",
  99.    "execution_count": 7,
  100.    "metadata": {},
  101.    "outputs": [
  102.     {
  103.      "data": {
  104.       "text/plain": [
  105.        "15"
  106.       ]
  107.      },
  108.      "execution_count": 7,
  109.      "metadata": {},
  110.      "output_type": "execute_result"
  111.     }
  112.    ],
  113.    "source": [
  114.     "# Find the length of string\n",
  115.     "\n",
  116.     "len(\"Michael Jackson\")"
  117.    ]
  118.   },
  119.   {
  120.    "cell_type": "code",
  121.    "execution_count": 8,
  122.    "metadata": {},
  123.    "outputs": [
  124.     {
  125.      "data": {
  126.       "text/plain": [
  127.        "'MICH'"
  128.       ]
  129.      },
  130.      "execution_count": 8,
  131.      "metadata": {},
  132.      "output_type": "execute_result"
  133.     }
  134.    ],
  135.    "source": [
  136.     "Name[0:4]\n",
  137.     "\n",
  138.     "# Slicing"
  139.    ]
  140.   },
  141.   {
  142.    "cell_type": "code",
  143.    "execution_count": 9,
  144.    "metadata": {},
  145.    "outputs": [
  146.     {
  147.      "data": {
  148.       "text/plain": [
  149.        "'JACK'"
  150.       ]
  151.      },
  152.      "execution_count": 9,
  153.      "metadata": {},
  154.      "output_type": "execute_result"
  155.     }
  156.    ],
  157.    "source": [
  158.     "Name [8:12]\n",
  159.     "\n",
  160.     "# When taking the slice, the first number means the index (start at 0), and the second number means the length from the index to the last element you want (start at 1)"
  161.    ]
  162.   },
  163.   {
  164.    "cell_type": "code",
  165.    "execution_count": 10,
  166.    "metadata": {},
  167.    "outputs": [
  168.     {
  169.      "data": {
  170.       "text/plain": [
  171.        "'MCELJCSN'"
  172.       ]
  173.      },
  174.      "execution_count": 10,
  175.      "metadata": {},
  176.      "output_type": "execute_result"
  177.     }
  178.    ],
  179.    "source": [
  180.     "Name[::2]\n",
  181.     "\n",
  182.     "# Get every second element. The elments on index 1, 3, 5 ..."
  183.    ]
  184.   },
  185.   {
  186.    "cell_type": "code",
  187.    "execution_count": 11,
  188.    "metadata": {},
  189.    "outputs": [
  190.     {
  191.      "data": {
  192.       "text/plain": [
  193.        "'MCE'"
  194.       ]
  195.      },
  196.      "execution_count": 11,
  197.      "metadata": {},
  198.      "output_type": "execute_result"
  199.     }
  200.    ],
  201.    "source": [
  202.     "# Get every second element in the range from index 0 to index 4\n",
  203.     "\n",
  204.     "Name[0:5:2]"
  205.    ]
  206.   },
  207.   {
  208.    "cell_type": "code",
  209.    "execution_count": 13,
  210.    "metadata": {},
  211.    "outputs": [
  212.     {
  213.      "data": {
  214.       "text/plain": [
  215.        "'MICHEAL JACKSON is the best'"
  216.       ]
  217.      },
  218.      "execution_count": 13,
  219.      "metadata": {},
  220.      "output_type": "execute_result"
  221.     }
  222.    ],
  223.    "source": [
  224.     "# Concatenate two strings\n",
  225.     "\n",
  226.     "Statement = Name + \" is the best\"\n",
  227.     "Statement"
  228.    ]
  229.   },
  230.   {
  231.    "cell_type": "code",
  232.    "execution_count": 15,
  233.    "metadata": {},
  234.    "outputs": [
  235.     {
  236.      "data": {
  237.       "text/plain": [
  238.        "' Michael Jackson Michael Jackson Michael Jackson'"
  239.       ]
  240.      },
  241.      "execution_count": 15,
  242.      "metadata": {},
  243.      "output_type": "execute_result"
  244.     }
  245.    ],
  246.    "source": [
  247.     "# Print the string for 3 times\n",
  248.     "\n",
  249.     "3 * \" Michael Jackson\""
  250.    ]
  251.   },
  252.   {
  253.    "cell_type": "code",
  254.    "execution_count": 16,
  255.    "metadata": {},
  256.    "outputs": [
  257.     {
  258.      "name": "stdout",
  259.      "output_type": "stream",
  260.      "text": [
  261.       " Michael Jackson \n",
  262.       " is the best\n"
  263.      ]
  264.     }
  265.    ],
  266.    "source": [
  267.     "# New line escape sequence\n",
  268.     "\n",
  269.     "print(\" Michael Jackson \\n is the best\" )"
  270.    ]
  271.   },
  272.   {
  273.    "cell_type": "code",
  274.    "execution_count": 17,
  275.    "metadata": {},
  276.    "outputs": [
  277.     {
  278.      "name": "stdout",
  279.      "output_type": "stream",
  280.      "text": [
  281.       " Michael Jackson \t is the best\n"
  282.      ]
  283.     }
  284.    ],
  285.    "source": [
  286.     "# Tab escape sequence\n",
  287.     "\n",
  288.     "print(\" Michael Jackson \\t is the best\" )"
  289.    ]
  290.   },
  291.   {
  292.    "cell_type": "code",
  293.    "execution_count": 18,
  294.    "metadata": {},
  295.    "outputs": [
  296.     {
  297.      "name": "stdout",
  298.      "output_type": "stream",
  299.      "text": [
  300.       " Michael Jackson \\ is the best\n",
  301.       " Michael Jackson \\ is the best\n"
  302.      ]
  303.     }
  304.    ],
  305.    "source": [
  306.     "# r will tell python that string will be display as raw string\n",
  307.     "\n",
  308.     "print(r\" Michael Jackson \\ is the best\" )\n",
  309.     "\n",
  310.     "# Include back slash in string\n",
  311.     "\n",
  312.     "print(\" Michael Jackson \\\\ is the best\" )"
  313.    ]
  314.   },
  315.   {
  316.    "cell_type": "code",
  317.    "execution_count": 19,
  318.    "metadata": {},
  319.    "outputs": [
  320.     {
  321.      "name": "stdout",
  322.      "output_type": "stream",
  323.      "text": [
  324.       "before upper: Thriller is the sixth studio album\n",
  325.       "After upper: THRILLER IS THE SIXTH STUDIO ALBUM\n",
  326.       "before lower: Thriller is the sixth studio album\n",
  327.       "After lower: thriller is the sixth studio album\n"
  328.      ]
  329.     }
  330.    ],
  331.    "source": [
  332.     "# Convert all the characters in string to upper case\n",
  333.     "\n",
  334.     "A = \"Thriller is the sixth studio album\"\n",
  335.     "print(\"before upper:\", A)\n",
  336.     "B = A.upper()\n",
  337.     "print(\"After upper:\", B)\n",
  338.     "\n",
  339.     "# Convert all the characters in string to lower case\n",
  340.     "\n",
  341.     "A = \"Thriller is the sixth studio album\"\n",
  342.     "print(\"before lower:\", A)\n",
  343.     "B = A.lower()\n",
  344.     "print(\"After lower:\", B)"
  345.    ]
  346.   },
  347.   {
  348.    "cell_type": "code",
  349.    "execution_count": 20,
  350.    "metadata": {},
  351.    "outputs": [
  352.     {
  353.      "data": {
  354.       "text/plain": [
  355.        "'Janet Jackson is the best'"
  356.       ]
  357.      },
  358.      "execution_count": 20,
  359.      "metadata": {},
  360.      "output_type": "execute_result"
  361.     }
  362.    ],
  363.    "source": [
  364.     "# Replace the old substring with the new target substring is the segment has been found in the string\n",
  365.     "\n",
  366.     "A = \"Michael Jackson is the best\"\n",
  367.     "B = A.replace('Michael', 'Janet')\n",
  368.     "B"
  369.    ]
  370.   },
  371.   {
  372.    "cell_type": "code",
  373.    "execution_count": 21,
  374.    "metadata": {},
  375.    "outputs": [
  376.     {
  377.      "data": {
  378.       "text/plain": [
  379.        "5"
  380.       ]
  381.      },
  382.      "execution_count": 21,
  383.      "metadata": {},
  384.      "output_type": "execute_result"
  385.     }
  386.    ],
  387.    "source": [
  388.     "# Find the substring in the string. Only the index of the first elment of substring in string will be the output\n",
  389.     "\n",
  390.     "Name = \"Michael Jackson\"\n",
  391.     "Name.find('el')"
  392.    ]
  393.   },
  394.   {
  395.    "cell_type": "code",
  396.    "execution_count": null,
  397.    "metadata": {},
  398.    "outputs": [],
  399.    "source": []
  400.   }
  401.  ],
  402.  "metadata": {
  403.   "kernelspec": {
  404.    "display_name": "Python",
  405.    "language": "python",
  406.    "name": "conda-env-python-py"
  407.   },
  408.   "language_info": {
  409.    "codemirror_mode": {
  410.     "name": "ipython",
  411.     "version": 3
  412.    },
  413.    "file_extension": ".py",
  414.    "mimetype": "text/x-python",
  415.    "name": "python",
  416.    "nbconvert_exporter": "python",
  417.    "pygments_lexer": "ipython3",
  418.    "version": "3.6.7"
  419.   }
  420.  },
  421.  "nbformat": 4,
  422.  "nbformat_minor": 4
  423. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top