Skip to content

Instantly share code, notes, and snippets.

@fakechris
Created April 7, 2014 01:54
Show Gist options
  • Save fakechris/10013774 to your computer and use it in GitHub Desktop.
Save fakechris/10013774 to your computer and use it in GitHub Desktop.
Python String Tutorials
{
"metadata": {
"name": "Python String Tutorials"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": "dir(__builtin__)",
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": "help(repr)",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Help on built-in function repr in module __builtin__:\n\nrepr(...)\n repr(object) -> string\n \n Return the canonical string representation of the object.\n For most object types, eval(repr(object)) == object.\n\n"
}
],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": "repr(\"Hello world\\n\")",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 27,
"text": "\"'Hello world\\\\n'\""
}
],
"prompt_number": 27
},
{
"cell_type": "code",
"collapsed": false,
"input": "print \"hello world\"",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "hello world\n"
}
],
"prompt_number": 28
},
{
"cell_type": "code",
"collapsed": false,
"input": "print(\"hello world\")",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "hello world\n"
}
],
"prompt_number": 32
},
{
"cell_type": "code",
"collapsed": false,
"input": "\"\"\" This is a long text\ncross the line\n\"\"\"",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 83,
"text": "' This is a long text\\ncross the line\\n'"
}
],
"prompt_number": 83
},
{
"cell_type": "code",
"collapsed": false,
"input": "\" This is a long text\\\ncross the line\\\n\"",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 84,
"text": "' This is a long textcross the line'"
}
],
"prompt_number": 84
},
{
"cell_type": "code",
"collapsed": false,
"input": "\"this is string escape demo \\\" \\\\n \\n\"",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 85,
"text": "'this is string escape demo \" \\\\n \\n'"
}
],
"prompt_number": 85
},
{
"cell_type": "code",
"collapsed": false,
"input": "'this is string escape demo \" \\\\n \\n'",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 86,
"text": "'this is string escape demo \" \\\\n \\n'"
}
],
"prompt_number": 86
},
{
"cell_type": "code",
"collapsed": false,
"input": "r'this is string escape demo \" \\n'",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 87,
"text": "'this is string escape demo \" \\\\n'"
}
],
"prompt_number": 87
},
{
"cell_type": "code",
"collapsed": false,
"input": "chr(46)",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 35,
"text": "'.'"
}
],
"prompt_number": 35
},
{
"cell_type": "code",
"collapsed": false,
"input": "ord('.')",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 36,
"text": "46"
}
],
"prompt_number": 36
},
{
"cell_type": "code",
"collapsed": false,
"input": "s = \"test\"",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": "s",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 8,
"text": "'test'"
}
],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": "type(s)",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 9,
"text": "str"
}
],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": "type(s) == 'str'",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 10,
"text": "False"
}
],
"prompt_number": 10
},
{
"cell_type": "code",
"collapsed": false,
"input": "type(s) == str",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 11,
"text": "True"
}
],
"prompt_number": 11
},
{
"cell_type": "code",
"collapsed": false,
"input": "dir(s)",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 12,
"text": "['__add__',\n '__class__',\n '__contains__',\n '__delattr__',\n '__doc__',\n '__eq__',\n '__format__',\n '__ge__',\n '__getattribute__',\n '__getitem__',\n '__getnewargs__',\n '__getslice__',\n '__gt__',\n '__hash__',\n '__init__',\n '__le__',\n '__len__',\n '__lt__',\n '__mod__',\n '__mul__',\n '__ne__',\n '__new__',\n '__reduce__',\n '__reduce_ex__',\n '__repr__',\n '__rmod__',\n '__rmul__',\n '__setattr__',\n '__sizeof__',\n '__str__',\n '__subclasshook__',\n '_formatter_field_name_split',\n '_formatter_parser',\n 'capitalize',\n 'center',\n 'count',\n 'decode',\n 'encode',\n 'endswith',\n 'expandtabs',\n 'find',\n 'format',\n 'index',\n 'isalnum',\n 'isalpha',\n 'isdigit',\n 'islower',\n 'isspace',\n 'istitle',\n 'isupper',\n 'join',\n 'ljust',\n 'lower',\n 'lstrip',\n 'partition',\n 'replace',\n 'rfind',\n 'rindex',\n 'rjust',\n 'rpartition',\n 'rsplit',\n 'rstrip',\n 'split',\n 'splitlines',\n 'startswith',\n 'strip',\n 'swapcase',\n 'title',\n 'translate',\n 'upper',\n 'zfill']"
}
],
"prompt_number": 12
},
{
"cell_type": "code",
"collapsed": false,
"input": "s.capitalize()",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 15,
"text": "'Test'"
}
],
"prompt_number": 15
},
{
"cell_type": "code",
"collapsed": false,
"input": "\"192.168.18.1\".split(\".\")",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 17,
"text": "['192', '168', '18', '1']"
}
],
"prompt_number": 17
},
{
"cell_type": "code",
"collapsed": false,
"input": "s.encode(\"base64\")",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 20,
"text": "'dGVzdA==\\n'"
}
],
"prompt_number": 20
},
{
"cell_type": "code",
"collapsed": false,
"input": "u\"\u6211\u662f\u8c01\".encode(\"utf8\")",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 21,
"text": "'\\xe6\\x88\\x91\\xe6\\x98\\xaf\\xe8\\xb0\\x81'"
}
],
"prompt_number": 21
},
{
"cell_type": "code",
"collapsed": false,
"input": "u\"\u6211\u662f\u8c01\".encode(\"gbk\")",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 22,
"text": "'\\xce\\xd2\\xca\\xc7\\xcb\\xad'"
}
],
"prompt_number": 22
},
{
"cell_type": "code",
"collapsed": false,
"input": "'\\xce\\xd2\\xca\\xc7\\xcb\\xad'.decode(\"gbk\")",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 23,
"text": "u'\\u6211\\u662f\\u8c01'"
}
],
"prompt_number": 23
},
{
"cell_type": "code",
"collapsed": false,
"input": "type(u'\\u6211\\u662f\\u8c01')",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 24,
"text": "unicode"
}
],
"prompt_number": 24
},
{
"cell_type": "code",
"collapsed": false,
"input": "'\\xcd\\x01'.encode('hex')",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 37,
"text": "'cd01'"
}
],
"prompt_number": 37
},
{
"cell_type": "code",
"collapsed": false,
"input": "\"tobezipped\".encode('zip')",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 42,
"text": "'x\\x9c+\\xc9OJ\\xad\\xca,(HM\\x01\\x00\\x17s\\x047'"
}
],
"prompt_number": 42
},
{
"cell_type": "code",
"collapsed": false,
"input": "import cgi",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 47
},
{
"cell_type": "code",
"collapsed": false,
"input": "cgi.escape(\"http://a.com/?test=12&o=2\")",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 48,
"text": "'http://a.com/?test=12&o=2'"
}
],
"prompt_number": 48
},
{
"cell_type": "code",
"collapsed": false,
"input": "cgi.parse_qs('test=12&o=2')",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 57,
"text": "{'o': ['2'], 'test': ['12']}"
}
],
"prompt_number": 57
},
{
"cell_type": "code",
"collapsed": false,
"input": "import urllib2",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 58
},
{
"cell_type": "code",
"collapsed": false,
"input": "urllib2.urlparse.urlparse(\"http://a.com/?test=12&o=2\")",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 59,
"text": "ParseResult(scheme='http', netloc='a.com', path='/', params='', query='test=12&o=2', fragment='')"
}
],
"prompt_number": 59
},
{
"cell_type": "code",
"collapsed": false,
"input": "urllib2.quote(\"http://a.com/?test=12&o=2\")",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 61,
"text": "'http%3A//a.com/%3Ftest%3D12%26o%3D2'"
}
],
"prompt_number": 61
},
{
"cell_type": "code",
"collapsed": false,
"input": "urllib2.unquote(\"http%3A//a.com/%3Ftest%3D12%26o%3D2\")",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 63,
"text": "'http://a.com/?test=12&o=2'"
}
],
"prompt_number": 63
},
{
"cell_type": "code",
"collapsed": false,
"input": "import md5",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 64
},
{
"cell_type": "code",
"collapsed": false,
"input": "md5.md5(\"src\").hexdigest()",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 65,
"text": "'25d902c24283ab8cfbac54dfa101ad31'"
}
],
"prompt_number": 65
},
{
"cell_type": "code",
"collapsed": false,
"input": "for line in open(\"/etc/syslog.conf\", \"r\"): print line",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "# Note that flat file logs are now configured in /etc/asl.conf\n\n\n\ninstall.*\t\t\t\t\t\[email protected]:32376\n\n"
}
],
"prompt_number": 74
},
{
"cell_type": "code",
"collapsed": false,
"input": "dir(file)",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 79,
"text": "['__class__',\n '__delattr__',\n '__doc__',\n '__enter__',\n '__exit__',\n '__format__',\n '__getattribute__',\n '__hash__',\n '__init__',\n '__iter__',\n '__new__',\n '__reduce__',\n '__reduce_ex__',\n '__repr__',\n '__setattr__',\n '__sizeof__',\n '__str__',\n '__subclasshook__',\n 'close',\n 'closed',\n 'encoding',\n 'errors',\n 'fileno',\n 'flush',\n 'isatty',\n 'mode',\n 'name',\n 'newlines',\n 'next',\n 'read',\n 'readinto',\n 'readline',\n 'readlines',\n 'seek',\n 'softspace',\n 'tell',\n 'truncate',\n 'write',\n 'writelines',\n 'xreadlines']"
}
],
"prompt_number": 79
},
{
"cell_type": "code",
"collapsed": false,
"input": "from cStringIO import StringIO",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 75
},
{
"cell_type": "code",
"collapsed": false,
"input": "sio = StringIO(\"treat string as file\")",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 76
},
{
"cell_type": "code",
"collapsed": false,
"input": "sio.tell()",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 78,
"text": "0"
}
],
"prompt_number": 78
},
{
"cell_type": "code",
"collapsed": false,
"input": "import re",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 80
},
{
"cell_type": "code",
"collapsed": false,
"input": "r = re.compile(r'^\\b([^ ]+)\\b$', re.M|re.S)",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 97
},
{
"cell_type": "code",
"collapsed": false,
"input": "m = r.match(\"this is a test\")",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 98
},
{
"cell_type": "code",
"collapsed": false,
"input": "m == None",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 102,
"text": "True"
}
],
"prompt_number": 102
},
{
"cell_type": "code",
"collapsed": false,
"input": "r = re.compile(r'\\b([^ ]+)\\b', re.M|re.S); m = r.search('this is a test')",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 105
},
{
"cell_type": "code",
"collapsed": false,
"input": "m.group()",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 106,
"text": "'this'"
}
],
"prompt_number": 106
},
{
"cell_type": "code",
"collapsed": false,
"input": "r.subn(r'>>\\g<1><<', \"this is a test\")",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 95,
"text": "('>>this<< >>is<< >>a<< >>test<<', 4)"
}
],
"prompt_number": 95
},
{
"cell_type": "code",
"collapsed": false,
"input": "r.subn(lambda matchobj:\"*\"+matchobj.group(0)+\"*\", \"this is a test\")",
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 109,
"text": "('*this* *is* *a* *test*', 4)"
}
],
"prompt_number": 109
},
{
"cell_type": "code",
"collapsed": false,
"input": "import pprint",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 110
},
{
"cell_type": "code",
"collapsed": false,
"input": "pprint.pprint({\"a\":\"b\", \"c\":\"D\", 1:3})",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "{1: 3, 'a': 'b', 'c': 'D'}\n"
}
],
"prompt_number": 112
},
{
"cell_type": "code",
"collapsed": false,
"input": "",
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment