How do I add a new line to a string in python
I have this code
def fullview():
rows = 3
elems_in_row = 4
List = ['-'] * rows
for i in range(rows):
List[i] = ['-'] * elems_in_row
for i in List:
elements = ''.join(i)
fullview = str(rows)+ ':' + elements
rows -= 1
return fullview
When i add a line to fullview it removes the previous line added to it.
My desired result is:
fullview = """
3:----
2:----
1:----
"""
Im not sure how to add the new line correctly to the string since += dont work.
python string list printing
add a comment |
I have this code
def fullview():
rows = 3
elems_in_row = 4
List = ['-'] * rows
for i in range(rows):
List[i] = ['-'] * elems_in_row
for i in List:
elements = ''.join(i)
fullview = str(rows)+ ':' + elements
rows -= 1
return fullview
When i add a line to fullview it removes the previous line added to it.
My desired result is:
fullview = """
3:----
2:----
1:----
"""
Im not sure how to add the new line correctly to the string since += dont work.
python string list printing
2
BTW, it's not a good idea to use the name of a function for a variable inside the function. It won't hurt anything in your code, but it would if your function were recursive. But the main reason not to do it is that it makes the code confusing to read.
– PM 2Ring
Nov 23 '18 at 12:53
add a comment |
I have this code
def fullview():
rows = 3
elems_in_row = 4
List = ['-'] * rows
for i in range(rows):
List[i] = ['-'] * elems_in_row
for i in List:
elements = ''.join(i)
fullview = str(rows)+ ':' + elements
rows -= 1
return fullview
When i add a line to fullview it removes the previous line added to it.
My desired result is:
fullview = """
3:----
2:----
1:----
"""
Im not sure how to add the new line correctly to the string since += dont work.
python string list printing
I have this code
def fullview():
rows = 3
elems_in_row = 4
List = ['-'] * rows
for i in range(rows):
List[i] = ['-'] * elems_in_row
for i in List:
elements = ''.join(i)
fullview = str(rows)+ ':' + elements
rows -= 1
return fullview
When i add a line to fullview it removes the previous line added to it.
My desired result is:
fullview = """
3:----
2:----
1:----
"""
Im not sure how to add the new line correctly to the string since += dont work.
python string list printing
python string list printing
edited Nov 23 '18 at 13:03
khelwood
30.1k74162
30.1k74162
asked Nov 23 '18 at 12:39
monkey D
11
11
2
BTW, it's not a good idea to use the name of a function for a variable inside the function. It won't hurt anything in your code, but it would if your function were recursive. But the main reason not to do it is that it makes the code confusing to read.
– PM 2Ring
Nov 23 '18 at 12:53
add a comment |
2
BTW, it's not a good idea to use the name of a function for a variable inside the function. It won't hurt anything in your code, but it would if your function were recursive. But the main reason not to do it is that it makes the code confusing to read.
– PM 2Ring
Nov 23 '18 at 12:53
2
2
BTW, it's not a good idea to use the name of a function for a variable inside the function. It won't hurt anything in your code, but it would if your function were recursive. But the main reason not to do it is that it makes the code confusing to read.
– PM 2Ring
Nov 23 '18 at 12:53
BTW, it's not a good idea to use the name of a function for a variable inside the function. It won't hurt anything in your code, but it would if your function were recursive. But the main reason not to do it is that it makes the code confusing to read.
– PM 2Ring
Nov 23 '18 at 12:53
add a comment |
2 Answers
2
active
oldest
votes
This can be done a single construction, instead of multiple steps:
result = 'n'.join(['{}:{}'.format(i, '-' * elements_in_row)
for i in range(1, rows + 1)][::-1])
One reason the original code did not work is that the line updating fullview
did not take into account the previous state:
fullview = str(rows)+ ':' + elements
add a comment |
Just use 'n'
def fullview():
rows = 3
elems_in_row = 4
fullview_str = ''
for i in range(rows, 0, -1):
fullview_str += "{}:{}n".format(i, '-' * elems_in_row)
return fullview_str
Example output:
3:----
2:----
1:----
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%2f53446896%2fhow-do-i-add-a-new-line-to-a-string-in-python%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
This can be done a single construction, instead of multiple steps:
result = 'n'.join(['{}:{}'.format(i, '-' * elements_in_row)
for i in range(1, rows + 1)][::-1])
One reason the original code did not work is that the line updating fullview
did not take into account the previous state:
fullview = str(rows)+ ':' + elements
add a comment |
This can be done a single construction, instead of multiple steps:
result = 'n'.join(['{}:{}'.format(i, '-' * elements_in_row)
for i in range(1, rows + 1)][::-1])
One reason the original code did not work is that the line updating fullview
did not take into account the previous state:
fullview = str(rows)+ ':' + elements
add a comment |
This can be done a single construction, instead of multiple steps:
result = 'n'.join(['{}:{}'.format(i, '-' * elements_in_row)
for i in range(1, rows + 1)][::-1])
One reason the original code did not work is that the line updating fullview
did not take into account the previous state:
fullview = str(rows)+ ':' + elements
This can be done a single construction, instead of multiple steps:
result = 'n'.join(['{}:{}'.format(i, '-' * elements_in_row)
for i in range(1, rows + 1)][::-1])
One reason the original code did not work is that the line updating fullview
did not take into account the previous state:
fullview = str(rows)+ ':' + elements
answered Nov 23 '18 at 12:46
Elisha
19.5k45069
19.5k45069
add a comment |
add a comment |
Just use 'n'
def fullview():
rows = 3
elems_in_row = 4
fullview_str = ''
for i in range(rows, 0, -1):
fullview_str += "{}:{}n".format(i, '-' * elems_in_row)
return fullview_str
Example output:
3:----
2:----
1:----
add a comment |
Just use 'n'
def fullview():
rows = 3
elems_in_row = 4
fullview_str = ''
for i in range(rows, 0, -1):
fullview_str += "{}:{}n".format(i, '-' * elems_in_row)
return fullview_str
Example output:
3:----
2:----
1:----
add a comment |
Just use 'n'
def fullview():
rows = 3
elems_in_row = 4
fullview_str = ''
for i in range(rows, 0, -1):
fullview_str += "{}:{}n".format(i, '-' * elems_in_row)
return fullview_str
Example output:
3:----
2:----
1:----
Just use 'n'
def fullview():
rows = 3
elems_in_row = 4
fullview_str = ''
for i in range(rows, 0, -1):
fullview_str += "{}:{}n".format(i, '-' * elems_in_row)
return fullview_str
Example output:
3:----
2:----
1:----
answered Nov 23 '18 at 12:47
Ihor Voronin
1876
1876
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%2f53446896%2fhow-do-i-add-a-new-line-to-a-string-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
2
BTW, it's not a good idea to use the name of a function for a variable inside the function. It won't hurt anything in your code, but it would if your function were recursive. But the main reason not to do it is that it makes the code confusing to read.
– PM 2Ring
Nov 23 '18 at 12:53