I need hepl with spliting a string

37 Views Asked by At

I have a problem with python It is really easy but I still can find a solution for that:

I have a string. I split it. I have a for cycle

string = "Super best good string"
temp = string.split()
for finished_string in temp:
   number = 0
   finished_string.append(temp[number])
   number += 1

The out of this should be: "Super+best+good+string+"

but This solution must be good for a different length of string with 2 words or 3 words

2

There are 2 best solutions below

2
Cameron On
string = "Super best good string"
temp = string.split()
new_string = ""
for finished_string in temp:
    new_string += finished_string + "+"
0
Eli Harold On

This code doesn't work for a few reasons, like finished_string.append(temp[number]) is appending something to itself in effect.

Instead use this:

string = "Super best good string"
finished_string = "+".join(string.split()) + "+"
print(finished_string)