Table of Contents
Introduction
The task is to split the given string and also join the strings based on the delimiter (here we are using space delimiter for splitting and ‘ – ‘ for joining) .
In machine learning, we often come into the scenario where we have to specifically select some string. Here splitting comes into play. We can take simple example; splitting the address into multiple format (house number, street address, area, city, state , pin code). And use join to get the complete address.
Program
def str_split(input_str): # Splitting string using space delimiter output_split = input_str.split(' ') return output_split def str_join(output_split): # Joining string using '-' delimiter output_join = '-'.join(output_split) return output_join input_str = input("Enter the string : ") # Splitting string output_split = str_split(input_str) print("Splitting string: ",output_split) #Joining strings output_join = str_join(output_split) print("Joining string :", output_join)
Output
Explanation
In the above python code we have used split() and join() to split and join the strings respectively. We have created two separate functions for this. The input_str is passed as argument to the function str_split and the output is stored in variable output_split. The split string is printed on the screen.
Now, we passed the split string ( output_split) variable as the argument to function str_join to join the strings using delimited. The joined string is returned to the variable output_join and is printed on the screen.
0 Comments