Learning the split( ) Method
In this lesson, you’ll learn how to use the split() method in Python to break a string into smaller parts. By the end, you’ll know how to divide a sentence into words, separate text by commas, and handle various delimiters.
In this lesson, you’ll learn how to use the split() method in Python to break a string into smaller parts. By the end, you’ll know how to divide a sentence into words, separate text by commas, and handle various delimiters.
1. What Is the split() Method?
The split() method divides a string into a list of smaller strings based on a specific separator (like a space, comma, or dash).
Here’s the syntax:
string.split(separator)
string: The text you want to split.
separator: The character or string that determines where the split happens (optional; defaults to spaces).
The result is a list of the separated parts.
2. Splitting by Spaces
sentence = "I love Python programming"
words = sentence.split()
print(words)
Output:
['I', 'love', 'Python', 'programming']
Explanation:
The split() method separates the string into words based on spaces.
3. Splitting by a Specific Character
You can specify a character to split by, such as a comma or a dash.
Example 1: Splitting by Comma
fruits = "apple,banana,cherry"
fruit_list = fruits.split(",")
print(fruit_list)
Output:
['apple', 'banana', 'cherry']
Example 2: Splitting by Dash
text = "2023-12-25"
date_parts = text.split("-")
print(date_parts)
Output:
['2023', '12', '25']
4. Splitting by Multiple Spaces
If the string has multiple spaces, split() automatically handles it:
sentence = "Python is fun"
words = sentence.split()
print(words)
Output:
['Python', 'is', 'fun']
5. Splitting with a Limit
You can limit the number of splits by adding an optional argument.
text = "one, two, three, four"
result = text.split(", ", 2)
print(result)
Output:
['one', 'two', 'three, four']
Explanation:
The string is split into at most 3 parts.
6. Practical Uses of split()
Example 1: Splitting Sentences into Words
quote = "Learning Python is fun"
words = quote.split()
print("Words in the quote:", words)
Output:
Words in the quote: ['Learning', 'Python', 'is', 'fun']
Example 2: Extracting Data from a CSV
data = "John,25,New York"
info = data.split(",")
print("Name:", info[0])
print("Age:", info[1])
print("City:", info[2])
Output:
Name: John
Age: 25
City: New York
Example 3: Processing Dates
date = "2024-12-22"
parts = date.split("-")
print(f"Year: {parts[0]}, Month: {parts[1]}, Day: {parts[2]}")
Output:
Year: 2024, Month: 12, Day: 22
7. Practice Exercises
Exercise 1: Split a Sentence
Write a program that splits the sentence "Coding is exciting" into words.
Exercise 2: Split by Commas
Given the string "cat,dog,fish", write a program to split it into a list of animals.
Exercise 3: Split and Extract
Given the string "Name: Alice, Age: 13, Grade: 8", write a program to extract and print just the name, age, and grade.
Exercise 4: Process a CSV Line
Split the string "Math, 90, 85, 88" into a list and print each value on a separate line.
8. Bonus Challenge
Create a program that asks the user to input their full name (e.g., "John A. Smith") and then splits it into first, middle, and last names. If no middle name is provided, handle it gracefully.
9. What’s Next?
You’ve just learned how to use the split() method! It’s super helpful for processing text and working with lists. Keep practicing to master string manipulation in Python. Happy coding! 🎉