Python program to split a string and join with comma

PythonServer Side ProgrammingProgramming

Suppose we have few words that are separated by spaces. We have to split these words to form a list, then join them into a string by placing comma in-between.

So, if the input is like s = "Programming Python Language Easy Funny", then the output will be Programming, Python, Language, Easy, Funny

To solve this, we will follow these steps −

  • words := a list of words by applying split function on s with delimiter " " blank space.

  • ret := join each items present in words and place ", " in between each pair of words

  • return ret

Example

Let us see the following implementation to get better understanding

def solve(s):
   words = s.split(' ')
   ret = ', '.join(words)
   return ret

s = "Programming Python Language Easy Funny"
print(solve(s))

Input

"Programming Python Language Easy Funny"

Output

Programming, Python, Language, Easy, Funny
raja
Updated on 11-Oct-2021 10:31:58

Advertisements