Section 1/6split()
split()
Splits a string into a list of substrings. If no separator is specified, it splits by any sequence of whitespace characters (spaces, tabs, newlines).
Common String Methods in Python
Python strings come equipped with a variety of powerful methods for text manipulation. In this lesson, we'll explore some of the most frequently used ones:split(), upper(), lower(),replace(), and join(). Mastering these will allow you to process and transform text data with ease.
split()
Splits a string into a list of substrings. If no separator is specified, it splits by any sequence of whitespace characters (spaces, tabs, newlines).
Default separator (whitespace):
sentence = "hello world\nnew line"words = sentence.split()print(words)
→
['hello', 'world', 'new', 'line']Custom separator (e.g., comma):
data = "apple,banana,orange"fruits = data.split(',')print(fruits)
→
['apple', 'banana', 'orange']