Python program to Split a string based on a delimiter and join the string using another delimiter.
Split a string can be quite useful sometimes, especially when you need only certain parts of strings. A simple yet effective example is splitting the First-name and Last-name of a person. Another application is CSV(Comma Separated Files). We use split to get data from CSV and join to write data to CSV.
In Python, we can use the function split() to split a string and join() to join a string. For detailed article on split() and join() functions, refer these : split() in Python and join() in Python.
Examples :
Split the string into list of strings
Input : Geeks for Geeks
Output : ['Geeks', 'for', 'Geeks']
Join the list of strings into a string based on delimiter ('-')
Input : ['Geeks', 'for', 'Geeks']
Output : Geeks-for-Geeks
Below is Python code to Split and Join the string based on a delimiter :
# Python program to split a string and # join it using different delimiter def split_string(string): # Split the string based on space delimiter list_string = string.split(' ') return list_string def join_string(list_string): # Join the string based on '-' delimiter string = '-'.join(list_string) return string # Driver Function if __name__ == '__main__': string = 'Geeks for Geeks' # Splitting a string list_string = split_string(string) print(list_string) # Join list of strings into one new_string = join_string(list_string) print(new_string) |
Output :
['Geeks', 'for', 'Geeks'] Geeks-for-Geeks
Recommended Posts:
- Python Program to perform cross join in Pandas
- Python Program to Split the array and add the first part to the end
- Python | Split strings and digits from string list
- join() function in Python
- Python | Join tuple elements in a list
- Python | Ways to join pair of elements in list
- Python | Join cycle in list
- Python - Consecutive K elements join in List
- Python - Join Tuples to Integers in Tuple List
- Python - Join Tuples if similar initial element
- Python - Cross Join every Kth segment
- Python String | split()
- Python | Split given string into equal halves
- Split a string in equal parts (grouper in Python)
- Python | Split string into list of characters
- Python | Split string in groups of n consecutive characters
- Python | String Split including spaces
- Python | Split CamelCase string to individual strings
- Python | Split string on Kth Occurrence of Character
- Python | Ways to split a string in different ways
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

