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 browser1# Program to split and join a string23sentence = input("Enter a sentence: ")4delimiter = input("Enter delimiter: ")56words = sentence.split()7joined = delimiter.join(words)89print("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.