Q:

Python program to split and join a string

belongs to collection: Python String Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to split and join a string

# Python program to split and join string 

# Getting input from user 
myStr =  input('Enter the string : ')
separator = input('Enter the separator : ')

# splitting and joining string 
splitColl = myStr.split(' ')
joinedString = separator.join(splitColl)
		
# printing values
print(splitColl)
print(joinedString)

Output:

Enter the string : learn python programming at include help
Enter the separator : /
['learn', 'python', 'programming', 'at', 'include', 'help']
learn/python/programming/at/include/help

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python String Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to find words which are greater tha... >>
<< Python program to find the least frequent characte...