Python Practice › String Programs › Program 40

Split and join a string

EasyString Programs · 40 of 106

Write a Python program to split a string into words and join them with “-“.

Advertisement
Input
"Hello world"
Output
['Hello', 'world']
Hello-world

Approach

  1. split() then "-".join().

Solution

Python
parts = "Hello world".split()print(parts)print("-".join(parts))
Output
['Hello', 'world']
Hello-world
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More string programs

← All 106 String Programs