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
1# 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.
After this program
If the idea is still fuzzy, read Day 4: Strings and String Methods in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 25 string programs, or see every topic on Python exercises.