1 – M5 SC 1 Raw Strings V1

Hello and welcome. The following lessons, we will learn how to create basic regular expressions in Python. As mentioned in the previous lesson, regular expressions or regexes as for short, allows us search for patterns of text in documents. But before we dive in and start creating our regular expressions, let’s take a quick look at raw strings since we will be using them to create our regular expressions. First, let’s remember that in Python, strings can be specified using either double quotes or single quotes. Special characters within these strings such as new lines or tabs are specified by using the backslash character. For example, here we have a string that has both a new line and a tab. When we print this string with the print function, we can see that the print function puts out, “Hello world,” but has interpreted the \n as a newline character and the \t as a tab. In some cases however, you may want the print function to interpret the strings literally. What that means is that you do not want the characters following a backslash to be interpreted as special characters. We can do this by adding an r before the beginning of the string as we have done here. Strings that are preceded with an r are known as raw strings. So now, if we print the same string we had before, but now as a raw string because we have added the r before the beginning of the string, we can see that now the print function prints Hello world, but it doesn’t print a new line or a tab anymore. But rather, it prints out the \n and \t literally. We will be using raw strings to create our regular expressions because regular expressions themselves also use the backslash to indicate their own special characters. Therefore, by using raw strings, we avoid the problem of Python interpreting our special characters in the wrong way.

%d 블로거가 이것을 좋아합니다: