Split and Join Strings in Python

Split a sentence into words and join them with a given delimiter.

BeginnerString ProgramsExample 19 of 25
split-and-join-strings.py
Run in browser
1# Program to split and join a string
2
3sentence = input("Enter a sentence: ")
4delimiter = input("Enter delimiter: ")
5
6words = sentence.split()
7joined = delimiter.join(words)
8
9print("Joined string:", joined)

Output

Enter a sentence: split this string
Enter delimiter: -
Joined string: split-this-string

What's going on

We use .split() to break on whitespace and delimiter.join(words) to join with a custom separator.