The split() method in Python returns a list of the words in the string/line , separated by the delimiter string. This method will return one or more new strings. All substrings are returned in the list datatype.
Syntax
separator | The is a delimiter. The string splits at this specified separator. If is not provided then any white space is a separator. |
maxsplit | It is a number, which tells us to split the string into maximum of provided number of times. If it is not provided then there is no limit. |
return | The split() breaks the string at the separator and returns a list of strings. |
If no separator is defined when you call upon the function, whitespace will be used by default. In simpler terms, the separator is a defined character that will be placed between each variable. The behavior of split on an empty string depends on the value of sep. If sep is not specified, or specified as None, the result will be an empty list. If sep is specified as any string, the result will be a list containing one element which is an empty string .
Splitting String by space
The split() method in Python without an argument splits on whitespace.
example
output
Splitting on first occurrence
In the following example, it will Split by first 2 whitespace only.
example
output
Splitting lines from a text file in Python
The following Python program reading a text file and splitting it into single words in python
example
Splitting String by newline(\n)
output
Splitting String by tab(\t)
output
Splitting String by comma(,)
output
Split string with multiple delimiters
In this case Python uses Regular Expression.
example
output
Split a string into a list
The following Python program split a string to a List.
example
output
maxsplit parameter
Split the string into a list with max 2 items
output
In the above program maxsplit is 2, the first two string are split and rest of them are in a same string.
Split a string into array of characters
output
Python split() using substring
Extact a string after a specific substring.
In the above example, you can see the split() function return next part of a string using a specific substring.
Here, you can see the split() function return the previous part of the string using a specific substring.
Looking for a Python job ?
Chances are you will need to prove that you know how to work with Python. These Python Interview Questions have been designed especially to get you acquainted with the nature of questions you may encounter during your interview for the subject of Python Programming . Here are the top objective type sample Python Interview questions and their answers are given just below to them. These sample questions are framed by our experts team who trains for Python training to give you an idea of type of questions which may be asked in interview.
Go to... Python Interview Questions
I like iterators!
def chunk(in_string,num_chunks): chunk_size = len(in_string)//num_chunks if len(in_string) % num_chunks: chunk_size += 1 iterator = iter(in_string) for _ in range(num_chunks): accumulator = list() for _ in range(chunk_size): try: accumulator.append(next(iterator)) except StopIteration: break yield ''.join(accumulator) ## DEMO >>> string = "a"*32+"b"*32+"c"*32+"d"*32 >>> list(chunk(string,4)) ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', 'cccccccccccccccccccccccccccccccc', 'dddddddddddddddddddddddddddddddd'] >>> string += "e" # so it's not evenly divisible >>> list(chunk(string,4)) ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbcc', 'ccccccccccccccccccccccccccccccddd', 'ddddddddddddddddddddddddddddde']Also demonstrably faster than textwrap.wrap, although almost certainly less "good"
>>> timeit.timeit(lambda: list(chunk(string,4)),number=500) 0.047726927170444355 >>> timeit.timeit(lambda: textwrap.wrap(string,len(string)//4),number=500) 0.20812756575945457And pretty easy to hack to work with any iterable (just drop the str.join and yield accumulator unless isinstance(in_string,str))
# after a petty hack >>> list(chunk([1,2,3,4,5,6,7,8,9,10,11,12],4)) [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]