Bonus Episode: Strings#
This notebook is based on materials kindly provided by the IN1900 team.
Python has many functions for working with strings. We will look at a few of them.
String Search#
We can search for a substring in a string:
text = "a short text"
print("short" in text)
print("an" in text)
True
False
Exercise: String Search #
Examine the code below. What results do you expect?
Now, run the code. Does the result match your expectation?
quote = "Strange women lying in ponds, distributing swords, is no basis for a system of government!"
queries = ["women", "men", "Camelot"]
for query in queries:
if query in quote:
print(query, "was found in quote")
else:
print(query, "was not found in quote")
women was found in quote
men was found in quote
Camelot was not found in quote
string.split()
#
split()
divides/splits a string into a list of parts. If we don’t specify an argument, it splits on whitespace, which includes spaces and ‘tab’-characters.
quote_list = quote.split()
print(quote_list)
['Strange', 'women', 'lying', 'in', 'ponds,', 'distributing', 'swords,', 'is', 'no', 'basis', 'for', 'a', 'system', 'of', 'government!']
You can also specify one or more characters to use as the separator:
quote_list = quote.split(",")
print(quote_list)
['Strange women lying in ponds', ' distributing swords', ' is no basis for a system of government!']
Joining Strings#
We can also go the other way, joining a list of strings to a single string. The syntax is different, we must first specify the string that is used to “glue” the parts together. This is the opposite of the separator above.
joined_list = ",".join(quote_list)
print(joined_list)
Strange women lying in ponds, distributing swords, is no basis for a system of government!
Exercise: Inverting Names #
Complete the function below which inverts names. “John Doe” should become “Doe, John”.
To reverse a list, you can use the function reversed()
.
def invert_name(name):
'your code here'
print(invert_name("John Doe"))
None
Indexing Strings#
You can use indexes with Strings, just like with lists. This yields characters, not words:
print("First character in quote is", quote[0])
print("Last character in quote is", quote[-1])
First character in quote is S
Last character in quote is !
Remeber that quote = "Strange women lying in ponds, distributing swords, is no basis for a system of government!"
.
sub_string = quote[57:63]
print(sub_string)
basis
string.replace()
#
string.replace(old, new)
takes two arguments. The first argument, old
, is the string to be replaced, and new
is the replacement.
This is often useful when changing from norwegian notation for decimal numbers to english notation for decimal numbers, as shown below:
number = "1,57"
number = number.replace(',', '.') # number is now string object "1.57"
number = float(number) # number is now float 1.57
print(number*2)
3.14
But can also be used to replace larger pieces of text:
quote = "All work and no play makes Jack a dull boy".replace("play", "python-programming")
print(quote)
All work and no python-programming makes Jack a dull boy
Sorting#
We can sort lists. This can be done in two ways. Firstly, we can make a new, sorted list, while also keeping the old one.
names = ["Charlie", "Alice", "Bob"]
names_sorted = sorted(names)
print(names_sorted)
['Alice', 'Bob', 'Charlie']
Secondly, if we don’t need the original list, it’s more efficient to sort the existing list in place:
print(names)
names.sort()
print(names)
['Charlie', 'Alice', 'Bob']
['Alice', 'Bob', 'Charlie']
We can reverse the sort order:
names.sort(reverse=True)
print(names)
['Charlie', 'Bob', 'Alice']
Exercise: Sorting Numbers #
We can also sort lists of numbers. Sort the list below, and print the result.
numbers = [2,5,3,15,1,35,0]
#Your solution
Case Law Exercise: Attorneys Sorted #
Here, we expand on the exercise “Attorneys and Head Matter” from the chapter JSON and Case Law, so you might want to have a look at your solution to that. Complete the code below to print the attorneys for each case in alphabetical order.
(The simplest solution uses the names as they occur in the data, starting with first names.
It would be nice to invert the names, but since the attorneys
field also contains company names, this is too tricky for this exercise.)
If you’re starting from scratch, you will need to browse the data.
import requests
import json
URL = "https://api.case.law/v1/cases/?jurisdiction=ill&full_case=true&decision_date_min=2011-01-01&page_size=3"
data = requests.get(URL).json()
#Your solution
---------------------------------------------------------------------------
JSONDecodeError Traceback (most recent call last)
File /opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/requests/models.py:974, in Response.json(self, **kwargs)
973 try:
--> 974 return complexjson.loads(self.text, **kwargs)
975 except JSONDecodeError as e:
976 # Catch JSON-related errors and raise as requests.JSONDecodeError
977 # This aliases json.JSONDecodeError and simplejson.JSONDecodeError
File /opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/json/__init__.py:346, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
343 if (cls is None and object_hook is None and
344 parse_int is None and parse_float is None and
345 parse_constant is None and object_pairs_hook is None and not kw):
--> 346 return _default_decoder.decode(s)
347 if cls is None:
File /opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/json/decoder.py:337, in JSONDecoder.decode(self, s, _w)
333 """Return the Python representation of ``s`` (a ``str`` instance
334 containing a JSON document).
335
336 """
--> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
338 end = _w(s, end).end()
File /opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/json/decoder.py:355, in JSONDecoder.raw_decode(self, s, idx)
354 except StopIteration as err:
--> 355 raise JSONDecodeError("Expecting value", s, err.value) from None
356 return obj, end
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
During handling of the above exception, another exception occurred:
JSONDecodeError Traceback (most recent call last)
Cell In[15], line 5
2 import json
4 URL = "https://api.case.law/v1/cases/?jurisdiction=ill&full_case=true&decision_date_min=2011-01-01&page_size=3"
----> 5 data = requests.get(URL).json()
7 #Your solution
File /opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/requests/models.py:978, in Response.json(self, **kwargs)
974 return complexjson.loads(self.text, **kwargs)
975 except JSONDecodeError as e:
976 # Catch JSON-related errors and raise as requests.JSONDecodeError
977 # This aliases json.JSONDecodeError and simplejson.JSONDecodeError
--> 978 raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Library Data Exercise: Creators Sorted #
Here, we expand on the exercise “Creators” from the chapter JSON and Library Catalog Data, so you might want to have a look at your solution to that. Complete the code below to print the creators for each case in alphabetical order.
If you’re starting from scratch, you will need to browse the data.
import requests
import json
URL = "https://api.nb.no/catalog/v1/items?digitalAccessibleOnly=true&size=3&filter=mediatype:bøker&q=Bing,Jon"
data = requests.get(URL).json()
embedded = data['_embedded']
items = embedded['items']
for item in items:
metadata = item['metadata']
print("Item title:", metadata['title'])
#your code here
Key Points#
Strings can be split into lists of words
We can use indexes to access individual characters in a String
Use
string.replace()
to replace parts of a string