๐ŸŒ
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 - However, you can reassign a different string by updating the variable, like so: fave_language = "python" fave_language = "Python" print(fave_language) #output #Python ยท A list is an ordered collection of data. Multiple (typically related) items are stored together under the same variable.
๐ŸŒ
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.
Top answer
1 of 4
24

I'm not sure that this is the fastest, but it's definitely the safest/easiest:

import ast
lst = ast.literal_eval(s)

regular eval would work too:

lst = eval(s)

Some basic timings from my machine:

>>> s = '[54, 95, 45, -97, -51, 84, 0, 32, -55, 14, 50, 54, 68, -3, 57, 88, -1]' 
>>> def f1():
...    eval(s)
... 
>>> def f2():
...    ast.literal_eval(s)
... 
>>> timeit.timeit('f1()', 'from __main__ import f1')
31.415852785110474
>>> timeit.timeit('f2()', 'from __main__ import f2')
46.25958704948425

So, according to my computer, eval is about 50% faster than ast.literal_eval. However, eval is terribly unsafe and should never be used on any string unless you trust it completely. Unless this is a real demonstratable bottleneck and you trust the input 100%, I would consider the little bit of extra time worth it in exchange for being able to sleep soundly at night.

2 of 4
18

Since we care about speed, in this particular case I might use json.loads:

>>> import ast, json
>>> s = "[54, 95, 45, -97, -51, 84, 0, 32, -55, 14, 50, 54, 68, -3, 57, 88, -1]"
>>> %timeit ast.literal_eval(s)
10000 loops, best of 3: 61.6 ยตs per loop
>>> %timeit eval(s)
10000 loops, best of 3: 45.7 ยตs per loop
>>> %timeit json.loads(s)
100000 loops, best of 3: 6.61 ยตs per loop
>>> json.loads(s)
[54, 95, 45, -97, -51, 84, 0, 32, -55, 14, 50, 54, 68, -3, 57, 88, -1]

(Note that this works here because this line is sufficiently JSON-like. It can't be used everywhere that ast.literal_eval can because not all Python literal syntax is valid JSON.)

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-program-convert-string-list
Convert string to a list in Python - GeeksforGeeks
The most common way to convert a string into a list is by using the split() method. The split() method by default splits a string based on spaces, but it can also split using a custom delimiter.
Published ย  May 5, 2025
๐ŸŒ
New York University
physics.nyu.edu โ€บ pine โ€บ pymanual โ€บ html โ€บ chap3 โ€บ chap3_arrays.html
3. Strings, Lists, Arrays, and Dictionaries โ€” PyMan 0.9.31 documentation
For other tasks, lists work just ... to arrays. Strings are lists of keyboard characters as well as other characters not on your keyboard. They are not particularly interesting in scientific computing, but they are nevertheless necessary and useful. Texts on programming with Python typically ...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ gloss_python_strings_are_arrays.asp
Python Strings Are Arrays
Like many other popular programming languages, strings in Python are arrays of bytes representing unicode characters. However, Python does not have a character data type, a single character is simply a string with a length of 1. Square brackets can be used to access elements of the string.
๐ŸŒ
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 - The code defines a function called string_to_array that takes a string s and converts it into an array of characters. It then uses this function to convert the โ€˜Educba Trainingโ€™ string into an array of individual characters and prints the ...
Call ย  +917738666252
Address ย  Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
Find elsewhere
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_arrays.asp
Python Arrays
Remove List Duplicates Reverse a String Add Two Numbers ยท Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... Note: Python does not have built-in support for Arrays, but Python Lists can be used instead. Note: This page shows you how to use LISTS as ARRAYS, however, to work with arrays in Python you will have to import a library, like the NumPy library.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-create-string-array-in-python
How to Create String Array in Python ? - GeeksforGeeks
July 23, 2025 - There are various ways to create array using list. In this method, when a list array is created, the values are assigned during the declaration. Later on an element can be added or removed from this array. ... Numpy module in Python is used for working with array. Using the numpy module's array() function, a string array can be created easily.
๐ŸŒ
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 the earlier tutorial, we learned how to convert list to string in Python. Here we will look at converting string to list with examples. We will be using the String.split() method to convert string to array in Python.
๐ŸŒ
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
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...
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
How to split one word into an array? - Python Help - Discussions on Python.org
September 9, 2020 - Uhh, i was wondering if i had a single word such as dog, how would i convert that into an array of [d,o,g]? I thought i could use the .split function but i doesnโ€™t work with an empty separator. I know the doubt is kind oโ€ฆ
๐ŸŒ
Quora
quora.com โ€บ How-can-I-import-an-array-of-string-in-Python-or-what-is-the-type-code-to-make-an-array-of-string-if-any
How to import an array of string in Python or what is the type code to make an array of string (if any) - Quora
If you have a supply of strings from somewhere and you want to put them in an array (list) then you often start with an empty list and append the strings, one by one. [code]>>> x = [] >>>...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_string_split.asp
Python String split() Method
The split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ how to create array of strings in python
How to Create Array of Strings in Python - Spark By {Examples}
December 20, 2024 - There is no built-in array data structure in Python specifically for storing strings. However, you can use a list to create an array of strings. First, create an empty array and then add stings to the array using the append() function.