Python program to split and join a string

12 Jun, 2022
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 : 
Python3
# 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 Functionif __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)
['Geeks', 'for', 'Geeks']
Geeks-for-Geeks

Method: In Python, we can use the function split() to split a string and join() to join a string. the split() method in Python split a string into a list of strings after breaking the given string by the specified separator. Python String join() method is a string method and returns a string in which the elements of the sequence have been joined by the str separator. 
Python3
# Python code# to split and join given string # input strings = 'Geeks for Geeks'# print the string after split methodprint(s.split(" "))# print the string after join methodprint("-".join(s.split()))  # this code is contributed by gangarajula laxmi
['Geeks', 'for', 'Geeks']
Geeks-for-Geeks

Python Programming Tutorial | Program to split and join a string | GeeksforGeeks - YouTubeGeeksforGeeks529K subscribersPython Programming Tutorial | Program to split and join a string | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.More videosMore videosYou're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 2:02•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=aWab32mDz9Y" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
&t=1s
laxmigangarajula03
Python string-programs
python-string
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
Python program to convert a list to string
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Convert a list to dictionary
Python | Convert string dictionary to dictionary