Split and Join Strings

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

PythonBeginner
Python
# Program to split and join a string

sentence = input("Enter a sentence: ")
delimiter = input("Enter delimiter: ")

words = sentence.split()
joined = delimiter.join(words)

print("Joined string:", joined)

Output

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

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