Python program to split and join a string?



We'll learn how to split and join a string in Python in this article. Python defines strings as a collection of characters enclosed in one, two, or three quotations.

When a string is split, it is divided into smaller strings using a specific delimiter. A set of one or more characters used to define a boundary is known as a delimiter. Anything could serve as a delimiter.

The comma (,), semicolon (;), tab (t), space (), and pipe (|) are the most widely used delimiters. Everywhere the specified delimiter appears in a given string, we must split it, and then we must connect the substrings together.

Input-Output Scenario

Following is an input and its output scenario to demonstrate the split and join of a string in Python −

# Splitting the strings into list of strings
Input: Welcome to TutorialsPoint
Output : [‘Welcome’, ‘to’, ‘TutorialsPoint’]

# Using the delimiter ('-'), join the list of strings into a single string.
Input: Welcome to TutorialsPoint
Output : [Welcome-to-TutorialsPoint]

As we can see that at first the string is splitted into the list of strings and then combined with the help of the delimiter.

Using split() and join() methods

The split() method will break the given string by the specified separator and return a list of strings.

The str separator is used by the join() method to connect the elements of a sequence into a string.

Algorithm

Following is an approach on how to split and join a string −

  • Create a function that will split a string when it is passed in.

  • Declare a variable in the function that will hold the list of substrings returned by split ().

  • Returning variable.

  • Create a different function that joins a list of substrings together to form a string.

  • Declare a string variable in the function that will hold the joined string.

  • Declare a character string.

  • Pass the string when calling the defined split function.

  • Print the value that the function returned.

  • Pass the list when calling the defined join function.

  • Print the value that the function returned.

Example

Following is an example to split and join a string where two functions, one for string splitting and the other for string joining, have been defined −

def splitting(str): # Split based on the space delimiter list = str.split(' ') return list def joining(list): # Join based on the '-' delimiter str = '-'.join(list) return str str = 'Once in a blue moon' # Split the string list = splitting(str) print("The string after splitting is: ",list) # Joining the list of strings into one restoring = joining(list) print("The string after joining is: ",restoring)

Output

Following is an output of the above code −

The string after splitting is:  ['Once', 'in', 'a', 'blue', 'moon']
The string after joining is:  Once-in-a-blue-moon

Example

Obtain the input of the string and separator. Create a list of words from the string using the split() method. Make a string with words in it that are separated by a separator using the join() method. The values should then be printed as shown in the following example −

String = 'Once in a blue moon' Separator = '-' # split and join the string splitting = String.split(' ') joining = Separator.join(splitting) # print the values print('The string after splitting is :',splitting) print('The string after performing join is:',joining)

Output

Following is an output of the above code −

The string after splitting is : ['Once', 'in', 'a', 'blue', 'moon']
The string after performing join is: Once-in-a-blue-moon

Advertisements