Python Koans: About Strings

This is a proof-of-concept for writing Python koans in an IPython notebook, based on the original about strings koan.

Varieties of quote characters

In [ ]:
# replace the __ with an equality operator (like == or !=)
string = "Hello, world."
assert type(string) __ str
In [ ]:
# replace the __ with an equality operator (like == or !=)
string1 = 'Goodbye, world.'
string2 = "Goodbye, world."
assert string1 __ string2
In [ ]:
# fix the syntax error
print "Goodbye, world.'
In [ ]:
# fix the syntax error
print 'Goodbye, world."
In [ ]:
# replace the __ with an equality operator (like == or !=)
string1 = """Howdy, world!"""
string2 = 'Howdy, world!'
assert string1 __ string2
In [ ]:
# fix the syntax error
print """Howdy, world!"""
print ""Howdy, world!""
print "Howdy, world!"
In [ ]:
# replace the __ with an equality operator (like == or !=)
string = '''Bonjour tout le monde!'''
string = "Bonjour tout le monde!"
assert string1 __ string2
In [ ]:
# fix the syntax error
print '''Bonjour tout le monde!'''
print ''Bonjour tout le monde!''
print 'Bonjour tout le monde!'
In [ ]:
# fix the syntax error
print '''Bonjour tout le monde!"""
In [ ]:
# replace the __ with an equality operator (like == or !=)
string = r"Konnichi wa, world!"
string = "Konnichi wa, world!"
assert string1 __ string2

Escaping quote characters

In [ ]:
# replace the __ with an equality operator (like == or !=)
a = "He said, \"Don't\""
b = 'He said, "Don\'t"'
assert a __ b
In [ ]:
# replace the __ with an equality operator (like == or !=)
a = "He said, \"Don't\""
b = 'He said, \'Don"t\''
assert a __ b
In [ ]:
# replace the __ with an equality operator (like == or !=)
a = "He said, 'Don\"t'"
b = 'He said, "Don\'t"'
assert a __ b
In [ ]:
# fix the syntax error
print "He said, "Go Away.""
In [ ]:
# fix the syntax error
print 'He said, 'Go Away.''
In [ ]:
# fix the syntax error
print 'He said, "Don't"'
In [ ]:
# replace the __ with an equality operator (like == or !=)
a = "Hello \"world\"."
b = """Hello "world"."""
assert a __ b
In [ ]:
# fix the syntax error (hint: escape one of the quotes)
print """Hello "world""""

Escape characters and newlines

In [ ]:
# replace __ with the length of the string
string = "\n"
assert '\n' == string
assert """\n""" == string
assert __ == len(string)
In [ ]:
# replace __ with the length of the string
string = "\t"
assert '\t' == string
assert """\t""" == string
assert __ == len(string)
In [ ]:
# replace __ with the length of the string
string = "\m"
assert '\m' == string
assert """\m""" == string
assert __ == len(string)
In [ ]:
# replace the __ with an equality operator (like == or !=)
string1 = "It was the best of times,\n\
It was the worst of times."
string2 = """It was the best of times,
It was the worst of times."""
assert string1 __ string2
In [ ]:
# replace the __ with an equality operator (like == or !=)
string1 = "It was the best of times,\n\
It was the worst of times."
string2 = "It was the best of times, It was the worst of times."
assert string1 __ string2
In [ ]:
# fix string1 or string2 so the test passes
string1 = "It was the best of times,\
It was the worst of times."
string2 = "It was the best of times, It was the worst of times."
assert string1 == string2
In [ ]:
# replace the __ with an equality operator (like == or !=)
string1 = "It was the best of times,\n\
It was the worst of times."
string2 = """It was the best of times,\n\
It was the worst of times."""
assert string1 __ string2
In [ ]:
# replace the __ with an equality operator (like == or !=)
string1 = "\nHowdy,\nworld!\n"
string2 = """
Howdy,
world!
"""
assert string1 __ string2
In [ ]:
# replace the __ with an equality operator (like == or !=)
string1 = "\n Howdy,\n world!\n"
string2 = """
Howdy,
world!
"""
assert string1 __ string2
In [ ]:
# replace the __ with an equality operator (like == or !=)
string1 = "\n\
Howdy,\n\
world!\n\
"
string2 = """
Howdy,
world!
"""
string1 __ string2

Concatenating strings

In [ ]:
# replace __ with a string
string = "Hello, " + "world"
assert __ == string
In [ ]:
# replace __ with a string
string = "Hallo" ", " "Welt"
assert __ == string
In [ ]:
# replace __ with strings
hi = "Hello, "
there = "world"
string = hi + there
assert __ == hi
assert __ == there
assert __ == string
In [ ]:
# replace __ with strings
hi = "Hello, "
there = "world"
hi += there
assert __ == hi
assert __ == there
In [ ]:
# replace __ with strings
original = "Hello, "
hi = original
there = "world"
hi += there
assert __ == original
assert __ == hi
assert __ == there