๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-string-to-array-how-to-convert-text-to-a-list
Python String to Array โ€“ How to Convert Text to a List
February 21, 2022 - You do this by using the built-in list() function and passing the given string as the argument to the function. programming_language = "Python" programming_language_list = list(programming_language) print(programming_language_list) #output #['P', ...
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ python โ€บ third party โ€บ numpy โ€บ fromstring()
Python Numpy fromstring() - Convert String to Array
November 18, 2024 - The fromstring() function in Python's NumPy library is a powerful tool for converting string data into numerical arrays quickly and efficiently.
Discussions

python - How do I convert a string to an array? - Stack Overflow
Not a duplicate of Python - convert string to an array since answers there are relevant only for Python 2 How do I convert a string to a Python 3 array? I know how to convert a string into a list:... More on stackoverflow.com
๐ŸŒ stackoverflow.com
mysql - How to convert array string to an array in python - Stack Overflow
Im trying to convert an array that ive stored in a mysql database (as a string) to a standard array in python an example of what I mean is: This is what i get from the database: "['a',['b','c','d... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to Convert a String to an array in Python? - Stack Overflow
In the actual project I'm working on,I'm using redis for memory,but it only accepts strings,bytes,int and float as values,so as I need to store arrays,I transform an array like [{'hi':'there'}] int... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How would you approach transforming this string into an array?
I would approach it by figuring out what decides where to split the string...? More on reddit.com
๐ŸŒ r/learnpython
7
1
August 5, 2021
๐ŸŒ
Python Guides
pythonguides.com โ€บ convert-string-to-array-in-python
Convert String To Array In Python
May 16, 2025 - Learn how to convert a string to an array in Python using the split() method, list() constructor, or custom logic for more control over element separation.
๐ŸŒ
Replit
replit.com โ€บ home โ€บ discover โ€บ how to convert a string to an array in python
How to convert a string to an array in Python | Replit
April 14, 2026 - Learn how to convert a string to an array in Python. Explore various methods, tips, real-world examples, and common error debugging.
๐ŸŒ
EDUCBA
educba.com โ€บ home โ€บ software development โ€บ software development tutorials โ€บ python tutorial โ€บ python string to array
Python String to Array | How to Convert String to an Array with Examples?
October 13, 2023 - Converting a Python string to an array involves breaking down a string into individual elements or characters and storing them as an ordered sequence in an array. This is commonly achieved using the split() method, which divides the string based ...
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
๐ŸŒ
YouTube
youtube.com โ€บ wrt tech
Python - Convert a String to an Array | Codewars 8KYU - YouTube
Super easy codewars problem for beginners
Published: June 29, 2022
Views: 2K
๐ŸŒ
DEV Community
dev.to โ€บ itsmycode โ€บ how-to-convert-python-string-to-array-3fpb
How To Convert Python String To Array - DEV Community
December 8, 2021 - In Python, we do not have an in-built array data type. However, we can convert Python string to list, which can be used as an array type.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-program-to-convert-a-list-to-string
Python Program to Convert a List to String - GeeksforGeeks
May 8, 2026 - This can be done using methods like join(), loops, list comprehension and map() depending on the type of elements in the list. The join() method is one of the most common and efficient ways to convert a list of strings into a single string.
Find elsewhere
๐ŸŒ
Quora
quora.com โ€บ How-can-we-convert-a-string-into-an-array-in-Python-or-PHP
How can we convert a string into an array in Python or PHP? - Quora
Answer: In PHP that depends on how the string needs to be split up in the resulting array. For char per array position you can use: [code] str_split(string $string, int $length = 1): array [/code]If the optional [code ]length[/code] parameter is specified, the returned array will be broken down...
๐ŸŒ
arrayThis
arraythis.com
Online array converter; convert your text list to array | arrayThis
instantly convert your text list to array; valid for JS, PHP, PERL, PYTHON (as list) and much more. Is your list ready? In the need to optimize your list (remove duplicates, empty lines, sort, prefix and suffix, etc.) use KitTxt before. convert ยท quotes ยท Double ยท Single ยท
Top answer
1 of 4
4

Nice solution, a few suggestions:

  • The function name convert_to_int_array seems too general. Consider a more specific name, for example, extract_numbers, or something similar.
  • There is no need to initialize results_as_array to None.
  • Mapping a list of strings to integers:
    for index in range(0, len(results_as_array)):
        results_as_array[index] = int(results_as_array[index].strip())
    return results_as_array
    
    looks like the job for map:
    return list(map(int, results_as_array))
    
    Since the result of map is a generator, it needs to be converted to a list with list.
  • Tests: I noticed this line:
    draw_result = "04-15-17-25-41" # change to "01-12 + 08-20" or "04-15-17-25-41" or "03-23-27-34-37, Mega Ball: 13" to test
    
    testing by changing draw_result manually it is time-consuming. A better way would be to keep the tests in a dictionary and to use assert. For example:
    tests = {
          "04-15-17-25-41": [4, 15, 17, 25, 41],
          "01-12 + 08-20": [1, 12, 8, 20],
          "03-23-27-34-37, Mega Ball: 13": [3, 23, 27, 34, 37]
    }
    
    for lottery_string, expected_output in tests.items():
          assert expected_output == convert_to_int_array(lottery_string)
    
    If you want to explore more "unit testing" have a look at unittest.

Alternative approach:

  1. Remove , Mega Ball: 13 if exists
  2. Extract all numbers with a regular expression
import re

def extract_numbers(draw_result):
    draw_result = draw_result.split(',')[0]
    matches = re.findall(r'\d+', draw_result)
    return list(map(int, matches))
2 of 4
5

Marc's answer is an excellent option if you don't care about validation.

If you need to be somewhat sure that the given string represents one of the known formats, then you can pre-bind a match method in a list of pre-compiled regular expressions:

import re
from typing import Iterable

lottery_searches = [
    re.compile(pat).match
    for pat in (
        r'^(\d+)-(\d+)-(\d+)-(\d+)-(\d+), Mega Ball.*$',
        r'^(\d+)-(\d+) \+ (\d+)-(\d+)$',
        r'^(\d+)-(\d+)-(\d+)-(\d+)-(\d+)$',
    )
]


def lottery_string_to_ints(lottery: str) -> Iterable[int]:
    for search in lottery_searches:
        match = search(lottery)
        if match:
            return (int(g) for g in match.groups())

    raise ValueError(f'"{lottery}" is not a valid lottery string')

with output

In[2]: tuple(lottery_string_to_ints('03-23-27-34-37, Mega Ball: 13'))
Out[2]: (3, 23, 27, 34, 37)

In[3]: tuple(lottery_string_to_ints('01-12 + 08-20'))
Out[3]: (1, 12, 8, 20)

In[4]: tuple(lottery_string_to_ints('04-15-17-25-41'))
Out[4]: (4, 15, 17, 25, 41)

In[5]: tuple(lottery_string_to_ints('04-15'))
Traceback (most recent call last):
  File "262933.py", line 20, in lottery_string_to_ints
    raise ValueError(f'"{lottery}" is not a valid lottery string')
ValueError: "04-15" is not a valid lottery string
๐ŸŒ
HCL GUVI
studytonight.com โ€บ python-howtos โ€บ how-to-convert-string-to-character-array-in-python
HCL GUVI | Learn to code in your native language
January 30, 2021 - HCL GUVI's Data Science Program was just fantastic. It covered statistics, machine learning, data visualization, and Python within its curriculum.