str.split() / str.join()
Methods for splitting a string into a list using a delimiter, or joining list elements into a string.
Syntax
# Splits a string by the delimiter and returns a list. str.split(sep=None, maxsplit=-1) # Splits from the right. The difference appears when maxsplit is specified. str.rsplit(sep=None, maxsplit=-1) # Joins each element of an iterable with the separator string and returns a string. separator_str.join(iterable)
Method List
| Method | Description |
|---|---|
| str.split(sep=None, maxsplit=-1) | Returns a list of the string split by sep. If sep is omitted, splits on whitespace (spaces, tabs, and newlines). |
| str.rsplit(sep=None, maxsplit=-1) | Splits from the right. When maxsplit is specified, cuts from the opposite direction compared to split(). |
| sep.join(iterable) | Converts each element of the iterable to a string and concatenates them with the separator. All elements must be strings. |
Sample Code
# Split a string with split().
sentence = "Python is fun and powerful"
words = sentence.split() # Split on whitespace.
print(words) # ['Python', 'is', 'fun', 'and', 'powerful']
# Split with a specific delimiter.
csv_line = "Alice,20,Tokyo,Engineer"
fields = csv_line.split(",")
print(fields) # ['Alice', '20', 'Tokyo', 'Engineer']
# Limit the number of splits with maxsplit.
text = "a:b:c:d:e"
print(text.split(":", 2)) # ['a', 'b', 'c:d:e'] (split at most 2 times)
# Extract the filename from a path.
path = "/home/user/documents/file.txt"
parts = path.split("/")
print(parts[-1]) # file.txt (the last element is the filename)
# Split from the right with rsplit().
print(text.rsplit(":", 2)) # ['a:b:c', 'd', 'e'] (split at most 2 times from the right)
# Practical example: separate a file extension.
filename = "photo.2026.jpg"
name, ext = filename.rsplit(".", 1) # Split only once from the right.
print(name) # photo.2026
print(ext) # jpg
# Use join() to combine a list into a string.
words2 = ["Python", "is", "fun"]
result = " ".join(words2) # Join with a space.
print(result) # Python is fun
# Build a CSV line.
data = ["Alice", "25", "Osaka"]
csv = ",".join(data)
print(csv) # Alice,25,Osaka
# Join lines with newlines to build a block of text.
lines = ["Line 1", "Line 2", "Line 3"]
text = "\n".join(lines)
print(text)
# Line 1
# Line 2
# Line 3
# join() requires all elements to be strings. Convert numbers first.
nums = [1, 2, 3, 4, 5]
print("-".join(str(n) for n in nums)) # 1-2-3-4-5
# Normalize whitespace by combining split() and join().
messy = " Python is fun "
normalized = " ".join(messy.split())
print(normalized) # Python is fun
Notes
When the argument to split() is omitted (or set to None), consecutive whitespace characters are treated as a single delimiter, and leading and trailing whitespace is also removed. Passing an empty string as the delimiter raises a ValueError.
With join(), it is Python convention to write the separator first. This may feel counterintuitive at first, but it has advantages — for example, you can concatenate elements with no separator by writing "".join(parts). All elements passed to join() must be strings. If the iterable contains integers or other non-string types, a TypeError is raised, so convert them with str() beforehand.
For string replacement, see str.replace(). For trimming whitespace, see str.strip().
If you find any errors or copyright issues, please contact us.