HackerRank Solution: String Split and Join [3 Methods]


Hacker Rank Python

Author: Bashir Alam
Reviewer: Deepak Prasad

Question: Python String Split and Join

In Python, a string can be split on a delimiter.

Example:

>>> a = "this is a string"
>>> a = a.split(" ") # a is converted to a list of strings. 
>>> print a
['this', 'is', 'a', 'string']

Joining a string is simple:

>>> a = "-".join(a)
>>> print a
this-is-a-string 

Task

You are given a string. Split the string on a " " (space) delimiter and join using a - hyphen.

Function Description

Complete the split_and_join function in the editor below.

split_and_join has the following parameters:

  • string line: a string of space-separated words

Returns

string: the resulting string

Input Format

One line contains a string consisting of space-separated words.

Sample Input:

this is a string   

Sample Output:

this-is-a-string

 

Possible solutions

Now, let us solve the problem using possible solutions. The following code is already given in the editor of the hacker rank website.

def split_and_join(line):
    # write your code here

if __name__ == '__main__':
    line = input()
    result = split_and_join(line)
    print(result)

Let us now move toward the solutions:

 

Solution-1: Using join and split function

Let us now use the join and split methods to solve the problem:

def split_and_join(line):
    return "-".join(line.split())

if __name__ == '__main__':
    line = input()
    result = split_and_join(line)
    print(result)

This code defines a function called "split_and_join" that takes in a string (called "line") as its input. Inside the function, the string is first split on the whitespace characters using the "split()" method. This returns a list of substrings, with each substring being a word in the original string. Then, the "join()" method is called on the string "-", and this is used to join the elements of the list of substrings with the "-" character in between them. Finally, the modified string is returned by the function. If the script is run directly, it prompts the user to input a string, assigns the user's input to the variable "line", then calls the "split_and_join" function on "line" and assigns the result to the variable "result", which is then printed to the console.

 

Solution-2: Alternative way

Let us now modify the above code and solve the problem:

def split_and_join(line):
    # write your code here
    a = line
    b = a.split(" ")
    return "-".join(b)

if __name__ == '__main__':
    line = input()
    result = split_and_join(line)
    print(result)

This code is similar to the previous one, but with a small difference in the implementation of the "split_and_join" function. In this version, the string is split using the "split()" method, but with an explicit argument " " (a space character) passed to it, instead of using the default behavior of splitting on whitespace characters. This means that in this version, the function will only split the input string on space characters, and not on any other whitespace characters such as tabs or newlines. The rest of the code is the same as before, where the output of the "split()" method is passed as an argument to the "join()" method, which joins the elements in the list with a "-" character. The final output is the same as before, which is the original string with "-" in between the words.

 

Solution-3: Using replace() method

Let us now solve the problem using the replace() method:

def split_and_join(line):
    line=line.replace(' ','-')
    # write your code here
    return line
if __name__ == '__main__':
    line = input()
    result = split_and_join(line)
    print(result)

Similar to the previous solutions, this solution also defines a function called "split_and_join" that takes in a single input "line" and replaces all spaces in the input with dashes using the string method "replace()". The modified input is then returned by the function. When the code is run as a standalone script (as determined by the if name == 'main': block), it prompts the user to input a line, assigns the input to the variable "line", calls the "split_and_join" function with "line" as the input and assigns the returned value to the variable "result". The final line of the script then prints the value of "result".

In summary, the code takes a string input from the user and replaces all spaces in the input with dashes and prints the modified input.

 

Summary

In this short article, we discussed how we can solve the string split and join problem on hacker rank. We solved the problem using three different solutions.

 

Further Reading

Question on Hacker Rank: Python String split and join [Strings]

 

Bashir Alam

Bashir Alam

He is a Computer Science graduate from the University of Central Asia, currently employed as a full-time Machine Learning Engineer at uExel. His expertise lies in Python, Java, Machine Learning, OCR, text extraction, data preprocessing, and predictive models. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment