I need to "concatenate to a string in a for loop". To explain, I have this list: Show
And inside a for loop I need to end with this:
Can you give me a clue on how to achieve this in python?
Adam Zalcman 26.2k4 gold badges68 silver badges91 bronze badges asked Nov 23, 2011 at 21:49
2 That's not how you do it.
is what you want. If you do it in a
answered Nov 23, 2011 at 21:51
Tim PietzckerTim Pietzcker 318k56 gold badges492 silver badges548 bronze badges 8
answered Nov 23, 2011 at 21:55
1 If you must, this is how you can do it in a for loop:
but you should consider using
answered Nov 23, 2011 at 21:57
Adam ZalcmanAdam Zalcman 26.2k4 gold badges68 silver badges91 bronze badges 1 This should work:
answered Nov 23, 2011 at 21:51
Óscar LópezÓscar López 227k35 gold badges304 silver badges377 bronze badges 2 While "".join is more pythonic, and the correct answer for this problem, it is indeed possible to use a for loop. If this is a homework assignment (please add a tag if this is so!), and you are required to use a for loop then what will work (although is not pythonic, and shouldn't really be done this way if you are a professional programmer writing python) is this:
You don't need the 'prints', I just threw them in there so you can see what is happening. answered Nov 23, 2011 at 21:57
AuroraAurora 4,3743 gold badges37 silver badges44 bronze badges I need to "concatenate to a string in a for loop". To explain, I have this list: Nội dung chính
And inside a for loop I need to end with this:
Can you give me a clue on how to achieve this in python? Adam Zalcman 26.2k4 gold badges68 silver badges91 bronze badges asked Nov 23, 2011 at 21:49 2 That's not how you do it.
is what you want. If you do it in a
answered Nov 23, 2011 at 21:51 Tim PietzckerTim Pietzcker 318k56 gold badges492 silver badges548 bronze badges 8
answered Nov 23, 2011 at 21:55 1 If you must, this is how you can do it in a for loop:
but you should consider using
answered Nov 23, 2011 at 21:57 Adam ZalcmanAdam Zalcman 26.2k4 gold badges68 silver badges91 bronze badges 1 This should work:
answered Nov 23, 2011 at 21:51 Óscar LópezÓscar López 227k35 gold badges303 silver badges377 bronze badges 2 While "".join is more pythonic, and the correct answer for this problem, it is indeed possible to use a for loop. If this is a homework assignment (please add a tag if this is so!), and you are required to use a for loop then what will work (although is not pythonic, and shouldn't really be done this way if you are a professional programmer writing python) is this:
You don't need the 'prints', I just threw them in there so you can see what is happening. answered Nov 23, 2011 at 21:57 AuroraAurora 4,3743 gold badges37 silver badges44 bronze badges If you have a list of strings and you want to concatenate them to create a single string made of these elements, you can use the For loop.
With each pass of the loop, the next word is added to the end of the string. The result is a single word. Final result: onetwothree The problem with this approach is that there are no separators between characters. We can easily fix that inside the loop. We are going to separate words with a comma.
Now, with each pass, there is a word and comma added to the end of the string. Before printing the result, we have to remove the comma at the end of the string, that was added in the last pass.
This code assigns the my_string variable without the last character (which is a comma) to itself. At the end of the string, we are going to add a dot.
If you run this code, you are going to get this result. Final result: one,two,three. The While LoopIf you prefer to use the While loop, you need to create a counting variable and know how many words are there inside the list.
This loop continues to meet the requirement when the counter variable is lower than the number of words inside the list. Each time the counter variable is incremented.
This code returns the same result as before: Final result: one,two,three.
How do you append a string in a for loop in Python?To concatenate strings we will use for loop, and the “+ ” operator is the most common way to concatenate strings in python. To get the output, I have used print(my_str). How do you add strings to a string in Python?Use the + operator. str1="Hello". str2="World". print ("String 1:",str1). print ("String 2:",str2). str=str1+str2.. print("Concatenated two different strings:",str). How do you append a string in the same line in Python?+= operator You can append another string to a string with the in-place operator, += . The string on the right is concatenated after the string variable on the left. If you want to add a string to the end of a string variable, use the += operator. How do you concatenate a string with a comma in Python?String Concatenation in Python. Using + operator.. Using join() method.. Using % operator.. Using format() function.. Using , (comma). |