Splitting a string is taking the string and then storing it in a list as a list of words.
Joining string is taking a collection and a separator, and joining all values to a string.
Splitting and joining a string
We will take a string and separator as input from the user. Then print a list created by splitting the string into words. Then we will join them back to a string separated by the separator.
For splitting the string into words, we will use the split() method.
Syntax:
string_name.split(delemitor)
For joining the collection of words into a string with a separator, we will use the join() method.
Syntax:
'separator'.join(collection of string)
Algorithm:
- Get the string and separator as input from the user.
- Using the split() method, create a collection of words from the string.
- Using the join() method, create a string with words separated by a separator.
- Print both the values.
Program to split and join a string
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer