Lesson 06
split() and join()
Learn how to break a string into a list with split() and rebuild it into a string using join() through visual diagrams and real-world examples.
What you'll learn
| Topic | Focus |
|---|---|
split() |
Convert String → List |
| Custom delimiters | -, #, $, / |
| Date parsing | Extract day, month & year |
join() |
Convert List → String |
Visual roadmap
These two methods are opposites.
Golden rule:
split()breaks ·join()builds
1. split() → Default (spaces)
By default, split() separates words using whitespace.
s = "Python is an oop lang"
words = s.split()
print(words)
# ['Python', 'is', 'an', 'oop', 'lang']
print(len(words))
# 52. split(delimiter)
Provide a delimiter to split on a specific character.
dob = "08-07-2023"
parts = dob.split("-")
print(parts)
# ['08', '07', '2023']
print(parts[0]) # Day
print(parts[1]) # Month
print(parts[2]) # Year3. Different delimiters
You can split using any separator.
s = "Apple#Banana#Kiwi"
print(s.split("#"))
# ['Apple', 'Banana', 'Kiwi']
s = "Apple/Banana/Kiwi"
print(s.split("/"))
# ['Apple', 'Banana', 'Kiwi']4. Trailing delimiter
A separator at the end creates an empty last item.
s = "123$456$678$"
print(s.split("$"))
# ['123', '456', '678', '']Warning: The last
''represents nothing after the final delimiter.
5. Nested splitting
Sometimes data contains multiple separators.
s = "Apple#Mango-Kiwi#Guava"
x = s.split("#")
print(x)
y = x[1].split("-")
print(y)Output:
['Apple', 'Mango-Kiwi', 'Guava']
['Mango', 'Kiwi']6. join() → Build a string
join() combines list or tuple items into one string.
lst = ["Python", "is", "an", "oop", "lang"]
sentence = " ".join(lst)
print(sentence)
# Python is an oop langMemory:
join()= glue between list items
7. Different separators with join()
The string before .join() becomes the separator.
cities = ["HYD", "BANG", "AP"]
print("".join(cities))
# HYDBANGAP
print(" ".join(cities))
# HYD BANG APComma separated:
print(",".join(cities))
# HYD,BANG,AP8. join() works with tuples
Any iterable works — not just lists.
t = ("Rossum", "is", "Father", "of", "Python")
print(" ".join(t))
# Rossum is Father of Python9. split() ↔ join() relationship
These methods are exact opposites.
| Direction | Method | Returns |
|---|---|---|
| String → Parts | split() |
List |
| Parts → String | join() |
String |
Playground example
s = "Python is an oop lang"
words = s.split()
print(words, len(words))
dob = "08-07-2023"
parts = dob.split("-")
print("Day=", parts[0])
print("Month=", parts[1])
print("Year=", parts[2])
lst = ["Python", "is", "an", "oop", "lang"]
sentence = " ".join(lst)
print(sentence)Expected output:
['Python', 'is', 'an', 'oop', 'lang'] 5
Day= 08
Month= 07
Year= 2023
Python is an oop langUse the lesson playground at the bottom of the page to run this code live.
Quick knowledge check
-
What is the default delimiter for
split()?
Answer: Whitespace (spaces, tabs, newlines) -
What does
"06-05-2024".split("-")return?
Answer:['06', '05', '2024'] -
How do you join
['a', 'b', 'c']with commas?
Answer:
",".join(["a", "b", "c"])
# a,b,c30-second visual revision
| Method | Easy memory |
|---|---|
split() |
Break a string into pieces |
split("-") |
Parse dates |
join() |
Glue list items together |
" ".join() |
Build readable sentences |
Interview shortcut: Split = String → List · Join = List → String · The string before join() is the separator.
Next: startswith() and endswith().
Loading playground…