How to fix “ DeprecationWarning: invalid escape sequence” in Python?
I'm getting lots of warnings like this in Python:
DeprecationWarning: invalid escape sequence A
orcid_regex = 'A[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{3}[0-9X]Z'
DeprecationWarning: invalid escape sequence /
AUTH_TOKEN_PATH_PATTERN = '^/api/groups'
DeprecationWarning: invalid escape sequence
"""
DeprecationWarning: invalid escape sequence .
DOI_PATTERN = re.compile('(https?://(dx.)?doi.org/)?10.[0-9]{4,}[.0-9]*/.*')
<unknown>:20: DeprecationWarning: invalid escape sequence (
<unknown>:21: DeprecationWarning: invalid escape sequence (
What do they mean? And how can I fix them?
python python-3.x python-3.6
add a comment |
I'm getting lots of warnings like this in Python:
DeprecationWarning: invalid escape sequence A
orcid_regex = 'A[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{3}[0-9X]Z'
DeprecationWarning: invalid escape sequence /
AUTH_TOKEN_PATH_PATTERN = '^/api/groups'
DeprecationWarning: invalid escape sequence
"""
DeprecationWarning: invalid escape sequence .
DOI_PATTERN = re.compile('(https?://(dx.)?doi.org/)?10.[0-9]{4,}[.0-9]*/.*')
<unknown>:20: DeprecationWarning: invalid escape sequence (
<unknown>:21: DeprecationWarning: invalid escape sequence (
What do they mean? And how can I fix them?
python python-3.x python-3.6
add a comment |
I'm getting lots of warnings like this in Python:
DeprecationWarning: invalid escape sequence A
orcid_regex = 'A[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{3}[0-9X]Z'
DeprecationWarning: invalid escape sequence /
AUTH_TOKEN_PATH_PATTERN = '^/api/groups'
DeprecationWarning: invalid escape sequence
"""
DeprecationWarning: invalid escape sequence .
DOI_PATTERN = re.compile('(https?://(dx.)?doi.org/)?10.[0-9]{4,}[.0-9]*/.*')
<unknown>:20: DeprecationWarning: invalid escape sequence (
<unknown>:21: DeprecationWarning: invalid escape sequence (
What do they mean? And how can I fix them?
python python-3.x python-3.6
I'm getting lots of warnings like this in Python:
DeprecationWarning: invalid escape sequence A
orcid_regex = 'A[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{3}[0-9X]Z'
DeprecationWarning: invalid escape sequence /
AUTH_TOKEN_PATH_PATTERN = '^/api/groups'
DeprecationWarning: invalid escape sequence
"""
DeprecationWarning: invalid escape sequence .
DOI_PATTERN = re.compile('(https?://(dx.)?doi.org/)?10.[0-9]{4,}[.0-9]*/.*')
<unknown>:20: DeprecationWarning: invalid escape sequence (
<unknown>:21: DeprecationWarning: invalid escape sequence (
What do they mean? And how can I fix them?
python python-3.x python-3.6
python python-3.x python-3.6
asked Sep 14 at 16:30
Sean Hammond
2,5711025
2,5711025
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
is the escape character in Python string literals.
For example if you want to put a tab character in a string you would do:
>>> print("foo t bar")
foo bar
If you want to put a literal in a string you have to use
\
:
>>> print("foo \ bar")
foo bar
Or use a "raw string":
>>> print(r"foo bar")
foo bar
You can't just go putting backslashes in string literals whenever you want one. A backslash isn't valid when not followed by one of the valid escape sequences, and newer versions of Python print a deprecation warning. For example A
isn't an escape sequence:
$ python3.6 -Wd -c '"A"'
<string>:1: DeprecationWarning: invalid escape sequence A
If your backslash sequence does accidentally match one of Python's escape sequences, but you didn't mean it to, that's even worse.
So you should always use raw strings or \
.
It's important to remember that a string literal is still a string literal even if that string is intended to be used as a regular expression. Python's regular expression syntax supports lots of special sequences that begin with . For example
A
matches the start of a string. But A
is not valid in a Python string literal! This is invalid:
my_regex = "Afoo"
Instead you should do this:
my_regex = r"Afoo"
Docstrings are another one to remember: docstrings are string literals too, and invalid sequences are invalid in docstrings too! Use raw strings (
r"""..."""
) for docstrings if they contain 's.
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%2f52335970%2fhow-to-fix-string-deprecationwarning-invalid-escape-sequence-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
is the escape character in Python string literals.
For example if you want to put a tab character in a string you would do:
>>> print("foo t bar")
foo bar
If you want to put a literal in a string you have to use
\
:
>>> print("foo \ bar")
foo bar
Or use a "raw string":
>>> print(r"foo bar")
foo bar
You can't just go putting backslashes in string literals whenever you want one. A backslash isn't valid when not followed by one of the valid escape sequences, and newer versions of Python print a deprecation warning. For example A
isn't an escape sequence:
$ python3.6 -Wd -c '"A"'
<string>:1: DeprecationWarning: invalid escape sequence A
If your backslash sequence does accidentally match one of Python's escape sequences, but you didn't mean it to, that's even worse.
So you should always use raw strings or \
.
It's important to remember that a string literal is still a string literal even if that string is intended to be used as a regular expression. Python's regular expression syntax supports lots of special sequences that begin with . For example
A
matches the start of a string. But A
is not valid in a Python string literal! This is invalid:
my_regex = "Afoo"
Instead you should do this:
my_regex = r"Afoo"
Docstrings are another one to remember: docstrings are string literals too, and invalid sequences are invalid in docstrings too! Use raw strings (
r"""..."""
) for docstrings if they contain 's.
add a comment |
is the escape character in Python string literals.
For example if you want to put a tab character in a string you would do:
>>> print("foo t bar")
foo bar
If you want to put a literal in a string you have to use
\
:
>>> print("foo \ bar")
foo bar
Or use a "raw string":
>>> print(r"foo bar")
foo bar
You can't just go putting backslashes in string literals whenever you want one. A backslash isn't valid when not followed by one of the valid escape sequences, and newer versions of Python print a deprecation warning. For example A
isn't an escape sequence:
$ python3.6 -Wd -c '"A"'
<string>:1: DeprecationWarning: invalid escape sequence A
If your backslash sequence does accidentally match one of Python's escape sequences, but you didn't mean it to, that's even worse.
So you should always use raw strings or \
.
It's important to remember that a string literal is still a string literal even if that string is intended to be used as a regular expression. Python's regular expression syntax supports lots of special sequences that begin with . For example
A
matches the start of a string. But A
is not valid in a Python string literal! This is invalid:
my_regex = "Afoo"
Instead you should do this:
my_regex = r"Afoo"
Docstrings are another one to remember: docstrings are string literals too, and invalid sequences are invalid in docstrings too! Use raw strings (
r"""..."""
) for docstrings if they contain 's.
add a comment |
is the escape character in Python string literals.
For example if you want to put a tab character in a string you would do:
>>> print("foo t bar")
foo bar
If you want to put a literal in a string you have to use
\
:
>>> print("foo \ bar")
foo bar
Or use a "raw string":
>>> print(r"foo bar")
foo bar
You can't just go putting backslashes in string literals whenever you want one. A backslash isn't valid when not followed by one of the valid escape sequences, and newer versions of Python print a deprecation warning. For example A
isn't an escape sequence:
$ python3.6 -Wd -c '"A"'
<string>:1: DeprecationWarning: invalid escape sequence A
If your backslash sequence does accidentally match one of Python's escape sequences, but you didn't mean it to, that's even worse.
So you should always use raw strings or \
.
It's important to remember that a string literal is still a string literal even if that string is intended to be used as a regular expression. Python's regular expression syntax supports lots of special sequences that begin with . For example
A
matches the start of a string. But A
is not valid in a Python string literal! This is invalid:
my_regex = "Afoo"
Instead you should do this:
my_regex = r"Afoo"
Docstrings are another one to remember: docstrings are string literals too, and invalid sequences are invalid in docstrings too! Use raw strings (
r"""..."""
) for docstrings if they contain 's.
is the escape character in Python string literals.
For example if you want to put a tab character in a string you would do:
>>> print("foo t bar")
foo bar
If you want to put a literal in a string you have to use
\
:
>>> print("foo \ bar")
foo bar
Or use a "raw string":
>>> print(r"foo bar")
foo bar
You can't just go putting backslashes in string literals whenever you want one. A backslash isn't valid when not followed by one of the valid escape sequences, and newer versions of Python print a deprecation warning. For example A
isn't an escape sequence:
$ python3.6 -Wd -c '"A"'
<string>:1: DeprecationWarning: invalid escape sequence A
If your backslash sequence does accidentally match one of Python's escape sequences, but you didn't mean it to, that's even worse.
So you should always use raw strings or \
.
It's important to remember that a string literal is still a string literal even if that string is intended to be used as a regular expression. Python's regular expression syntax supports lots of special sequences that begin with . For example
A
matches the start of a string. But A
is not valid in a Python string literal! This is invalid:
my_regex = "Afoo"
Instead you should do this:
my_regex = r"Afoo"
Docstrings are another one to remember: docstrings are string literals too, and invalid sequences are invalid in docstrings too! Use raw strings (
r"""..."""
) for docstrings if they contain 's.
answered Sep 14 at 16:30
Sean Hammond
2,5711025
2,5711025
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f52335970%2fhow-to-fix-string-deprecationwarning-invalid-escape-sequence-in-python%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