String Split and Join in Python - HackerRank Solution

String Split and Join - Python HackerRank Solution

Hello Friends, How are you? Today I am going to solve the HackerRank String Split and Join Problem in Python with a very easy explanation. In this article, you will get one or more approaches to solving this problem. So let's start-

{tocify} $title={Table of Contents}

Default Arguments Python HackerRank Solution

HackerRank Python String Split and Join Solution - Problem Statement

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

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

Joining a string is simple:

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

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

The one line contains a string consisting of space separated words.

Sample Input 0

this is a string {codeBox}

Sample Output 0

this-is-a-string {codeBox}

Python String Split and Join - Hacker Rank Solution

Approach I: String Split and Join HackerRank Python Solution

 # ========================
#       Information
# ========================

# Name: String Split and Join in Python - HackerRank Solution
# Direct Link: https://www.hackerrank.com/challenges/python-string-split-and-join/problem
# Difficulty: Easy
# Max Score: 10
# Language: Pypy 3

# ========================
#         Solution Start
# ========================

#String Split and Join in Python - Hacker Rank Solution Start
   
def split_and_join(line):
    Output = line.split();
    Output = "-".join(Output)
    return Output;
    
if __name__ == '__main__':
    line = input()
    result = split_and_join(line)
    print(result)
   
#String Split and Join in Python - Hacker Rank Solution END
# MyEduWaves


Disclaimer: The above Problem ( Python String Split and Join ) is generated by Hackerrank but the Solution is Provided by MyEduWaves. This tutorial is only for Educational and Learning purposes. Authority if any queries regarding this post or website fill out the contact form.

I hope you have understood the solution to this HackerRank Problem. All these three solutions will pass all the test cases. Now visit Python String Split and Join Hackerrank Problem and try to solve it again.

All the Best!

Post a Comment

Previous Post Next Post