{ "metadata": { "name": "", "signature": "sha256:5233b8440acc7b2d62016ba76634e04bd09fd81a7f83bcd9847657fe204b0cea" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 23: Library Support for Numbers and Character Data" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example tclassify.c, Page 613" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import string\n", "print \" alnum alpha digit graph lower print punct space upper xdigit\"\n", "p=\"azAZ0 !\\t\"\n", "p1=list(p)\n", "for i in p1:\n", " listt=[\" \"]*10\n", " print i,\n", " if(i.isalnum()):\n", " listt[0]=\" X\"\n", " if(i.isalpha()):\n", " listt[1]=\" X\"\n", " if(i.isdigit()):\n", " listt[2]=\" X\"\n", " if(i.isspace()!=True):\n", " listt[3]=\" X\"\n", " if(i.islower()):\n", " listt[4]=\" X\"\n", " if(i in string.printable):\n", " listt[5]=\" X\"\n", " if(i in string.punctuation):\n", " listt[6]=\" X\"\n", " if(i.isspace()):\n", " listt[7]=\" X\"\n", " if(i.isupper()):\n", " listt[8]=\" X\"\n", " if(i in string.hexdigits):\n", " listt[9]=\" X\"\n", " print listt" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " alnum alpha digit graph lower print punct space upper xdigit\n", "a [' X', ' X', ' ', ' X', ' X', ' X', ' ', ' ', ' ', ' X']\n", "z [' X', ' X', ' ', ' X', ' X', ' X', ' ', ' ', ' ', ' ']\n", "A [' X', ' X', ' ', ' X', ' ', ' X', ' ', ' ', ' X', ' X']\n", "Z [' X', ' X', ' ', ' X', ' ', ' X', ' ', ' ', ' X', ' ']\n", "0 [' X', ' ', ' X', ' X', ' ', ' X', ' ', ' ', ' ', ' X']\n", " [' ', ' ', ' ', ' ', ' ', ' X', ' ', ' X', ' ', ' ']\n", "! [' ', ' ', ' ', ' X', ' ', ' X', ' X', ' ', ' ', ' ']\n", "\t[' ', ' ', ' ', ' ', ' ', ' X', ' ', ' X', ' ', ' ']\n" ] } ], "prompt_number": 38 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example tcasemap.c, Page 615" ] }, { "cell_type": "code", "collapsed": false, "input": [ "p=\"aA0!\"\n", "pl=list(p)\n", "for i in pl:\n", " print \"tolower('%c') is '%c'; \"% (i,i.lower()),\n", " print \"toupper('%c') is '%c'; \"% (i,i.upper())" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "tolower('a') is 'a'; toupper('a') is 'A'; \n", "tolower('A') is 'a'; toupper('A') is 'A'; \n", "tolower('0') is '0'; toupper('0') is '0'; \n", "tolower('!') is '!'; toupper('!') is '!'; \n" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }