Method Name:
split
Method Signature:
split()
Method Overview:
- The split() method tokenizes a string into 'n' tokens based on the separator-substring and the number of tokens specified.
- The split() method starts tokenization at the lowest index whereas the rsplit() method starts tokenization at the highest index.
- The split() and rsplit() methods are useful in data processing applications. Examples include time series data like stock quotes, historic weather data and so on.
- If no substring is specified as the separator-substring whitespace is assumed to be the separator-substring.
- split() method treats continuous whitespaces as one single whitespace character.
Example:
# Comma separated values csvFields = "field1,field2,field3,field4"
# Specify the separator as comma tokens = csvFields.split(",") print(tokens)
# Specify the separator as comma and the token count as 2 tokens = csvFields.split(",", 2) print(tokens)
# whitespace is assumed as the separator quote = "If you love life, don't waste time, for time is what life is made up of" quoteTokens = quote.split() print(quoteTokens)
|
Output:
['field1', 'field2', 'field3', 'field4'] ['field1', 'field2', 'field3,field4'] ['If', 'you', 'love', 'life,', "don't", 'waste', 'time,', 'for', 'time', 'is', 'what', 'life', 'is', 'made', 'up', 'of'] |