Built with Tutify.io

    AIStudyLabs

    Python String split() with Examples

    Python String split() with Examples

    The split() is a method in Python that is used to split a string into a list of substrings. It takes two optional arguments: First, sep, which is a string delimiter that separates the substrings (defaults to while space), and second, maxsplit which is an integer that specifies the maximum number of splits to perform.

    Key Points of Split String in Python:

    In this article, we will discuss several ways to split the given string in different forms using the split() method in Python. The string is a set of characters similar to C, C++, and other programming languages.

    <a id="h-1-quick-examples-of-string-split"></a>

    1. Quick Examples of String Split

    Following are quick examples of how to split a string.

    
    # Consider the string
    st1="Hello-welcome-to-sparkby-examples"
    print("String: ",st1)
    
    # Split the string using sep parameterr.
    print(st1.split("-"))
    
    # Split by using sep & maxsplit as 2.
    print(st1.split("-",2))
    
    # Consider the string
    st1="Hello"
    
    # Split the string by using list()
    print(list(st1))
    
    # Split the string by using tuple()
    print(tuple(st1))
    

    <a id="h-2-string-split-in-python"></a>

    2. String split() in Python

    The split() in python is a built-in function that is used to split the string by a delimiter into several substrings. It will take sep as the first parameter that specified the delimiter to split on and maxsplit as a parameter that takes an integer value to specify a maximum number of splits to be done.

    <a id="h-2-1-split-syntax"></a>

    2.1 split() syntax

    Following is the syntax.

    
    # Syntax
    string1.split(delimiter,maxsplit)
    

    <a id="h-2-2-split-parameters"></a>

    2.2 split() parameters

    1. The first parameter takes a delimiter in which the string is split based on this delimiter.
    2. The maxsplit takes an integer value that specifies a maximum number of splits to be done. If it is not specified, it splits the string by all possible delimiters.

    <a id="h-2-3-python-string-split-examples"></a>

    2.3 Python String split() Examples

    Let's see how to split the string by using the different combinations of the parameters.

    <a id="h-2-3-1-split-with-space-delimiter"></a>

    2.3.1 split() with space delimiter

    As I mentioned above, the default value of sep is space hence, if you don't specify the sep, it splits the string by space. however, in the below example, I have used the sep with " " as a param to explain.

    
    # Consider the string
    st1="Hello welcome to sparkby examples"
    print("String: ",st1)
    
    # Split the string by using space as a separator
    splitted = st1.split(" ")
    print(splitted)
    

    Yields below output.

    Here, we didn't specify the maxsplit parameter hence, it split string by all spaces and returns all substrings as a list.

    <a id="h-2-3-2-split-with-maxsplit"></a>

    2.3.2 split() with maxsplit

    Let's use another string with - as a separator and use both sep and maxsplit parameters. In the first example below, the string is split on the first '-' character, resulting in a list with two elements. In the second example, the string is split on the first two '-' characters, resulting in a list with three elements.

    
    # Consider the string
    st1="Hello-welcome-to-sparkby-examples"
    
    print("String: ",st1)
    
    # Split the string by using - as a separator with maxsplit as 1.
    print(st1.split("-",1))
    
    # Split the string by using - as a separator with maxsplit as 2.
    print(st1.split("-",2))
    
    # Output:
    # String:  Hello-welcome-to-sparkby-examples
    # ['Hello', 'welcome-to-sparkby-examples']
    # ['Hello', 'welcome', 'to-sparkby-examples']
    

    <a id="h-3-split-the-string-with-substring"></a>

    3. Split the String with substring

    In all the above examples, we have used the single-character delimiter to split the string in python. The sep param can also take a string as a value. so let's split the string by substring and see how it behaves.

    Here, we are splitting the string with a delimiter 'welcome', The final list has 3 strings.

    
    # Consider the string
    st1="Hello welcome to sparkby welcome examples"
    
    print("String: ",st1)
    
    # Split the string by using "welcome" as a separator
    splitted = st1.split("welcome")
    
    print(splitted)
    
    # Output:
    # String:  Hello welcome to sparkby welcome examples
    # ['Hello ', ' to sparkby ', ' examples']
    

    <a id="h-4-split-the-string-using-list"></a>

    4. Split the String using list()

    The list() is the inbuilt method in python that can be used to split the string by each character. In the below example, the total number of characters in the actual string is 5, when we applied list() function, It is splitted into 5 characters and returned as a list.

    
    # Consider the string
    st1="Hello"
    print("String: ",st1)
    
    # Split the string by using list()
    splitted = list(st1)
    print(splitted)
    
    # Output:
    # String:  Hello
    # ['H', 'e', 'l', 'l', 'o']
    

    <a id="h-5-split-the-string-using-tuple"></a>

    5. Split the String using tuple()

    The tuple() is an inbuilt method in python that will split the string by each character into tuple. In the below example, the total number of characters in the actual string is 5, when we applied tuple() function, It is splitted into 5 characters and returned in tuple.

    
    # Consider the string
    st1="Hello"
    
    print("String: ",st1)
    
    # Split the string by using tuple()
    splitted = tuple(st1)
    
    print(splitted)
    
    # Output:
    # String:  Hello
    # ('H', 'e', 'l', 'l', 'o')
    

    <a id="h-6-conclusion"></a>

    6. Conclusion

    In this article, you have learned the python split() method syntax, parameters, and how to split the string by delimiter. It takes two optional arguments: First, sep, which is a string delimiter that separates the substrings (defaults to while space), and second, maxsplit which is an integer that specifies the maximum number of splits to perform. In case if you wanted to split string by character use list() or tuple() methods.

    <a id="h-related-articles"></a>

    Related Articles