Timeline for answer to Python - How to concatenate to a string in a for loop? by Tim Pietzcker
Current License: CC BY-SA 3.0
Post Revisions
10 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Feb 16, 2020 at 3:18 | comment | added | Gringo Suave | Have a results list to append to, then join() it at the end. | |
| Feb 11, 2019 at 6:18 | comment | added | Tim Pietzcker |
@steven: ' + '.join(['first', 'second', 'other'])
|
|
| Feb 11, 2019 at 3:09 | comment | added | steven | Is there any pythonic way to return 'first + second + other'? | |
| Jul 19, 2018 at 5:22 | comment | added | Tim Pietzcker | As long as it contains only strings, yes. See the docs: docs.python.org/3/library/stdtypes.html#str.join | |
| Jul 19, 2018 at 2:18 | comment | added | oldboy | does the join method work on ANY iterable? | |
| Nov 24, 2011 at 8:04 | vote | accept | André | ||
| Nov 23, 2011 at 21:55 | comment | added | André | Thanks for your reply. It is that piece of code that I was looking for. | |
| Nov 23, 2011 at 21:54 | comment | added | user395760 | @André Depending on the logic you need (some transformation to the elements? some unrelated side effects?), it may make sense to pull the string construction out of the loop or create the transformed elements and then apply this to concat them. Doing this naively in a loop can be very bad for performance (as in, quadratic slowdown = add one string, need twice the same; it's not a good habit to get into). | |
| Nov 23, 2011 at 21:52 | comment | added | André | I need to do it in a for loop, I need to add some logic inside a for loop. It is possible to do that inside a for loop? | |
| Nov 23, 2011 at 21:51 | history | answered | Tim Pietzcker | CC BY-SA 3.0 |