Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python program to split and join a string

  • Difficulty Level : Basic
  • Last Updated : 26 Oct, 2018

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.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

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)
     # Join list of strings into one
    new_string = join_string(list_string)

Output :

['Geeks', 'for', 'Geeks']

My Personal Notes arrow_drop_up
Recommended Articles
Page :