Split and Join Strings
Split a sentence into words and join them with a given delimiter.
BeginnerTopic: String Programs
Python Split and Join Strings Program
This program helps you to learn the fundamental structure and syntax of Python programming.
# 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
Understanding Split and Join Strings
We use .split() to break on whitespace and delimiter.join(words) to join with a custom separator.
Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.