- 5 simple examples to learn python string.split()
- Python string.split() syntax
- Example-1: Split string with whitespace
- Example-2: Use comma as separator
- Example-3: Define maximum split limit
- Example-4: Count occurrences of word in a file
- Example-5: Split string using one liner with for loop
- Conclusion
- Leave a Comment Cancel reply
- Python Tutorial
- Python split() – String Splitting Example
- Syntax of the split() Method in Python
- How to Use the split() Method Without Parameters
- How to Use the split() Method With Parameters
- Conclusion
5 simple examples to learn python string.split()
In this Python tutorial we will learn about Python split() string function. Unlike len() , some functions are specific to strings. To use a string function, type the name of the string, a dot, the name of the function, and any arguments that the function needs: string.function(arguments) . You can use the built-in string split() function to break a string into a list of smaller strings based on some separator.
Python string.split() syntax
The syntax as per docs.python.org to use string.split() :
string.split([separator[, maxsplit]])
- separator is the delimiter string
- If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements)
- If maxsplit is not specified or -1 , then there is no limit on the number of splits (all possible splits are made).
- If separator is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example, ‘1,,2’.split(‘,’) returns [‘1’, », ‘2’] )
- If separator is not specified or is None, runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. For example, ‘ 1 2 3 ‘.split() returns [‘1’, ‘2’, ‘3’]
Example-1: Split string with whitespace
In this example script we will split a sentence containing strings into multiple sub string using whitespace as the separator. If you don’t have a separator to be defined then you can just provide split() which will by default consider separator as None .
#!/usr/bin/env python3 mystring = "This is Python Tutorial" print(type(mystring)) ## This will return type as string newstring = mystring.split() ## split the string and store into newstring var print(newstring) ## print the content of newstring print(type(newstring)) ## the new type would be list after splitting
string.split() will break and split the string on the argument that is passed and return all the parts in a list. The list will not include the splitting character(s).
Example-2: Use comma as separator
In this example we will define a separator as comma( , ) and split the strings into list
#!/usr/bin/env python3 mystring = "abc,def,ghi" print(type(mystring)) ## This will return type as string newstring = mystring.split(',') ## split the string using ',' and store into newstring var print(newstring) ## print the content of newstring print(type(newstring)) ## the new type would be list after splitting
So the output is split using the comma character this time because we used string.split(,) . Similarly you can use any other character to split your string.
Example-3: Define maximum split limit
By default if your don’t specify split limit, then all the possible values will be slit from the provided string. In this example we will define maxlimit as 1 so after the first split, python will ignore the remaining separators.
#!/usr/bin/env python3 mystring = "abc,def,ghi,tre,deb" print(type(mystring)) ## This will return type as string ## split the string using sep=',' with maxlimit=1 and store into newstring var newstring = mystring.split(',',1) print(newstring) ## print the content of newstring print(type(newstring)) ## the new type would be list after splitting
As you can see from the output, our string was split into two parts wherein after the first separator match, all other commas are ignored.
Example-4: Count occurrences of word in a file
The split() method separates a string into parts wherever it finds a space and stores all the parts of the string in a list. The result is a list of words from the string, although some punctuation may also appear with some of the words.
We will use split() to count the number of word in » /usr/share/doc/grep/README » file. You can ignore the try and except block if you are not yet familiar with it, you can concentrate on the else block where I am performing the actual task:
~]# python3 count-words.py The file /usr/share/doc/grep/README has about 372 words.
Let us verify the output with wc :
~]# wc -w /usr/share/doc/grep/README 372 /usr/share/doc/grep/README
So the output from our script and wc are same which means split() has successfully separated the words.
Example-5: Split string using one liner with for loop
In this example we will use one liner code to split the string and print words having more than 4 characters.
#!/usr/bin/env python3 mystring = 'This is a dummy text we are testing python split string' ## One-Liner w = [[x for x in line.split() if len(x)>4] for line in mystring.split('\n')] ## Result print(w)
- The inner list comprehension expression [x for x in line.split() if len(x)>4] uses the string split() function to divide a given line into a sequence of words. We iterate over all words x and add them to the list if they have more than three characters.
- The outer list comprehension expression creates the string line used in the previous statement. Again, it uses the split() function to divide the mystring on the newline characters ‘\n’ .
~]# python3 one-liner-split.py [['dummy', 'testing', 'python', 'split', 'string']]
Conclusion
In this tutorial we learned about string.split() using different examples. We can combine split with regex to add more powerful features which will cover in different tutorial. Here I have covered limited examples on using it with string in different scenarios.
Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud
If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.
For any other feedbacks or questions you can either use the comments section or contact me form.
Thank You for your support!!
Leave a Comment Cancel reply
Python Tutorial
- Python Multiline Comments
- Python Line Continuation
- Python Data Types
- Python Numbers
- Python List
- Python Tuple
- Python Set
- Python Dictionary
- Python Nested Dictionary
- Python List Comprehension
- Python List vs Set vs Tuple vs Dictionary
- Python if else
- Python for loop
- Python while loop
- Python try except
- Python try catch
- Python switch case
- Python Ternary Operator
- Python pass statement
- Python break statement
- Python continue statement
- Python pass Vs break Vs continue statement
- Python function
- Python call function
- Python argparse
- Python *args and **kwargs
- Python lambda function
- Python Anonymous Function
- Python optional arguments
- Python return multiple values
- Python print variable
- Python global variable
- Python copy
- Python counter
- Python datetime
- Python logging
- Python requests
- Python struct
- Python subprocess
- Python pwd
- Python UUID
- Python read CSV
- Python write to file
- Python delete file
- Python any() function
- Python casefold() function
- Python ceil() function
- Python enumerate() function
- Python filter() function
- Python floor() function
- Python len() function
- Python input() function
- Python map() function
- Python pop() function
- Python pow() function
- Python range() function
- Python reversed() function
- Python round() function
- Python sort() function
- Python strip() function
- Python super() function
- Python zip function
- Python class method
- Python os.path.join() method
- Python set.add() method
- Python set.intersection() method
- Python set.difference() method
- Python string.startswith() method
- Python static method
- Python writelines() method
- Python exit() method
- Python list.extend() method
- Python append() vs extend() in list
- Create your first Python Web App
- Flask Templates with Jinja2
- Flask with Gunicorn and Nginx
- Flask SQLAlchemy
Python split() – String Splitting Example
Ihechikara Vincent Abba
The split() method in Python splits characters in a string into separate items in a list.
In this tutorial, we’ll go over some examples to help you learn how to use the split() method. We’ll start from the syntax and then see how we can modify the list returned using the split() method’s parameters.
Syntax of the split() Method in Python
The split() method takes in two parameters. Here’s what the syntax looks like:
str.split(separator, maxsplit)
The parameters above are separator and maxsplit . These parameters are both optional, but let’s discuss what they do.
separator specifies the character at which the split occurs. If not specified, whitespaces will be used as the default character where splitting occurs. You’ll understand this better in the examples in the coming sections.
maxsplit specifies the maximum number of splits. The default value is -1 which allows for a continuous number of splits. This argument is also optional.
How to Use the split() Method Without Parameters
In this section, we’ll see some examples of string splitting using the split() method without passing in any parameters.
myString = "Python is a programming language" print(myString.split()) # ['Python', 'is', 'a', 'programming', 'language']
In the code above, we created a string called myString with five characters making up the string: «Python is a programming language».
We then used the split() method on our string using dot notation.
When printed to the console, each character in the string became a separate item in a list data type: [‘Python’, ‘is’, ‘a’, ‘programming’, ‘language’] .
The split() method is able to separate each word because, by default, whitespaces indicate the point of splitting for each character (refer to the separator parameter in the previous section).
How to Use the split() Method With Parameters
In this section, we’ll understand how to use the split() method’s parameters with examples.
myString = "Hello World!, if you're reading this, you're awesome" print(myString.split(", ")) # ['Hello World!', "if you're reading this", "you're awesome"]
In the example above, we passed in a comma (,) as the separator : myString.split(«, «) .
So instead of splitting the characters after each whitespace, the characters will only be split when a comma appears: [‘Hello World!’, «if you’re reading this», «you’re awesome»] . This means that characters that appear before a comma will be grouped together.
In the next example, we’ll work with the second parameter – maxsplit .
myString = "Hello World!, if you're reading this, you're awesome" print(myString.split(", ", 0)) # ["Hello World!, if you're reading this, you're awesome"]
We’ve added a maxsplit value of 0 in the code above. This controls how the string is split. 0 implies 1 so the characters are returned as one item in a list: [«Hello World!, if you’re reading this, you’re awesome»]
Let’s change the number and see what happens
myString = "Hello World!, if you're reading this, you're awesome" print(myString.split(", ", 1)) # ['Hello World!', "if you're reading this, you're awesome"]
Now that we’ve changed the number to 1, the characters are split into two items in the list – »Hello World!» and «if you’re reading this, you’re awesome».
Omitting the maxsplit value will set it to -1 by default. This negative value allows the split() methods to split each character continuously into separate items until there are no more characters left. If there is a specified separator , the splitting will be done with respect to that value – otherwise, whitespaces will be used.
Conclusion
In this article, we talked about the split() method in Python which splits characters in a string and returns them as items in a list.
We saw the syntax for the split() method and the two parameters provided by default – the separator and maxsplit parameters.
We also saw some examples divided into two sections. The first section showed how to use the split() method without parameters while the second showed how we’d use the method’s parameters to achieve varying results.