Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Split and join a string in Python
Next article icon

Split and join a string in Python

Last Updated : 01 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The goal here is to split a string into smaller parts based on a delimiter and then join those parts back together with a different delimiter. For example, given the string "Hello, how are you?", you might want to split it by spaces to get a list of individual words and then join them back together with a different delimiter like a hyphen (-) to create a formatted string. The result would be "Hello,-how-are-you?".

Using split() and join()

split() function divides the string into a list of words and join() reassembles them with a specified separator. It is highly efficient and commonly used in Python for basic string manipulations.

Python
a = "Hello, how are you?" b = a.split()  # Split by space c = "-".join(b)  # Join with hyphen  print(b) print(c) 

Output
['Hello,', 'how', 'are', 'you?'] Hello,-how-are-you? 

Explanation:

  • split() divides the string into a list of words, split by spaces by default.
  • "-".join(b) reassembles the list into a string with hyphens between the words.

Using re.split() and '-'.join()

In cases needing advanced splitting e.g., handling multiple spaces or different delimiters, re.split() from the re module offers more flexibility. However, it’s less efficient than split() for simple cases due to the overhead of regular expression processing.

Python
import re a = "Hello, how are you?"  b = re.split(r'\s+', a) # Split by spaces c = "-".join(b) # Join with a hyphen  print(b) print(c) 

Output
['Hello,', 'how', 'are', 'you?'] Hello,-how-are-you? 

Explanation:

  • re.split(r'\s+', a) split the string by one or more spaces, providing more flexibility for advanced splitting.
  • "-".join(b) joins the list of words with hyphens.

Using str.partition() and str.replace()

This method splits the string manually using partition(), iterating over it to separate head and tail parts. After splitting, replace() or manual manipulation joins the parts. While functional, it’s less efficient due to multiple iterations and extra logic.

Python
a = "Hello, how are you?" words, rem = [], a  # Split by space manually while rem:     head, _, rem = rem.partition(" ")     if head: words.append(head)  c = "-".join(words) # Join with hyphen print(words) print(c) 

Output
['Hello,', 'how', 'are', 'you?'] Hello,-how-are-you? 

Explanation:

  • partition(" ") splits the string into three parts, before the first space (head), the space itself (sep) and after the space (tail). This process repeats until the string is fully split.
  • Loop ensures all words are extracted and added to the list words.

Using str.split() with list comprehension

This method splits the string with split() and uses a list comprehension to process or filter the list. While more compact and readable, it adds an extra step, reducing efficiency compared to using split() and join() directly.

Python
a = "Hello, how are you?"  # Split by space b = [word for word in a.split()]  # Join with a hyphen c = "-".join(b)  print(b) print(c) 

Output
['Hello,', 'how', 'are', 'you?'] Hello,-how-are-you? 

Explanation:

  • [word for word in a.split()] splits a by spaces into a list of words .
  • "-".join(b) joins the words in b with hyphens.

Related Articles

  • split()
  • join()
  • re.split()
  • str.partition()
  • str.replace()
  • List comprehension

Next Article
Split and join a string in Python

S

shubham_rana_77
Improve
Article Tags :
  • Technical Scripter
  • Python
  • Python Programs
  • DSA
  • python-string
  • Python string-programs
Practice Tags :
  • python

Similar Reads

    Split a String by a Delimiter in Python
    In Python Programming, the split() method is used to split a string into a list of substrings which is based on the specified delimiter. This method takes the delimiter as an argument and returns the list of substrings. Along with this method, we can use various approaches wot split a string by the
    2 min read
    Python | Exceptional Split in String
    Sometimes, while working with Strings, we may need to perform the split operation. The straightforward split is easy. But sometimes, we may have a problem in which we need to perform split on certain characters but have exceptions. This discusses split on comma, with the exception that comma should
    4 min read
    Python - Split String on all punctuations
    Given a String, Split the String on all the punctuations. Input : test_str = 'geeksforgeeks! is-best' Output : ['geeksforgeeks', '!', 'is', '-', 'best'] Explanation : Splits on '!' and '-'. Input : test_str = 'geek-sfo, rgeeks! is-best' Output : ['geek', '-', 'sfo', ', ', 'rgeeks', '!', 'is', '-', '
    3 min read
    Python - Splitting Text and Number in string
    Given a string containing both letters and numbers, the task is to separate the text (letters) and the numbers into two separate outputs. For example, if the input is "abc123", the output should be "abc" and "123". Let's discuss various ways in which we can achieve this in Python.Using for loopThis
    3 min read
    Python | Split flatten String List
    Sometimes, while working with Python Strings, we can have problem in which we need to perform the split of strings on a particular deliminator. In this, we might need to flatten this to a single String List. Let's discuss certain ways in which this task can be performed. Method #1 : Using list compr
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences