How do you split a string into a list of integers in python?

To convert a space-separated string to a list in Python, call the str.split() method:

sentence = "This is a test"

words_list = sentence.split()

print(words_list)

Output:

['This', 'is', 'a', 'test']

This works because the str.split() splits the string by blank spaces by default.

Let’s then take a look at how to split a string of integers into a list of integers.

To convert a space-separated string of integers to a list in Python:

  1. Split the string on empty spaces.
  2. Convert each element to an integer.
  3. Add each integer to a list.

You can do this with a for loop:

numbers_str = "1 2 3 4 5"

numbers_list = []

for num_str in numbers_str.split():
    num_int = int(num_str)
    numbers_list.append(num_int)

print(numbers_list)

Output:

[1, 2, 3, 4, 5]

To make the expression way shorter, you can use a list comprehension:

numbers_str = "1 2 3 4 5"

numbers_list = [int(num) for num in numbers_str.split()]

print(numbers_list)

Output:

[1, 2, 3, 4, 5]

Conclusion

Thanks for reading. I hope you found the answer you were looking for.

Happy coding!

Further Reading

Python Tricks

Python List Comprehensions

How do you split a string into a list of integers in python?

In any programming language, working with a string datatype is normal, and there are two most essential operations regarding string: String concatenation and String split.

Python string split() is a built-in function that splits the string into a list. The split() method returns the list of strings after breaking the given string by a specified separator. You can specify the separator, and 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 that splits the string from a specified character.

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

How to split a string in Python

To split a string in Python, use the split() method. The split() method splits or breaks up the string and adds the data to a string list using a defined separator.

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

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.

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

Passing 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.

How do you split a string into a list of integers in python?

Provide A Separator in split() Method

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.

How do you split a string into a list of integers in python?

How Python split() works when maxsplit is specified

See this case where we provide both 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 will split at max three times.

How do you split a string into a list of integers in python?

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 strings into a list using the list().

# 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 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() functions. 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.

Python String Contains

Python String isupper islower upper-lower functions

Python String startswith

How to Check if Python String Contains Another String

How do you split a string of integers?

Use str..
a_string = "1 2 3".
a_list = a_string. split().
map_object = map(int, a_list) applies int() to a_list elements..
list_of_integers = list(map_object).
print(list_of_integers).

How do you turn a string into a list in Python?

To convert a list to a string, use Python List Comprehension and the join() function. The list comprehension will traverse the elements one by one, and the join() method will concatenate the list's elements into a new string and return it as output.

How do you split an integer in Python?

How to split Integer Into Digits in Python.
How to split Integer Into Digits in Python..
Method 1: Use math.ceil().
Method 2: Use List comprehension..
Method 3: Use for loop..
Method 4: Use int() and slice..
Conclusion..