Backslash use in python 3
What is the difference between print("this is \ double backslash") and print("this is \\ double backslash") as they are giving same answer ?
python-3.x
add a comment |
What is the difference between print("this is \ double backslash") and print("this is \\ double backslash") as they are giving same answer ?
python-3.x
2
For me they give different answers I get and \
– Tobias Wilfert
Nov 23 '18 at 16:46
add a comment |
What is the difference between print("this is \ double backslash") and print("this is \\ double backslash") as they are giving same answer ?
python-3.x
What is the difference between print("this is \ double backslash") and print("this is \\ double backslash") as they are giving same answer ?
python-3.x
python-3.x
edited Nov 23 '18 at 17:02
Lucas
2,31711128
2,31711128
asked Nov 23 '18 at 16:41
Shivam Saraswat
32
32
2
For me they give different answers I get and \
– Tobias Wilfert
Nov 23 '18 at 16:46
add a comment |
2
For me they give different answers I get and \
– Tobias Wilfert
Nov 23 '18 at 16:46
2
2
For me they give different answers I get and \
– Tobias Wilfert
Nov 23 '18 at 16:46
For me they give different answers I get and \
– Tobias Wilfert
Nov 23 '18 at 16:46
add a comment |
2 Answers
2
active
oldest
votes
In the case of:
print("this is \ double backslash") # case 1
print("this is \\ double backslash") # case 2
which indeed outputs:
this is \ double backslash
this is \ double backslash
The reason is because in case 1, the first is acting upon the after it and the third is escaped automatically due to the whitespace that follows, whereas in case 2, the first and third are acting upon the second and fourth respectively. Thus, both cases result in two to be printed.
add a comment |
Like many languages, Python allows you to use backslashes in string literals to enter special characters (like a newline) or escape otherwise untypeable characters (like quotes of the same type the string is quoted with).
Unlike in some other languages, when the following character is not one of the special characters that Python's string literals recognize (e.g. t, n, , "), it will just assume that you meant to just type a literal backslash.
Four backslashes are just two escaped backslashes. When you enter three backslashes followed by a space Python escapes your third backslash automatically, meaning it also results in printing two backslashes.
You can see the difference if your third/fourth backslash is followed by e.g. a t:
>>> print("\\t")
\t
>>> print("\t")
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%2f53450303%2fbackslash-use-in-python-3%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
In the case of:
print("this is \ double backslash") # case 1
print("this is \\ double backslash") # case 2
which indeed outputs:
this is \ double backslash
this is \ double backslash
The reason is because in case 1, the first is acting upon the after it and the third is escaped automatically due to the whitespace that follows, whereas in case 2, the first and third are acting upon the second and fourth respectively. Thus, both cases result in two to be printed.
add a comment |
In the case of:
print("this is \ double backslash") # case 1
print("this is \\ double backslash") # case 2
which indeed outputs:
this is \ double backslash
this is \ double backslash
The reason is because in case 1, the first is acting upon the after it and the third is escaped automatically due to the whitespace that follows, whereas in case 2, the first and third are acting upon the second and fourth respectively. Thus, both cases result in two to be printed.
add a comment |
In the case of:
print("this is \ double backslash") # case 1
print("this is \\ double backslash") # case 2
which indeed outputs:
this is \ double backslash
this is \ double backslash
The reason is because in case 1, the first is acting upon the after it and the third is escaped automatically due to the whitespace that follows, whereas in case 2, the first and third are acting upon the second and fourth respectively. Thus, both cases result in two to be printed.
In the case of:
print("this is \ double backslash") # case 1
print("this is \\ double backslash") # case 2
which indeed outputs:
this is \ double backslash
this is \ double backslash
The reason is because in case 1, the first is acting upon the after it and the third is escaped automatically due to the whitespace that follows, whereas in case 2, the first and third are acting upon the second and fourth respectively. Thus, both cases result in two to be printed.
edited Nov 23 '18 at 20:04
answered Nov 23 '18 at 17:15
Ctrl S
503323
503323
add a comment |
add a comment |
Like many languages, Python allows you to use backslashes in string literals to enter special characters (like a newline) or escape otherwise untypeable characters (like quotes of the same type the string is quoted with).
Unlike in some other languages, when the following character is not one of the special characters that Python's string literals recognize (e.g. t, n, , "), it will just assume that you meant to just type a literal backslash.
Four backslashes are just two escaped backslashes. When you enter three backslashes followed by a space Python escapes your third backslash automatically, meaning it also results in printing two backslashes.
You can see the difference if your third/fourth backslash is followed by e.g. a t:
>>> print("\\t")
\t
>>> print("\t")
add a comment |
Like many languages, Python allows you to use backslashes in string literals to enter special characters (like a newline) or escape otherwise untypeable characters (like quotes of the same type the string is quoted with).
Unlike in some other languages, when the following character is not one of the special characters that Python's string literals recognize (e.g. t, n, , "), it will just assume that you meant to just type a literal backslash.
Four backslashes are just two escaped backslashes. When you enter three backslashes followed by a space Python escapes your third backslash automatically, meaning it also results in printing two backslashes.
You can see the difference if your third/fourth backslash is followed by e.g. a t:
>>> print("\\t")
\t
>>> print("\t")
add a comment |
Like many languages, Python allows you to use backslashes in string literals to enter special characters (like a newline) or escape otherwise untypeable characters (like quotes of the same type the string is quoted with).
Unlike in some other languages, when the following character is not one of the special characters that Python's string literals recognize (e.g. t, n, , "), it will just assume that you meant to just type a literal backslash.
Four backslashes are just two escaped backslashes. When you enter three backslashes followed by a space Python escapes your third backslash automatically, meaning it also results in printing two backslashes.
You can see the difference if your third/fourth backslash is followed by e.g. a t:
>>> print("\\t")
\t
>>> print("\t")
Like many languages, Python allows you to use backslashes in string literals to enter special characters (like a newline) or escape otherwise untypeable characters (like quotes of the same type the string is quoted with).
Unlike in some other languages, when the following character is not one of the special characters that Python's string literals recognize (e.g. t, n, , "), it will just assume that you meant to just type a literal backslash.
Four backslashes are just two escaped backslashes. When you enter three backslashes followed by a space Python escapes your third backslash automatically, meaning it also results in printing two backslashes.
You can see the difference if your third/fourth backslash is followed by e.g. a t:
>>> print("\\t")
\t
>>> print("\t")
answered Nov 23 '18 at 16:46
L3viathan
15.6k12847
15.6k12847
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%2f53450303%2fbackslash-use-in-python-3%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
For me they give different answers I get and \
– Tobias Wilfert
Nov 23 '18 at 16:46