Python: Why raw strings are not allowed to have odd number of backslashes?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
This works fine:
print(r'\')
This doesn't:
print(r'')
Why?
Edit. I understand what the doc is saying. But I don't see why this bug/feature/deficiency must exist. I wouldn't feel strange if this expression ''
fails because obviously the backslash is escaping the quote. But aren't raw strings meant to be raw?
As a counter example, in Bash you can write this:
echo ''
And it works as expected.
Is there anything to do with the parsers?
python string
add a comment |
This works fine:
print(r'\')
This doesn't:
print(r'')
Why?
Edit. I understand what the doc is saying. But I don't see why this bug/feature/deficiency must exist. I wouldn't feel strange if this expression ''
fails because obviously the backslash is escaping the quote. But aren't raw strings meant to be raw?
As a counter example, in Bash you can write this:
echo ''
And it works as expected.
Is there anything to do with the parsers?
python string
2
because you are escaping'
there and your string is incomplete without the ending single-quote
– Subramanya Vajiraya
Dec 10 '16 at 11:09
1
@SubramanyaVajiraya Doesn't the prefixr
mean the string should be interpreted verbatim?. Why would the backslash take effect here?
– Cyker
Dec 10 '16 at 11:33
add a comment |
This works fine:
print(r'\')
This doesn't:
print(r'')
Why?
Edit. I understand what the doc is saying. But I don't see why this bug/feature/deficiency must exist. I wouldn't feel strange if this expression ''
fails because obviously the backslash is escaping the quote. But aren't raw strings meant to be raw?
As a counter example, in Bash you can write this:
echo ''
And it works as expected.
Is there anything to do with the parsers?
python string
This works fine:
print(r'\')
This doesn't:
print(r'')
Why?
Edit. I understand what the doc is saying. But I don't see why this bug/feature/deficiency must exist. I wouldn't feel strange if this expression ''
fails because obviously the backslash is escaping the quote. But aren't raw strings meant to be raw?
As a counter example, in Bash you can write this:
echo ''
And it works as expected.
Is there anything to do with the parsers?
python string
python string
edited Dec 10 '16 at 11:54
Cyker
asked Dec 10 '16 at 11:06
CykerCyker
3,14863347
3,14863347
2
because you are escaping'
there and your string is incomplete without the ending single-quote
– Subramanya Vajiraya
Dec 10 '16 at 11:09
1
@SubramanyaVajiraya Doesn't the prefixr
mean the string should be interpreted verbatim?. Why would the backslash take effect here?
– Cyker
Dec 10 '16 at 11:33
add a comment |
2
because you are escaping'
there and your string is incomplete without the ending single-quote
– Subramanya Vajiraya
Dec 10 '16 at 11:09
1
@SubramanyaVajiraya Doesn't the prefixr
mean the string should be interpreted verbatim?. Why would the backslash take effect here?
– Cyker
Dec 10 '16 at 11:33
2
2
because you are escaping
'
there and your string is incomplete without the ending single-quote– Subramanya Vajiraya
Dec 10 '16 at 11:09
because you are escaping
'
there and your string is incomplete without the ending single-quote– Subramanya Vajiraya
Dec 10 '16 at 11:09
1
1
@SubramanyaVajiraya Doesn't the prefix
r
mean the string should be interpreted verbatim?. Why would the backslash take effect here?– Cyker
Dec 10 '16 at 11:33
@SubramanyaVajiraya Doesn't the prefix
r
mean the string should be interpreted verbatim?. Why would the backslash take effect here?– Cyker
Dec 10 '16 at 11:33
add a comment |
4 Answers
4
active
oldest
votes
From the documentation,
Specifically, a raw string cannot end in a single backslash (since the backslash would escape the following quote character). Note also that a single backslash followed by a newline is interpreted as those two characters as part of the string, not as a line continuation.
Is that because the parser cannot handle''
? Raw strings are meant to be raw, but this looks like a deficiency.
– Cyker
Dec 10 '16 at 11:36
Python's lexical analysis is defined to require its input to escape backslashes, as (again) pointed out in the documentation.
– Michael Foukarakis
Dec 10 '16 at 11:40
Do you mean the grammar listed here? Could you please update your answer with the paragraphs pointing out what you said here?
– Cyker
Dec 10 '16 at 11:44
Everything I've said is in the same link contained in my answer. Please read it.
– Michael Foukarakis
Dec 10 '16 at 11:45
The most relevant part I can find is the CFG:shortstringitem ::= shortstringchar | escapeseq
. If this is what you mean then it looks like a deficiency. There may be a better grammar for raw strings.
– Cyker
Dec 10 '16 at 11:48
add a comment |
When you write print(r'')
, Python understand '
in that statement as a character. Because of that python raised syntax error because the there is a incomplete string inside print function.
For an example if you need to print i am "free" man
, you should write
print("i am "free" man")
add a comment |
The limitation is due to the fact that you need someway to include a '
inside a raw. Otherwise there is no way to put bob said "I'm not hungry"
in a string.
So you end up in weird situation where you need an escape character for this case. So in raw strings you escape a '
with a and yes the
stays in the string.
So r'bob said "I'm not hungry"'
it is!!
raw
is for the convenience of backslashes. If you want to include single/double quotes then you should use triple quotes. Unfortunately in the case I pointed out in question,raw
isn't completely convenient for backslashes.
– Cyker
Dec 10 '16 at 12:19
Raw is normally so you can put backlashes in them without them being interpreted by the parser part of an escape sequence. But as my answer points out you still need an a way to escape quotes. Triple quoting solves most use cases but there will still be times where you need to encode a triple quote in string and so he problem remains. Having an escape character is still required.
– Nath
Dec 10 '16 at 12:28
add a comment |
Simple solution would be to use triple quotes. ex: r"""abc"hello'world"""
. Even if you are using raw strings, escape sequences still work in some cases.
Just for reference, this doesn't work for the op's question. r"""""" the last " is escaped and the string would not be closed.
– matt
Nov 22 '18 at 8:45
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41074815%2fpython-why-raw-strings-are-not-allowed-to-have-odd-number-of-backslashes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
From the documentation,
Specifically, a raw string cannot end in a single backslash (since the backslash would escape the following quote character). Note also that a single backslash followed by a newline is interpreted as those two characters as part of the string, not as a line continuation.
Is that because the parser cannot handle''
? Raw strings are meant to be raw, but this looks like a deficiency.
– Cyker
Dec 10 '16 at 11:36
Python's lexical analysis is defined to require its input to escape backslashes, as (again) pointed out in the documentation.
– Michael Foukarakis
Dec 10 '16 at 11:40
Do you mean the grammar listed here? Could you please update your answer with the paragraphs pointing out what you said here?
– Cyker
Dec 10 '16 at 11:44
Everything I've said is in the same link contained in my answer. Please read it.
– Michael Foukarakis
Dec 10 '16 at 11:45
The most relevant part I can find is the CFG:shortstringitem ::= shortstringchar | escapeseq
. If this is what you mean then it looks like a deficiency. There may be a better grammar for raw strings.
– Cyker
Dec 10 '16 at 11:48
add a comment |
From the documentation,
Specifically, a raw string cannot end in a single backslash (since the backslash would escape the following quote character). Note also that a single backslash followed by a newline is interpreted as those two characters as part of the string, not as a line continuation.
Is that because the parser cannot handle''
? Raw strings are meant to be raw, but this looks like a deficiency.
– Cyker
Dec 10 '16 at 11:36
Python's lexical analysis is defined to require its input to escape backslashes, as (again) pointed out in the documentation.
– Michael Foukarakis
Dec 10 '16 at 11:40
Do you mean the grammar listed here? Could you please update your answer with the paragraphs pointing out what you said here?
– Cyker
Dec 10 '16 at 11:44
Everything I've said is in the same link contained in my answer. Please read it.
– Michael Foukarakis
Dec 10 '16 at 11:45
The most relevant part I can find is the CFG:shortstringitem ::= shortstringchar | escapeseq
. If this is what you mean then it looks like a deficiency. There may be a better grammar for raw strings.
– Cyker
Dec 10 '16 at 11:48
add a comment |
From the documentation,
Specifically, a raw string cannot end in a single backslash (since the backslash would escape the following quote character). Note also that a single backslash followed by a newline is interpreted as those two characters as part of the string, not as a line continuation.
From the documentation,
Specifically, a raw string cannot end in a single backslash (since the backslash would escape the following quote character). Note also that a single backslash followed by a newline is interpreted as those two characters as part of the string, not as a line continuation.
answered Dec 10 '16 at 11:08
Michael FoukarakisMichael Foukarakis
29.6k466104
29.6k466104
Is that because the parser cannot handle''
? Raw strings are meant to be raw, but this looks like a deficiency.
– Cyker
Dec 10 '16 at 11:36
Python's lexical analysis is defined to require its input to escape backslashes, as (again) pointed out in the documentation.
– Michael Foukarakis
Dec 10 '16 at 11:40
Do you mean the grammar listed here? Could you please update your answer with the paragraphs pointing out what you said here?
– Cyker
Dec 10 '16 at 11:44
Everything I've said is in the same link contained in my answer. Please read it.
– Michael Foukarakis
Dec 10 '16 at 11:45
The most relevant part I can find is the CFG:shortstringitem ::= shortstringchar | escapeseq
. If this is what you mean then it looks like a deficiency. There may be a better grammar for raw strings.
– Cyker
Dec 10 '16 at 11:48
add a comment |
Is that because the parser cannot handle''
? Raw strings are meant to be raw, but this looks like a deficiency.
– Cyker
Dec 10 '16 at 11:36
Python's lexical analysis is defined to require its input to escape backslashes, as (again) pointed out in the documentation.
– Michael Foukarakis
Dec 10 '16 at 11:40
Do you mean the grammar listed here? Could you please update your answer with the paragraphs pointing out what you said here?
– Cyker
Dec 10 '16 at 11:44
Everything I've said is in the same link contained in my answer. Please read it.
– Michael Foukarakis
Dec 10 '16 at 11:45
The most relevant part I can find is the CFG:shortstringitem ::= shortstringchar | escapeseq
. If this is what you mean then it looks like a deficiency. There may be a better grammar for raw strings.
– Cyker
Dec 10 '16 at 11:48
Is that because the parser cannot handle
''
? Raw strings are meant to be raw, but this looks like a deficiency.– Cyker
Dec 10 '16 at 11:36
Is that because the parser cannot handle
''
? Raw strings are meant to be raw, but this looks like a deficiency.– Cyker
Dec 10 '16 at 11:36
Python's lexical analysis is defined to require its input to escape backslashes, as (again) pointed out in the documentation.
– Michael Foukarakis
Dec 10 '16 at 11:40
Python's lexical analysis is defined to require its input to escape backslashes, as (again) pointed out in the documentation.
– Michael Foukarakis
Dec 10 '16 at 11:40
Do you mean the grammar listed here? Could you please update your answer with the paragraphs pointing out what you said here?
– Cyker
Dec 10 '16 at 11:44
Do you mean the grammar listed here? Could you please update your answer with the paragraphs pointing out what you said here?
– Cyker
Dec 10 '16 at 11:44
Everything I've said is in the same link contained in my answer. Please read it.
– Michael Foukarakis
Dec 10 '16 at 11:45
Everything I've said is in the same link contained in my answer. Please read it.
– Michael Foukarakis
Dec 10 '16 at 11:45
The most relevant part I can find is the CFG:
shortstringitem ::= shortstringchar | escapeseq
. If this is what you mean then it looks like a deficiency. There may be a better grammar for raw strings.– Cyker
Dec 10 '16 at 11:48
The most relevant part I can find is the CFG:
shortstringitem ::= shortstringchar | escapeseq
. If this is what you mean then it looks like a deficiency. There may be a better grammar for raw strings.– Cyker
Dec 10 '16 at 11:48
add a comment |
When you write print(r'')
, Python understand '
in that statement as a character. Because of that python raised syntax error because the there is a incomplete string inside print function.
For an example if you need to print i am "free" man
, you should write
print("i am "free" man")
add a comment |
When you write print(r'')
, Python understand '
in that statement as a character. Because of that python raised syntax error because the there is a incomplete string inside print function.
For an example if you need to print i am "free" man
, you should write
print("i am "free" man")
add a comment |
When you write print(r'')
, Python understand '
in that statement as a character. Because of that python raised syntax error because the there is a incomplete string inside print function.
For an example if you need to print i am "free" man
, you should write
print("i am "free" man")
When you write print(r'')
, Python understand '
in that statement as a character. Because of that python raised syntax error because the there is a incomplete string inside print function.
For an example if you need to print i am "free" man
, you should write
print("i am "free" man")
answered Dec 10 '16 at 11:34
Bodhi94Bodhi94
999621
999621
add a comment |
add a comment |
The limitation is due to the fact that you need someway to include a '
inside a raw. Otherwise there is no way to put bob said "I'm not hungry"
in a string.
So you end up in weird situation where you need an escape character for this case. So in raw strings you escape a '
with a and yes the
stays in the string.
So r'bob said "I'm not hungry"'
it is!!
raw
is for the convenience of backslashes. If you want to include single/double quotes then you should use triple quotes. Unfortunately in the case I pointed out in question,raw
isn't completely convenient for backslashes.
– Cyker
Dec 10 '16 at 12:19
Raw is normally so you can put backlashes in them without them being interpreted by the parser part of an escape sequence. But as my answer points out you still need an a way to escape quotes. Triple quoting solves most use cases but there will still be times where you need to encode a triple quote in string and so he problem remains. Having an escape character is still required.
– Nath
Dec 10 '16 at 12:28
add a comment |
The limitation is due to the fact that you need someway to include a '
inside a raw. Otherwise there is no way to put bob said "I'm not hungry"
in a string.
So you end up in weird situation where you need an escape character for this case. So in raw strings you escape a '
with a and yes the
stays in the string.
So r'bob said "I'm not hungry"'
it is!!
raw
is for the convenience of backslashes. If you want to include single/double quotes then you should use triple quotes. Unfortunately in the case I pointed out in question,raw
isn't completely convenient for backslashes.
– Cyker
Dec 10 '16 at 12:19
Raw is normally so you can put backlashes in them without them being interpreted by the parser part of an escape sequence. But as my answer points out you still need an a way to escape quotes. Triple quoting solves most use cases but there will still be times where you need to encode a triple quote in string and so he problem remains. Having an escape character is still required.
– Nath
Dec 10 '16 at 12:28
add a comment |
The limitation is due to the fact that you need someway to include a '
inside a raw. Otherwise there is no way to put bob said "I'm not hungry"
in a string.
So you end up in weird situation where you need an escape character for this case. So in raw strings you escape a '
with a and yes the
stays in the string.
So r'bob said "I'm not hungry"'
it is!!
The limitation is due to the fact that you need someway to include a '
inside a raw. Otherwise there is no way to put bob said "I'm not hungry"
in a string.
So you end up in weird situation where you need an escape character for this case. So in raw strings you escape a '
with a and yes the
stays in the string.
So r'bob said "I'm not hungry"'
it is!!
answered Dec 10 '16 at 12:15
NathNath
458311
458311
raw
is for the convenience of backslashes. If you want to include single/double quotes then you should use triple quotes. Unfortunately in the case I pointed out in question,raw
isn't completely convenient for backslashes.
– Cyker
Dec 10 '16 at 12:19
Raw is normally so you can put backlashes in them without them being interpreted by the parser part of an escape sequence. But as my answer points out you still need an a way to escape quotes. Triple quoting solves most use cases but there will still be times where you need to encode a triple quote in string and so he problem remains. Having an escape character is still required.
– Nath
Dec 10 '16 at 12:28
add a comment |
raw
is for the convenience of backslashes. If you want to include single/double quotes then you should use triple quotes. Unfortunately in the case I pointed out in question,raw
isn't completely convenient for backslashes.
– Cyker
Dec 10 '16 at 12:19
Raw is normally so you can put backlashes in them without them being interpreted by the parser part of an escape sequence. But as my answer points out you still need an a way to escape quotes. Triple quoting solves most use cases but there will still be times where you need to encode a triple quote in string and so he problem remains. Having an escape character is still required.
– Nath
Dec 10 '16 at 12:28
raw
is for the convenience of backslashes. If you want to include single/double quotes then you should use triple quotes. Unfortunately in the case I pointed out in question, raw
isn't completely convenient for backslashes.– Cyker
Dec 10 '16 at 12:19
raw
is for the convenience of backslashes. If you want to include single/double quotes then you should use triple quotes. Unfortunately in the case I pointed out in question, raw
isn't completely convenient for backslashes.– Cyker
Dec 10 '16 at 12:19
Raw is normally so you can put backlashes in them without them being interpreted by the parser part of an escape sequence. But as my answer points out you still need an a way to escape quotes. Triple quoting solves most use cases but there will still be times where you need to encode a triple quote in string and so he problem remains. Having an escape character is still required.
– Nath
Dec 10 '16 at 12:28
Raw is normally so you can put backlashes in them without them being interpreted by the parser part of an escape sequence. But as my answer points out you still need an a way to escape quotes. Triple quoting solves most use cases but there will still be times where you need to encode a triple quote in string and so he problem remains. Having an escape character is still required.
– Nath
Dec 10 '16 at 12:28
add a comment |
Simple solution would be to use triple quotes. ex: r"""abc"hello'world"""
. Even if you are using raw strings, escape sequences still work in some cases.
Just for reference, this doesn't work for the op's question. r"""""" the last " is escaped and the string would not be closed.
– matt
Nov 22 '18 at 8:45
add a comment |
Simple solution would be to use triple quotes. ex: r"""abc"hello'world"""
. Even if you are using raw strings, escape sequences still work in some cases.
Just for reference, this doesn't work for the op's question. r"""""" the last " is escaped and the string would not be closed.
– matt
Nov 22 '18 at 8:45
add a comment |
Simple solution would be to use triple quotes. ex: r"""abc"hello'world"""
. Even if you are using raw strings, escape sequences still work in some cases.
Simple solution would be to use triple quotes. ex: r"""abc"hello'world"""
. Even if you are using raw strings, escape sequences still work in some cases.
answered Dec 10 '16 at 11:56
Subramanya VajirayaSubramanya Vajiraya
17210
17210
Just for reference, this doesn't work for the op's question. r"""""" the last " is escaped and the string would not be closed.
– matt
Nov 22 '18 at 8:45
add a comment |
Just for reference, this doesn't work for the op's question. r"""""" the last " is escaped and the string would not be closed.
– matt
Nov 22 '18 at 8:45
Just for reference, this doesn't work for the op's question. r"""""" the last " is escaped and the string would not be closed.
– matt
Nov 22 '18 at 8:45
Just for reference, this doesn't work for the op's question. r"""""" the last " is escaped and the string would not be closed.
– matt
Nov 22 '18 at 8:45
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41074815%2fpython-why-raw-strings-are-not-allowed-to-have-odd-number-of-backslashes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
because you are escaping
'
there and your string is incomplete without the ending single-quote– Subramanya Vajiraya
Dec 10 '16 at 11:09
1
@SubramanyaVajiraya Doesn't the prefix
r
mean the string should be interpreted verbatim?. Why would the backslash take effect here?– Cyker
Dec 10 '16 at 11:33