Hướng dẫn append string loop python

I need to "concatenate to a string in a for loop". To explain, I have this list:

list = ['first', 'second', 'other']

And inside a for loop I need to end with this:

endstring = 'firstsecondother'

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.

>>> ''.join(['first', 'second', 'other'])
'firstsecondother'

is what you want.

If you do it in a for loop, it's going to be inefficient as string "addition"/concatenation doesn't scale well (but of course it's possible):

>>> mylist = ['first', 'second', 'other']
>>> s = ""
>>> for item in mylist:
...    s += item
...
>>> s
'firstsecondother'

answered Nov 23, 2011 at 21:51

Tim PietzckerTim Pietzcker

318k56 gold badges492 silver badges548 bronze badges

8

endstring = ''
for s in list:
    endstring += s

answered Nov 23, 2011 at 21:55

1

If you must, this is how you can do it in a for loop:

mylist = ['first', 'second', 'other']
endstring = ''
for s in mylist:
  endstring += s

but you should consider using join():

''.join(mylist)

answered Nov 23, 2011 at 21:57

Adam ZalcmanAdam Zalcman

26.2k4 gold badges68 silver badges91 bronze badges

1

This should work:

endstring = ''.join(list)

answered Nov 23, 2011 at 21:51

Hướng dẫn append string loop python

Ó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:

endstring = ""
mylist = ['first', 'second', 'other']
for word in mylist:
  print "This is the word I am adding: " + word
  endstring = endstring + word
print "This is the answer I get: " + endstring

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

Hướng dẫn append string loop python

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

  • The While Loop
  • How do you append a string in a for loop in Python?
  • How do you add strings to a string in Python?
  • How do you append a string in the same line in Python?
  • How do you concatenate a string with a comma in Python?

list = ['first', 'second', 'other']

And inside a for loop I need to end with this:

endstring = 'firstsecondother'

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.

>>> ''.join(['first', 'second', 'other'])
'firstsecondother'

is what you want.

If you do it in a for loop, it's going to be inefficient as string "addition"/concatenation doesn't scale well (but of course it's possible):

>>> mylist = ['first', 'second', 'other']
>>> s = ""
>>> for item in mylist:
...    s += item
...
>>> s
'firstsecondother'

answered Nov 23, 2011 at 21:51

Tim PietzckerTim Pietzcker

318k56 gold badges492 silver badges548 bronze badges

8

endstring = ''
for s in list:
    endstring += s

answered Nov 23, 2011 at 21:55

1

If you must, this is how you can do it in a for loop:

mylist = ['first', 'second', 'other']
endstring = ''
for s in mylist:
  endstring += s

but you should consider using join():

''.join(mylist)

answered Nov 23, 2011 at 21:57

Adam ZalcmanAdam Zalcman

26.2k4 gold badges68 silver badges91 bronze badges

1

This should work:

endstring = ''.join(list)

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:

endstring = ""
mylist = ['first', 'second', 'other']
for word in mylist:
  print "This is the word I am adding: " + word
  endstring = endstring + word
print "This is the answer I get: " + endstring

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.

list_of_strings = ['one', 'two', 'three']
my_string = ''

for word in list_of_strings:
    my_string += str(word)

print("Final result:", my_string)

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.

list_of_strings = ['one', 'two', 'three']
my_string = ''

for word in list_of_strings:
    my_string += str(word + ",")

my_string = my_string[:-1]
my_string += '.'
print("Final result:", my_string)

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.

my_string = my_string[:-1]

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.

my_string += '.'

If you run this code, you are going to get this result.

Final result: one,two,three.

The While Loop

If you prefer to use the While loop, you need to create a counting variable and know how many words are there inside the list.

list_of_strings = ['one', 'two', 'three']
my_string = ''

counter = 0

while counter < list_of_strings.__len__():
    my_string += str(list_of_strings[counter] + ",")
    counter += 1

my_string = my_string[:-1]
my_string += '.'
print("Final result:", my_string)

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.

counter += 1

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).