In this source code example, we will demonstrate how to use the Python String split() method to split a string into a list of substrings.The following example illustrates how to use the split() method to split a string into multiple words:The following example shows how to use the split() method to split a string using the comma (,) separator:
Since the maxsplit is one, the number of elements in the results list is two.
Python
Python String
Python string split() method
The split() method splits a string and returns a list of substrings. The following shows the syntax of the split() method:str.split(sep=None, maxsplit=-1)The split() method accepts two optional parameters:
Python string split() method example
s = 'Source Code Examples'
substrings = s.split()
print(substrings)
Output:
['Source', 'Code', 'Examples']
s = 'Source,Code,Examples'
substrings = s.split(',')
print(substrings)
Output:
['Source', 'Code', 'Examples']
The following example illustrates how to use the split() method with the maxsplit parameter:
s = 'Source,Code,Examples'
substrings = s.split(',', 1)
print(substrings)
Output:
['Source', 'Code,Examples']
Related Python String Examples
- Python string literals
- Python string length example
- Python String join() method example
- Python string split() method example
- Python String index() method example
- Python string find() method example
- Python string startswith() method example
- Python string endswith() method example
- Python String lower() method example
- Python String upper() method example
- Python string title() method example
- Python string capitalize() method example
- Python string islower() method example
- Python string istitle() method example
- Python string isupper() method example
- Python string swapcase() method example
- Python string strip() method example
- Python string replace() method example
- Python string isdigit() method example
- Python string isdecimal() method example
- Python string isnumeric() method example
- Python string isalpha() method example
- Python string isalnum() method example
Comments
Post a Comment