<jupyter_start><jupyter_text>Ex1:Define a string s = 'colorless'. Write a Python statement that changes this to "colourless" using only the slice and concatenation operations.<jupyter_code>s = 'colorless'
sc = s[:4] + 'u' + s[4:]
print(sc)<jupyter_output>colourless<jupyter_text>Ex2:☼ We can use the slice notation to remove morphological endings on words. For example, 'dogs'[:-1] removes the last character of dogs, leaving dog. Use slice notation to remove the affixes from these words (we've inserted a hyphen to indicate the affix boundary, but omit this from your strings): dish-es, run-ning, nation-ality, un-do, pre-heat.<jupyter_code>def removeSuffix(word):
    hyphen_index = word.find('-')
    return word[:hyphen_index]

words = ["dish-es", "run-ning", "nation-ality", "un-do", "pre-heat"]
for word in words:
    print(removeSuffix(word), end=' ')<jupyter_output>dish run nation un pre<jupyter_text>Ex3☼ We saw how we can generate an IndexError by indexing beyond the end of a string. Is it possible to construct an index that goes too far to the left, before the start of the string?<jupyter_code>s = "good"
# s[-5]<jupyter_output><empty_output><jupyter_text>Ex4☼ We can specify a "step" size for the slice. The following returns every second character within the slice: monty[6:11:2]. It also works in the reverse direction: monty[10:5:-2] Try these for yourself, then experiment with different step values.<jupyter_code>s = "hello, world"
s[0:10:3]<jupyter_output><empty_output><jupyter_text>Ex5☼ What happens if you ask the interpreter to evaluate monty[::-1]? Explain why this is a reasonable result.<jupyter_code>s[::-1]<jupyter_output><empty_output><jupyter_text>Ex6☼ Describe the class of strings matched by the following regular expressions.[a-zA-Z]+[A-Z][a-z]*p[aeiou]{,2}t\d+(\.\d+)?([^aeiou][aeiou][^aeiou])*\w+|[^\w\s]+Test your answers using nltk.re_show().<jupyter_code>import nltk

nltk.re_show(r"\d+(\.\d+)?", "2.3")<jupyter_output>{2.3}
{'fraction_non_alphanumeric': 0.13326551373346898, 'fraction_numerical': 0.014750762970498474, 'mean_word_length': 5.853658536585366, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '272638', 'license': 'MIT', 'n_tokens_mistral': 698, 'n_tokens_neox': 681, 'n_words': 215, 'path': 'nlp/ntlk/nltk_ch3.ipynb', 'repo_name': 'tavaresdong/learn-nlp', 'size': '1966'}