Hello this is Juny.
there are special characters in string.
they are called "escape sequence".
we will see some of them in this post.
there are special characters in string.
they are called "escape sequence".
we will see some of them in this post.
escape sequence
escape sequence has a special behavior escaping its original behavior.
for example,
'n' is just 'n' as alphabet.
but,
if 'n' is represented as escape sequence,
it will has the other meaning as new line.
in order to make escape sequence with normal character,
add '\' at the beginning of the character.
it will has the other meaning as new line.
in order to make escape sequence with normal character,
add '\' at the beginning of the character.
actually,
the original data is not changed.
only the output will be changed on the screen.
for example,
if there is 'aaa\rbbb' string,
the output will be 'bbb' on screen.
but, the data is 'aaa\rbbb' as it was.
the original data is not changed.
only the output will be changed on the screen.
for example,
if there is 'aaa\rbbb' string,
the output will be 'bbb' on screen.
but, the data is 'aaa\rbbb' as it was.
carriage return
'\r' is escape sequence for carriage return.
if it operates,
the cursor is moved to the beginning.
for example,
str1 = 'aaabbb'str2 = 'aaa\rbbb'print(str1)print(str2)
'aaabbb' is assigned to str1.
'aaa\rbbb' that has carriage return is assigned to str2.
they are printed out on screen.
the string which has carriage return is printed as 'bbb' on screen.
for explanation,
'aaa' is wrote on screen at first.
if interpreter meet the carriage return,
the cursor of screen move to the beginning of line.
and then,
it continue to write the rest of string.
as a result,
'bbb' override 'aaa'.
for explanation,
'aaa' is wrote on screen at first.
if interpreter meet the carriage return,
the cursor of screen move to the beginning of line.
and then,
it continue to write the rest of string.
as a result,
'bbb' override 'aaa'.
backspace
'\b' is escape sequence for backspace.
if it operates,
the front character of backspace will be removed.
cursor of screen is also back once.
for example,
str1 = 'aaabbb'str2 = 'aaa\bbbb'print(str1)print(str2)
'aaabbb' is assigned to str1.
'aaa\bbbb' which has backspace is assigned to str2.
and then,
they are printed out.
'aaa\bbbb' which has backspace is assigned to str2.
and then,
they are printed out.
'a' is removed in str2.
so,
'bbb' is printed out.
so,
'bbb' is printed out.
tab
'\t' is escape sequence for tab.
if you use it,
a tab is added on the string.
in general,
tab has 4 white spaces in programming.
str1 = 'aaa\tbbb'print(str1)
'aaa\tbbb' is assigned to str1.
and then,
str1 is printed out on screen.
and then,
str1 is printed out on screen.
Quotes
the quotes are used for delimiter of string literal in python.
if you try to use them in the string,
you might meet error.
if you use escape sequence for quotes,
you can make it.
let's check it.
if you try to use them in the string,
you might meet error.
if you use escape sequence for quotes,
you can make it.
let's check it.
single = 'hello \'python\''double = "hello \"python\""print(single)print(double)
\' is used in single quoted string.
\" is used in double quoted string.
print out them.
\" is used in double quoted string.
print out them.
newline
'\n' is for newline.
let's check it right now.
newline = 'hello \npython'print(newline)
raw string
we need to print '\' sometimes.
but,
if we use '\' in string,
it will be handled as escape sequence.
in this case,
we can use raw string.
if it is used,
escape sequence doesn't work.
if you add either 'r' or 'R' at the beginning of string literal,
it will work.
but,
if we use '\' in string,
it will be handled as escape sequence.
in this case,
we can use raw string.
if it is used,
escape sequence doesn't work.
if you add either 'r' or 'R' at the beginning of string literal,
it will work.
raw1 = r'hello \npython'raw2 = R'hello \tpython'print(raw1)print(raw2)
the string with 'r' is assigned to raw1.
the string with 'R' is assigned to raw2.
the string with 'R' is assigned to raw2.
each one has escape sequence.
'\n' and '\t' are printed as raw string.
python escape sequence in string
Reviewed by Juny
on
7월 10, 2019
Rating:
댓글 없음: