• Home
  • 4.22 Splitting Strings

4.22 Splitting Strings

View Categories

4.22 Splitting Strings

4 min read

Splitting a string means breaking it into smaller parts based on a specified separator.

Python provides the split() method to divide a string into multiple substrings. The resulting substrings are stored in a list.

The split() method is widely used when processing user input, reading files, parsing CSV data, and extracting information from text.

If no separator is specified, Python automatically splits the string wherever one or more whitespace characters (spaces, tabs, or newlines) occur.


Syntax #

string.split()

or

string.split(separator)

or

string.split(separator, maxsplit)

Components #

Component Description
string The string to be split.
split() Method that divides the string into substrings.
separator Character or substring used as the splitting point (optional).
maxsplit Maximum number of splits to perform (optional).

Example 1: Splitting Using Spaces #

main.py

text = "Python is easy to learn"

print(text.split())

Output #

['Python', 'is', 'easy', 'to', 'learn']

Explanation #

  • No separator is specified.
  • Python splits the string wherever spaces occur.
  • The result is a list containing five words.

Example 2: Splitting Using a Comma #

main.py

text = "Apple,Banana,Mango"

print(text.split(","))

Output #

['Apple', 'Banana', 'Mango']

Explanation #

  • The comma acts as the separator.
  • Each fruit name becomes a separate list element.
  • The returned value is a list.

Example 3: Using maxsplit #

main.py

text = "One Two Three Four"

print(text.split(" ", 2))

Output #

['One', 'Two', 'Three Four']

Explanation #

  • The separator is a space.
  • Only two splits are performed.
  • The remaining text becomes the last list element.

Example 4: Splitting a Date #

main.py

date = "15-08-2026"

print(date.split("-"))

Output #

['15', '08', '2026']

Explanation #

  • The hyphen (-) is used as the separator.
  • The day, month, and year become separate elements.
  • The returned value is a list.

Example 5: User Input #

main.py

name = input("Enter first and last name: ")

print(name.split())

Sample Input #

Alice Johnson

Output #

['Alice', 'Johnson']

Explanation #

  • The user enters two words.
  • split() separates them using spaces.
  • Both names are stored in a list.

Example 6: Splitting a Sentence into Words #

main.py

sentence = "Learning Python is fun"

words = sentence.split()

print(words)

Output #

['Learning', 'Python', 'is', 'fun']

Explanation #

  • The sentence is divided into individual words.
  • Each word becomes a list element.
  • The list is stored in the variable words.

Example 7: Multiple Spaces #

main.py

text = "Python     Programming     Language"

print(text.split())

Output #

['Python', 'Programming', 'Language']

Explanation #

  • No separator is specified.
  • Python automatically treats multiple consecutive spaces as a single separator.
  • Extra spaces are ignored.

Example 8: Splitting a File Path #

main.py

path = "Documents/Projects/Python"

print(path.split("/"))

Output #

['Documents', 'Projects', 'Python']

Explanation #

  • The forward slash (/) is used as the separator.
  • Each folder name becomes a separate element in the list.
  • This is useful when processing file paths.

Example 9: Checking the Return Type #

main.py

text = "Python Programming"

result = text.split()

print(type(result))

Output #

<class 'list'>

Explanation #

  • split() returns a list.
  • Each element in the list is a string.
  • The return type is list.

Example 10: Splitting with a Character That Does Not Exist #

main.py

text = "Python"

print(text.split(","))

Output #

['Python']

Explanation #

  • The comma does not exist in the string.
  • No splitting occurs.
  • The original string becomes the only element of the returned list.

Common Mistakes #

1. Assuming split() Returns a String #

Incorrect

text = "Python Programming"

result = text.split()

print(result.upper())

Output #

AttributeError: 'list' object has no attribute 'upper'

Reason

split() returns a list, not a string.


2. Expecting split() to Modify the Original String #

Incorrect

text = "Python Programming"

text.split()

print(text)

Output #

Python Programming

Reason

split() returns a new list.
The original string remains unchanged.


3. Using the Wrong Separator #

Incorrect

text = "Apple,Banana,Mango"

print(text.split(";"))

Output #

['Apple,Banana,Mango']

Reason

The separator ; does not exist in the string.

Correct

print(text.split(","))

4. Assuming maxsplit Limits the Number of Returned Elements #

Incorrect

text = "A B C D"

print(text.split(" ", 2))

Incorrect Expectation #

['A', 'B']

Actual Output #

['A', 'B', 'C D']

Reason

maxsplit limits the number of splits, not the number of list elements.


Best Practices #

  • Use the default split() when separating words by whitespace.
  • Specify a separator when processing structured data such as CSV files or dates.
  • Use maxsplit when only a limited number of splits is required.
  • Store the returned list in a descriptive variable.
  • Remember that split() returns a list, not a string.

Key Points to Remember #

  • split() divides a string into multiple substrings.
  • The default separator is whitespace.
  • A custom separator can be specified.
  • maxsplit limits the number of splits performed.
  • split() returns a value of type list.
  • The original string remains unchanged.
  • If the separator is not found, the returned list contains the original string as its only element.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *