Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "#Accessing Values in Strings\n",
- "\n",
- "Python does not support a character type; these are treated as strings of length one, thus also considered a substring.\n",
- "\n",
- "To access substrings, use the square brackets for slicing along with the index or indices to obtain your substring. For example −"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "('var1[0]: ', 'H')\n",
- "('var2[1:5]: ', 'ytho')\n"
- ]
- }
- ],
- "source": [
- "var1 = 'Hello World!'\n",
- "var2 = \"Python Programming\"\n",
- "\n",
- "print(\"var1[0]: \", var1[0])\n",
- "print(\"var2[1:5]: \", var2[1:5])"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "#Updating Strings\n",
- "\n",
- "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 −"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Updated String :- Hello Python\n"
- ]
- }
- ],
- "source": [
- "var1 = 'Hello World! '\n",
- "\n",
- "print(\"Updated String :- \" + var1[:6] + 'Python')"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "#String Special Operators"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'Hello World! Hello World! Hello World! Hello World! '"
- ]
- },
- "execution_count": 7,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "var1 * 4"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 9,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "False"
- ]
- },
- "execution_count": 9,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "'Python' in var1"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 10,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "True"
- ]
- },
- "execution_count": 10,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "'World' in var1"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 11,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "True"
- ]
- },
- "execution_count": 11,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "'world' not in var1"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "#String Formatting Operator"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 14,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "My name is Zara and weight is 21 kg!\n"
- ]
- }
- ],
- "source": [
- "print(\"My name is %s and weight is %d kg!\" % ('Zara', 21)) "
- ]
- },
- {
- "cell_type": "raw",
- "metadata": {},
- "source": [
- "Format Symbol\tConversion\n",
- "%c \tcharacter\n",
- "%s \tstring conversion via str() prior to formatting\n",
- "%i \tsigned decimal integer\n",
- "%d \tsigned decimal integer\n",
- "%u \tunsigned decimal integer\n",
- "%o \toctal integer\n",
- "%x \thexadecimal integer (lowercase letters)\n",
- "%X \thexadecimal integer (UPPERcase letters)\n",
- "%e \texponential notation (with lowercase 'e')\n",
- "%E \texponential notation (with UPPERcase 'E')\n",
- "%f \tfloating point real number\n",
- "%g \tthe shorter of %f and %e\n",
- "%G \tthe shorter of %f and %E"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "#Triple Quotes\n",
- "\n",
- "Python's triple quotes comes to the rescue by allowing strings to span multiple lines, including verbatim NEWLINEs, TABs, and any other special characters."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 13,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "this is a long string that is made up of\n",
- "several lines and non-printable characters such as\n",
- "TAB ( \t ) and they will show up that way when displayed.\n",
- "NEWLINEs within the string, whether explicitly given like\n",
- "this within the brackets [ \n",
- " ], or just a NEWLINE within\n",
- "the variable assignment will also show up.\n",
- "\n"
- ]
- }
- ],
- "source": [
- "para_str = \"\"\"this is a long string that is made up of\n",
- "several lines and non-printable characters such as\n",
- "TAB ( \\t ) and they will show up that way when displayed.\n",
- "NEWLINEs within the string, whether explicitly given like\n",
- "this within the brackets [ \\n ], or just a NEWLINE within\n",
- "the variable assignment will also show up.\n",
- "\"\"\"\n",
- "print(para_str);\n",
- "\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "#Built-in String Methods"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 15,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "3\n",
- "2\n"
- ]
- }
- ],
- "source": [
- "value = \"finnegans wake\"\n",
- "\n",
- "# Count this substring.\n",
- "print(value.count(\"n\"))\n",
- "\n",
- "# Count substring in indexes 0 to 6.\n",
- "print(value.count(\"n\", 0, 6))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 17,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "True\n",
- "True\n",
- "False\n"
- ]
- }
- ],
- "source": [
- "s = \"voorheesville\"\n",
- "\n",
- "print(s.startswith(\"voo\"))\n",
- " \n",
- "\n",
- "print(s.endswith(\"ville\"))\n",
- "\n",
- "\n",
- "print(s.startswith(\"stuy\"))\n",
- " # Not reached.\n",
- " "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 18,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Paris.....\n",
- " Paris\n"
- ]
- }
- ],
- "source": [
- "s = \"Paris\"\n",
- "\n",
- "# Justify to left, add periods.\n",
- "print(s.ljust(10, \".\"))\n",
- "\n",
- "# Justify to right.\n",
- "print(s.rjust(10))\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 19,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "aayz\n",
- "xabc\n"
- ]
- }
- ],
- "source": [
- "value = \"aabc\"\n",
- "\n",
- "# Replace a substring with another.\n",
- "result = value.replace(\"bc\", \"yz\")\n",
- "print(result)\n",
- "\n",
- "# Replace the first occurrence with a substring.\n",
- "result = value.replace(\"a\", \"x\", 1)\n",
- "print(result)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 20,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "1,000\n",
- ":::::::cat\n"
- ]
- }
- ],
- "source": [
- "# Format this number with a comma.\n",
- "result = format(1000, \",\")\n",
- "print(result)\n",
- "\n",
- "# Align to the right of 10 chars, filling with \":\" and as a string.\n",
- "result = format(\"cat\", \":>10s\")\n",
- "print(result)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 22,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "['Hello', 'world!']"
- ]
- },
- "execution_count": 22,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "astring = \"Hello world!\"\n",
- "afewwords = astring.split(\" \")\n",
- "afewwords"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "#Exercise\n",
- "Try to fix the code to print out the correct information by changing the string."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 23,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Length of s = 38\n",
- "The first occurrence of the letter a = 13\n",
- "a occurs 1 times\n"
- ]
- }
- ],
- "source": [
- "s = \"Hey there! what should this string be?\"\n",
- "\n",
- "# Length should be 20\n",
- "print \"Length of s = %d\" % len(s)\n",
- "\n",
- "# First occurrence of \"a\" should be at index 8\n",
- "print \"The first occurrence of the letter a = %d\" % s.index(\"a\")\n",
- "\n",
- "# Number of a's should be 2\n",
- "print \"a occurs %d times\" % s.count(\"a\")\n",
- "\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": []
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 2",
- "language": "python",
- "name": "python2"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 2
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython2",
- "version": "2.7.6"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 0
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement