String Split and Join in Python

String Split and Join in Python

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} 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}

You are given a string. Split the string on a ” ” (space) delimiter and join using a – hyphen. 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
  • string: the resulting string

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

this is a string {codeBox}

this-is-a-string {codeBox}

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
No Comments

Sorry, the comment form is closed at this time.