str.split() / str.join()
| Since: | Python 2(2000) |
|---|
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_basic.py
sentence = "Python is fun and powerful"
words = sentence.split()
print(words)
csv_line = "Okabe Rintaro,18,Future Gadget Lab,Researcher"
fields = csv_line.split(",")
print(fields)
text = "a:b:c:d:e"
print(text.split(":", 2))
path = "/home/user/documents/file.txt"
parts = path.split("/")
print(parts[-1])
print(text.rsplit(":", 2))
filename = "photo.2026.jpg"
name, ext = filename.rsplit(".", 1)
print(name)
print(ext)
Running the code produces the following output:
python3 split_basic.py ['Python', 'is', 'fun', 'and', 'powerful'] ['Okabe Rintaro', '18', 'Future Gadget Lab', 'Researcher'] ['a', 'b', 'c:d:e'] file.txt ['a:b:c', 'd', 'e'] photo.2026 jpg
join_basic.py
words = ["Python", "is", "fun"]
result = " ".join(words)
print(result)
data = ["Okabe Rintaro", "18", "Future Gadget Lab"]
csv = ",".join(data)
print(csv)
lines = ["Line 1", "Line 2", "Line 3"]
text = "\n".join(lines)
print(text)
nums = [1, 2, 3, 4, 5]
print("-".join(str(n) for n in nums))
Running the code produces the following output:
python3 join_basic.py Python is fun Okabe Rintaro,18,Future Gadget Lab Line 1 Line 2 Line 3 1-2-3-4-5
split_join_normalize.py
messy = " Python is fun "
normalized = " ".join(messy.split())
print(normalized)
members = ["Okabe Rintaro", "Shiina Mayuri", "Makise Kurisu", "Amane Suzuha", "Kiryu Moeka"]
print(", ".join(members))
log_parts = ["2026-04-06", "INFO", "Server started"]
log_line = " | ".join(log_parts)
print(log_line)
Running the code produces the following output:
python3 split_join_normalize.py Python is fun Okabe Rintaro, Shiina Mayuri, Makise Kurisu, Amane Suzuha, Kiryu Moeka 2026-04-06 | INFO | Server started
Common Mistakes
Common Mistake 1: Empty strings in split() results
When a delimiter is specified, consecutive delimiters or a delimiter at the start or end of the string will result in empty strings "" appearing in the list. This does not happen when the argument is omitted (whitespace splitting), so be careful.
mistake1_ng.py
line = ",apple,,banana,"
items = line.split(",")
print(items)
print(len(items))
Running the code produces the following output:
python3 mistake1_ng.py ['', 'apple', '', 'banana', ''] 5
mistake1_ok.py
line = ",apple,,banana,"
items = [x for x in line.split(",") if x]
print(items)
print(len(items))
Running the code produces the following output:
python3 mistake1_ok.py ['apple', 'banana'] 2
Common Mistake 2: Passing non-strings to join()
All elements of the iterable passed to join() must be strings. If integers or other non-string types are included, a TypeError is raised. Convert them with str() first.
mistake2_ng.py
scores = [85, 90, 78]
print(",".join(scores))
Running the code produces the following output:
python3 mistake2_ng.py TypeError: sequence item 0: expected str instance, int found
mistake2_ok.py
scores = [85, 90, 78]
print(",".join(str(s) for s in scores))
Running the code produces the following output:
python3 mistake2_ok.py 85,90,78
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 you can 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.