---
# System prepended metadata

title: 'Project 4: Why we can use `\\x02` in our `sample` method doctest and information on how python doctests work'

---

# Project 4: Why we can use `\\x02` in our `sample` method doctest and information on how python doctests work

The reason why this doctest works actually has a lot to do with how Python doctests work. For reference, you can look at the attatched image which is a self-made doctest for `sample`, and note the 3 print statements `print('\x22')`  `print(r'\x22')`  and `print('\\x22')` . If you run the doctest you will actually see that all 3 print statements return `'"'`  which is the character represented by 22 in hex on an ASCII chart but what is strange is that in the doc test error message it turned our print statements into `print('"')`  `print(r'"')`  and `print('\x22')`. The reason this happened is that there are actually 2 different kinds of docstrings: the typical  `""" """ ` and then there are RAW docstrings which are `r""" """` . What I theorize is happening is that because we are using the typical `""" """`  docstring, python interprets it as
1. Turn all strings literals into their representation (so this turns our print statements into `print('"')`  `print(r'"')`  and `print('\x22')`
1. Then run the statements which would return  `'"'` ,`'"'` , and `'"'` (after interpreting `'\x22'` ) which is what we got.

What this means for our doctest is that after python turns all the string literals into their character form we are actually running this test:
`samp[:2] == '\x02 '`  instead of this test `samp[:2] == '\\x02 '`  (which is what is typed in the actual .py file). Then when we run the comparison operator `==`
we interpret the string literals again which is why the result is `True`. You can in fact change the doctest to `samp[:2] == '\x02 '` and it will still work because python will interpret `'\x02 '` and then during the comparison, it won't be interpreted again as it's already in the character form leading once again to `True`.

TLDR: Is correct that our function is returning `"\x02 "` , it's just that because of how python doctests work we can compare it to either `'\x02 '` or `'\\x02 '` and there is no change.

Here are some sources for where I got this info:
How python doctests work (including info on RAW doctests): https://docs.python.org/3/library/doctest.html
An ASCII chart for reference: https://www.asciitable.com/ 

![](https://i.imgur.com/Bjh5dTl.png)
