Python String split() Method
The split() function splits the string by the specified separator and returns a list of string elements.
How to formulate it like this:
string. split(separator, maxsplit)
separator: [Optional] Splits the string at the specified separator. If this argument is not provided, the default value will be whitespace.
maxsplit : [Optional] Sets the maximum number of splits that can be performed. If this argument is not provided, the default value is -1 meaning there is no limit to the number of splits.
Return value: Returns the elements of the string in a new list, and the original string is not modified in any way.
In the following example we will split the string elements into a new list. Without using any of the preceding arguments.
Example:
# string We define a variable that contains a string and name it
string = 'Learn Python with Ahmed Obaid'
# To return the elements of a string in a new list, we call the split() function
print(string.split())
The output will be:
['Learn', 'Python', 'with', 'Ahmed', 'Obaid']
In the following example, we will split the string elements into a new list. byundefined argument (separator). In which we put the letter or symbol that we want to be the separator between the elements of the string.
Example:
# We define a variable that contains a string and name it string1
string1 = 'Learn-Python-with-Ahmed-Obaid'
# To return the elements of a string in a new list, we call the split() function
# partition separator'-'
print(string1.split('-'))
# We define a variable that contains a string and name it string2
string2 = 'Learn:Python:with:Ahmed:Obaid'
# To return the elements of a string in a new list, we call the split() function
#partition separator ':'
print(string2.split(':'))
# We define a variable that contains a string and name it string3
string3 = 'LearnxPythonxwithxAhmedxObaid'
# To return the elements of a string in a new list, we call the split() function
#partition separator 'x'
print(string3.split('x'))
The output will be:
['Learn', 'Python', 'with', 'Ahmed', 'Obaid']
['Learn', 'Python', 'with', 'Ahmed', 'Obaid']
['Learn', 'Python', 'with', 'Ahmed', 'Obaid']
In the following example we will control the number of splits that will be returned after parsing the string. by undefined argument (maxsplit).
Example:
# We define a variable that contains a string and name it string
string = 'Learn-Python-with-Ahmed-Obaid'
# To return the elements of a string in a new list, we call the split() function
# partition separator '-'
# Max split: 0
print(string.split('-', 0))
# partition separator '-'
# Maximum split:2
print(string.split('-', 2))
# partition separator '-'
# Max split: 3
print(string.split('-', 3))
The output will be:
['Learn-Python-with-Ahmed-Obaid']
['Learn', 'Python', 'with-Ahmed-Obaid']
['Learn', 'Python', 'with', 'Ahmed-Obaid']
External sources:
Built-in functions - official Python documentation
If you have any questions or concerns, leave them in the comments
Tags
Python python data types Python String string methods python python string split method
Share page
About author
Ahmed Obaid
Hello, I am Ahmed Obaid, an Egyptian Arabic programmer. I would like to put my experiences learning Python on this site So that it will be a reference for you and me as well.
Sign up to leave a comment