Python - Any other way to add a space between 2 concatenating strings using print() function
Let us take 2 strings a and b and concatenate them using + using print() function.
a = 'Hello'
b = 'World'
print(a + b, sep = ' ')
# prints HelloWorld
print(a + ' ' + b)
# prints Hello World
I have 2 questions:
a) Can I use sep to add a space between the concatenated strings a and b?
b) If not, then, Is there any other way to add a space between the concatenated strings a and b?
python-3.x
|
show 3 more comments
Let us take 2 strings a and b and concatenate them using + using print() function.
a = 'Hello'
b = 'World'
print(a + b, sep = ' ')
# prints HelloWorld
print(a + ' ' + b)
# prints Hello World
I have 2 questions:
a) Can I use sep to add a space between the concatenated strings a and b?
b) If not, then, Is there any other way to add a space between the concatenated strings a and b?
python-3.x
print(a, b, sep=' ')?
– jpp
Nov 13 '18 at 10:27
I want to add space while concatenating the 2 strings
– Preetkaran Singh
Nov 13 '18 at 10:29
...f'{a} {b}'?
– jpp
Nov 13 '18 at 10:29
@jpp, I want to add space while concatenating 2 strings. Your solutions add a space between the strings, but these strings are not concatenated.
– Preetkaran Singh
Nov 13 '18 at 10:33
2
print(*[a]+[b])But please don't do that. Use Python as it is supposed to be used. Don't specify the syntax you want to use, but use that syntax as it is supposed to be used. Just use:print(a, b)
– Hielke Walinga
Nov 13 '18 at 10:45
|
show 3 more comments
Let us take 2 strings a and b and concatenate them using + using print() function.
a = 'Hello'
b = 'World'
print(a + b, sep = ' ')
# prints HelloWorld
print(a + ' ' + b)
# prints Hello World
I have 2 questions:
a) Can I use sep to add a space between the concatenated strings a and b?
b) If not, then, Is there any other way to add a space between the concatenated strings a and b?
python-3.x
Let us take 2 strings a and b and concatenate them using + using print() function.
a = 'Hello'
b = 'World'
print(a + b, sep = ' ')
# prints HelloWorld
print(a + ' ' + b)
# prints Hello World
I have 2 questions:
a) Can I use sep to add a space between the concatenated strings a and b?
b) If not, then, Is there any other way to add a space between the concatenated strings a and b?
python-3.x
python-3.x
asked Nov 13 '18 at 10:26
Preetkaran SinghPreetkaran Singh
101111
101111
print(a, b, sep=' ')?
– jpp
Nov 13 '18 at 10:27
I want to add space while concatenating the 2 strings
– Preetkaran Singh
Nov 13 '18 at 10:29
...f'{a} {b}'?
– jpp
Nov 13 '18 at 10:29
@jpp, I want to add space while concatenating 2 strings. Your solutions add a space between the strings, but these strings are not concatenated.
– Preetkaran Singh
Nov 13 '18 at 10:33
2
print(*[a]+[b])But please don't do that. Use Python as it is supposed to be used. Don't specify the syntax you want to use, but use that syntax as it is supposed to be used. Just use:print(a, b)
– Hielke Walinga
Nov 13 '18 at 10:45
|
show 3 more comments
print(a, b, sep=' ')?
– jpp
Nov 13 '18 at 10:27
I want to add space while concatenating the 2 strings
– Preetkaran Singh
Nov 13 '18 at 10:29
...f'{a} {b}'?
– jpp
Nov 13 '18 at 10:29
@jpp, I want to add space while concatenating 2 strings. Your solutions add a space between the strings, but these strings are not concatenated.
– Preetkaran Singh
Nov 13 '18 at 10:33
2
print(*[a]+[b])But please don't do that. Use Python as it is supposed to be used. Don't specify the syntax you want to use, but use that syntax as it is supposed to be used. Just use:print(a, b)
– Hielke Walinga
Nov 13 '18 at 10:45
print(a, b, sep=' ') ?– jpp
Nov 13 '18 at 10:27
print(a, b, sep=' ') ?– jpp
Nov 13 '18 at 10:27
I want to add space while concatenating the 2 strings
– Preetkaran Singh
Nov 13 '18 at 10:29
I want to add space while concatenating the 2 strings
– Preetkaran Singh
Nov 13 '18 at 10:29
...
f'{a} {b}' ?– jpp
Nov 13 '18 at 10:29
...
f'{a} {b}' ?– jpp
Nov 13 '18 at 10:29
@jpp, I want to add space while concatenating 2 strings. Your solutions add a space between the strings, but these strings are not concatenated.
– Preetkaran Singh
Nov 13 '18 at 10:33
@jpp, I want to add space while concatenating 2 strings. Your solutions add a space between the strings, but these strings are not concatenated.
– Preetkaran Singh
Nov 13 '18 at 10:33
2
2
print(*[a]+[b]) But please don't do that. Use Python as it is supposed to be used. Don't specify the syntax you want to use, but use that syntax as it is supposed to be used. Just use: print(a, b)– Hielke Walinga
Nov 13 '18 at 10:45
print(*[a]+[b]) But please don't do that. Use Python as it is supposed to be used. Don't specify the syntax you want to use, but use that syntax as it is supposed to be used. Just use: print(a, b)– Hielke Walinga
Nov 13 '18 at 10:45
|
show 3 more comments
1 Answer
1
active
oldest
votes
If you really like to make use of the plus sign to concatenate strings with a delimiter. You can use plus to make a list first and than apply something that will delimit the arguments in the list.
# So, let's make the list first
str_lst = [a] + [b] # you could also do [a, b], but we wanted to make use of the plus sign.
# now we can for example pass this to print and unpack it with *. print delimits by space by default.
print(*str_list) # which is the same as print(str_list[0], str_list[1]) or print(a, b), but that would not make use of the plus sign.
# Or you could use join the concetenate the string.
" ".join(*str_list)
Okay, so hope you learned some new things today. But please don't do it like this. This is not how it meant to be done.
Thanks @HielkeWalinga for your answer, I'm new to python, so wanted to explore it further. I know there are more easier ways to do the same, but I learnt a new way of doing this.
– Preetkaran Singh
Nov 13 '18 at 11:17
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%2f53278899%2fpython-any-other-way-to-add-a-space-between-2-concatenating-strings-using-prin%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
If you really like to make use of the plus sign to concatenate strings with a delimiter. You can use plus to make a list first and than apply something that will delimit the arguments in the list.
# So, let's make the list first
str_lst = [a] + [b] # you could also do [a, b], but we wanted to make use of the plus sign.
# now we can for example pass this to print and unpack it with *. print delimits by space by default.
print(*str_list) # which is the same as print(str_list[0], str_list[1]) or print(a, b), but that would not make use of the plus sign.
# Or you could use join the concetenate the string.
" ".join(*str_list)
Okay, so hope you learned some new things today. But please don't do it like this. This is not how it meant to be done.
Thanks @HielkeWalinga for your answer, I'm new to python, so wanted to explore it further. I know there are more easier ways to do the same, but I learnt a new way of doing this.
– Preetkaran Singh
Nov 13 '18 at 11:17
add a comment |
If you really like to make use of the plus sign to concatenate strings with a delimiter. You can use plus to make a list first and than apply something that will delimit the arguments in the list.
# So, let's make the list first
str_lst = [a] + [b] # you could also do [a, b], but we wanted to make use of the plus sign.
# now we can for example pass this to print and unpack it with *. print delimits by space by default.
print(*str_list) # which is the same as print(str_list[0], str_list[1]) or print(a, b), but that would not make use of the plus sign.
# Or you could use join the concetenate the string.
" ".join(*str_list)
Okay, so hope you learned some new things today. But please don't do it like this. This is not how it meant to be done.
Thanks @HielkeWalinga for your answer, I'm new to python, so wanted to explore it further. I know there are more easier ways to do the same, but I learnt a new way of doing this.
– Preetkaran Singh
Nov 13 '18 at 11:17
add a comment |
If you really like to make use of the plus sign to concatenate strings with a delimiter. You can use plus to make a list first and than apply something that will delimit the arguments in the list.
# So, let's make the list first
str_lst = [a] + [b] # you could also do [a, b], but we wanted to make use of the plus sign.
# now we can for example pass this to print and unpack it with *. print delimits by space by default.
print(*str_list) # which is the same as print(str_list[0], str_list[1]) or print(a, b), but that would not make use of the plus sign.
# Or you could use join the concetenate the string.
" ".join(*str_list)
Okay, so hope you learned some new things today. But please don't do it like this. This is not how it meant to be done.
If you really like to make use of the plus sign to concatenate strings with a delimiter. You can use plus to make a list first and than apply something that will delimit the arguments in the list.
# So, let's make the list first
str_lst = [a] + [b] # you could also do [a, b], but we wanted to make use of the plus sign.
# now we can for example pass this to print and unpack it with *. print delimits by space by default.
print(*str_list) # which is the same as print(str_list[0], str_list[1]) or print(a, b), but that would not make use of the plus sign.
# Or you could use join the concetenate the string.
" ".join(*str_list)
Okay, so hope you learned some new things today. But please don't do it like this. This is not how it meant to be done.
answered Nov 13 '18 at 11:11
Hielke WalingaHielke Walinga
806414
806414
Thanks @HielkeWalinga for your answer, I'm new to python, so wanted to explore it further. I know there are more easier ways to do the same, but I learnt a new way of doing this.
– Preetkaran Singh
Nov 13 '18 at 11:17
add a comment |
Thanks @HielkeWalinga for your answer, I'm new to python, so wanted to explore it further. I know there are more easier ways to do the same, but I learnt a new way of doing this.
– Preetkaran Singh
Nov 13 '18 at 11:17
Thanks @HielkeWalinga for your answer, I'm new to python, so wanted to explore it further. I know there are more easier ways to do the same, but I learnt a new way of doing this.
– Preetkaran Singh
Nov 13 '18 at 11:17
Thanks @HielkeWalinga for your answer, I'm new to python, so wanted to explore it further. I know there are more easier ways to do the same, but I learnt a new way of doing this.
– Preetkaran Singh
Nov 13 '18 at 11:17
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%2f53278899%2fpython-any-other-way-to-add-a-space-between-2-concatenating-strings-using-prin%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
print(a, b, sep=' ')?– jpp
Nov 13 '18 at 10:27
I want to add space while concatenating the 2 strings
– Preetkaran Singh
Nov 13 '18 at 10:29
...
f'{a} {b}'?– jpp
Nov 13 '18 at 10:29
@jpp, I want to add space while concatenating 2 strings. Your solutions add a space between the strings, but these strings are not concatenated.
– Preetkaran Singh
Nov 13 '18 at 10:33
2
print(*[a]+[b])But please don't do that. Use Python as it is supposed to be used. Don't specify the syntax you want to use, but use that syntax as it is supposed to be used. Just use:print(a, b)– Hielke Walinga
Nov 13 '18 at 10:45