Easy Tutorial
❮ Python String Startswith Python Insertion Sort ❯

Python3 split() Method

Python3 String


Description

The split() method slices the string by specifying a delimiter. If the second parameter num is specified, the string is split into num+1 substrings.

Syntax

The syntax for the split() method is:

str.split(str="", num=string.count(str))

Parameters

Return Value

Returns a list of split strings.

Example

The following example demonstrates the use of the split() function:

Example (Python 3.0+)

#!/usr/bin/python3

str = "this is string example....wow!!!"
print (str.split( ))       # Split by spaces
print (str.split('i',1))   # Split by 'i'
print (str.split('w'))     # Split by 'w'

The output of the above example is:

['this', 'is', 'string', 'example....wow!!!']
['th', 's is string example....wow!!!']
['this is string example....', 'o', '!!!']

The following example splits the string by the # symbol, with the second parameter set to 1, returning two substrings.

Example (Python 3.0+)

#!/usr/bin/python3

txt = "Google#tutorialpro#Taobao#Facebook"

# The second parameter is 1, returning two substrings
x = txt.split("#", 1)

print(x)

The output of the above example is:

['Google', 'tutorialpro#Taobao#Facebook']

Python3 String

❮ Python String Startswith Python Insertion Sort ❯