Introduction
Strings often need to be combined or separated when preparing text for display, storage, or processing. This note shows how to join multiple strings into one and split a string into parts (examples use Python syntax). The same concepts apply in other languages (PHP, JavaScript, etc.), but the functions/method names differ.
1. Joining Strings
Joining takes a list (or iterable) of strings and combines them into a single string using a separator.
Syntax
separator.join(iterable)
Example
words = ["Python", "is", "fun"]
sentence = " ".join(words)
print(sentence) # Output: Python is fun
More examples
"-".join(["2025", "11", "23"]) # "2025-11-23"
",".join(["apple", "banana", "mango"]) # "apple,banana,mango"
2. Splitting Strings
Splitting breaks a single string into parts based on a delimiter.
Syntax
string.split(separator)
Example
text = "Python is awesome"
parts = text.split(" ")
print(parts) # Output: ['Python', 'is', 'awesome']
Split by comma
data = "apple,banana,mango"
data.split(",") # ['apple', 'banana', 'mango']
Split by hyphen
date = "2025-11-23"
date.split("-") # ['2025', '11', '23']
3. Splitting by Whitespace (No Separator)
If you call split() without an argument, it splits on any whitespace (spaces, tabs, newlines) and collapses multiple spaces.
text = "Python is amazing"
text.split() # ['Python', 'is', 'amazing']
4. Re-joining After Split
You can split a string and re-join it with a different separator:
text = "apple-banana-mango"
parts = text.split("-")
new_string = ", ".join(parts)
print(new_string) # Output: apple, banana, mango
5. Useful Patterns & Tips
- When splitting CSV-like data, consider using a CSV parser to handle quoted fields.
- Use
str.split(maxsplit=n)to limit the number of splits. - For performance with many small concatenations, build a list and
join()once (avoid repeated+on strings). - Trim whitespace before or after split with
.strip()if needed. - In other languages: PHP uses
explode()(split) andimplode()/join()(join); JavaScript usessplit()andarray.join().
Summary Table
| Operation | Python method | Example result |
|---|---|---|
| Join list | "sep".join(list) | "a b" |
| Split string | str.split("sep") | ["a","b"] |
| Split by whitespace | str.split() | ["a","b"] |
Citations
🔗 View other articles about PHP:
http://savanka.com/category/learn/php/
🔗 External PHP Documentation:
https://www.php.net/manual/en/