human
I am working on the file "PythonQuestions.ipynb".
The first task is:
String spaces --> %20s using RE sub()
Can you write Python code for it?
assistant
#Here is the Python code you requested.

import re
import unittest

def space20(strg):
    '''Replace " " with "%20"'''
    space_to_20 = re.compile(r' ')
    return space_to_20.sub(r'%20', strg)

class Space20Test(unittest.TestCase):
    def test_zero_len(self):
        self.failUnlessEqual('', space20(''))
    def test_match_nothing(self):
        self.failUnlessEqual('abc;def', space20('abc;def'))
    def test_match_at_start(self):
        self.failUnlessEqual('%20abc', space20(' abc'))
    def test_match_in_middle(self):
        self.failUnlessEqual('abc%20def', space20('abc def'))
    def test_match_at_end(self):
        self.failUnlessEqual('abc%20', space20('abc '))
    def test_match_complex(self):
        self.failUnlessEqual('%20%20abc%20%20def%20%20',
                             space20('  abc  def  '))
            
squares_suite = unittest.TestLoader().loadTestsFromTestCase(Space20Test)
unittest.TextTestRunner().run(squares_suite)