Insights

BOJ Problem 2675: Analyzing Escape Character with a Debugger

funczun 2024. 11. 15. 11:15
Escape Character

 Escape characters play a special role within strings, and they are generally represented by a backslash(\). Escape characters combine with the following character to perform various special functions, so care must be taken in their usage.

 

Escape Sequence
\n New line
\t Tab
\\ Backslash
\' Single quote
\" Double quote

 

Checking Escape Character through Debugging

 \uXXXX is a Unicode escape sequence. Therefore, \u03B1 represents the Greek letter α.

 When the str is assigned to the variable test1, 'α' is assigned, while in the case of assigning a raw str to the variable test2, '\u03B1' is assigned. For reference, a raw str is indicated by placing an 'r' before the str.

 

 During debugging, I discovered that the double quotes("") I used to represent the str are shown as single quotes('') in memory. Additionally, the escape character backslash is recorded twice in the case of raw str.

 

What if the escape character was entered through user input?

 In a str, the backslash is treated as an escape character by default. Therefore, I realized that it's necessary to write code in raw str format, considering escape characters like the backslash.

 

 However, while solving the BOJ Problem 2675, I discovered that for user input, such as with the input() method or sys.stdin.readline(), the entered str is output exactly as it is, without needing to write it in raw str format.

 Through direct debugging, I found that escape characters were recorded twice when taking user input. Assuming that users are unlikely to input escape characters, it can be interpreted that the characters are recorded in memory as if they were raw str, in order to assign them exactly as they are.