<jupyter_start><jupyter_text>Python’s standard library has functions that accept str or bytesarguments and behave differently depending on the type. str Versus bytes in Regular Expressions Example 4-23. ramanujan.py: compare behavior of simple str and bytesregular expressions<jupyter_code>import re

re_numbers_str = re.compile(r'\d+')
re_words_str = re.compile(r'\w+')
re_numbers_bytes = re.compile(rb'\d+')
re_words_bytes = re.compile(rb'\w+')

text_str = ("Ramanujan saw \u0be7\u0bed\u0be8\u0bef"
            " as 1729 = 13 + 123 = 93 + 103.")

text_bytes = text_str.encode('utf_8')

print(f'Text\n {text_str!r}')
print('Numbers')
print(' str :', re_numbers_str.findall(text_str))
print(' bytes:', re_numbers_bytes.findall(text_bytes))
print('Words')
print(' str :', re_words_str.findall(text_str))
print(' bytes:', re_words_bytes.findall(text_bytes))<jupyter_output>Text
 'Ramanujan saw ௧௭௨௯ as 1729 = 13 + 123 = 93 + 103.'
Numbers
 str : ['௧௭௨௯', '1729', '13', '123', '93', '103']
 bytes: [b'1729', b'13', b'123', b'93', b'103']
Words
 str : ['Ramanujan', 'saw', '௧௭௨௯', 'as', '1729', '13', '123', '93', '103']
 bytes: [b'Ramanujan', b'saw', b'as', b'1729', b'13', b'123', b'93', b'103']<jupyter_text>Example 4-24. listdir with str and bytes arguments and results<jupyter_code>import os
os.listdir('.')
os.listdir(b'.')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17785977859778598, 'fraction_numerical': 0.08044280442804429, 'mean_word_length': 5.918367346938775, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '514385', 'license': 'MIT', 'n_tokens_mistral': 628, 'n_tokens_neox': 543, 'n_words': 126, 'path': 'chapter 4-Unicode Text versus Bytes/example 4-23--Dual-Mode str and bytes APIs.ipynb', 'repo_name': 'Jalilnkh/KartalOl-Fluent-Python-2022', 'size': '1355'}