AppDividend
Latest Code Tutorials

Python String split: How to Split String into List in Python

In this example, we will see how we can split the string by a separator. In any programming language, working with a string datatype is normal, and there are two most essential operations regarding string which is String Concatenation and String Split.

Python split()

Python string split() is an inbuilt function that splits the string into the list. The split() method returns the list of strings after breaking the given string by a specified separator. You can specify the separator, the default separator is any whitespace.

Note: When max is specified, the list will contain the specified number of elements plus one.

Syntax

str.split(separator, limit)

Parameters

The separator parameter is the delimiter which splits the string from a specified character.

The limit parameter is the optional limit, and if provided, then it splits the string into the maximum of the provided number of times.

How to split a string in Python

In Python, we can use the split() method to split the string. In some scenarios in our project, we may need to break the large string down into smaller chunks, or strings.

It is the opposite of concatenation which merges or combines the strings into one.

What it does is split or break up the string and add the data to a string list using a defined separator.

If the separator is not specified, then white space will be considered as a separator by default. In simpler terms, the separator is the defined character that will be placed between each item.

By default, the split() function takes whitespace as the delimiter.

# app.py

name = "k r u n a l"
data = name.split()

for char in data:
    print(char)

See the output.

➜  pyt python3 app.py
k
r
u
n
a
l
➜  pyt

Provide No Parameter in Split() Method

Let us see an example, where we do not provide any parameter in the split() method.

# app.py

strA = 'Welcome! Wish 2019 Will Be A Great Year'
splitedOut = strA.split()
print(splitedOut)

The output is the following.

Python String Split Example | split() Method Tutorial

Provide A Separator in Split() Method

Now, let us take an example to provide a separator and see the output.

# app.py

strA = 'Welcome! Wish 2019 Will Be A Great Year'
splitedOut = strA.split('!')
print(splitedOut)

In the output, it has split the string from the! Separator.

Provide A Separator in Split() Method

How Python split() works when maxsplit is specified

See this case where we provide both the parameters.

# app.py

strA = 'Welcome! Wish 2019 Will Be A Great Year'
splitedOut = strA.split(' ', 3)
print(splitedOut)

In the above code, we have defined the space as a separator, and we will split at max three times.

Python String Split Example

When the limit parameter is specified, the list will contain the specified number of items plus one. In the above example, we have put the limit of 3. That is why we get the list of items 4.

Python Split String into a list of characters

Write a Python program to split the characters of the given string into a list.

# app.py

def splitByChar(word):
    return [char for char in word]


eleven = 'MillieBobbyBrown'
print(splitByChar(eleven))

See the output.

➜  pyt python3 app.py
['M', 'i', 'l', 'l', 'i', 'e', 'B', 'o', 'b', 'b', 'y', 'B', 'r', 'o', 'w', 'n']
➜  pyt

Python provides direct typecasting of string into a list using the list() method.

# app.py

def splitByChar(word):
    return list(word)


eleven = 'MillieBobbyBrown'
print(splitByChar(eleven))

We will get the same output as above.

Python split a string into Dictionary

Let’s say, and I have a string like the following.

str = "el=Mille;mike=Finn;max=Sadie"

Now, we need to transform the above string into the dictionary.

We can accomplish the task reasonably merely with a generator comprehension.

# app.py

str = "el=Mille;mike=Finn;max=Sadie"
dictRes = dict(item.split("=") for item in str.split(";"))
print(dictRes)

Depending on the exact structure of your string format, you may need to write your simple parser. See the output.

➜  pyt python3 app.py
{'el': 'Mille', 'mike': 'Finn', 'max': 'Sadie'}
➜  pyt

Python split a string into integers

Let’s say we have the following string.

str = "11 21"

Now, we need to split the string into a list of integers.

We can use the Python map() and int() function. Then we need to convert the map object to a list using the list() method.

# app.py

str = "11 21"
mp = map(int, str.split())
res = list(mp)
print(res)

See the output.

➜  pyt python3 app.py
[11, 21]
➜  pyt

Python String Split by #(Hashtag)

See the following program.

# app.py

url = "appdividend.com#100#2017-03-27"
data = url.split("#")

for substring in data:
    print(substring)

See the output.

# app.py

➜  pyt python3 app.py
appdividend.com
100
2017-03-27
➜  pyt

Finally, the Python split() function example article is over.

Recommended Posts

String In Python

Python String Contains

Python String isupper islower upper-lower Functions

Python String startswith()

How to Check if Python String Contains Another String

1 Comment
  1. Jamaal says

    Thanks for this example.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.