If you are going to be playing with string representations of native types, you probably want to take a look at the struct standard library package.

With it, you can quickly process your r as follows:

>>> import struct
>>> ''.join(struct.pack('i', rr) for rr in r)
'012345678\x00\x00\x00\x00\x00\x00\x00'

And you can play with the ordering of the characters by explicitly describing the endianess of your input:

>>> ''.join(struct.pack('>i', rr) for rr in r)  # big endian
'32107654\x00\x00\x008\x00\x00\x00\x00'
Answer from Jaime on Stack Exchange
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-program-to-convert-a-list-to-string
Python Program to Convert a List to String - GeeksforGeeks
May 8, 2026 - Another approach is to use a loop (for loop) to iterate through the list and concatenate each element to a result string. This method provides more control if we need to manipulate each element before adding it to the string. ... a = ["Python", "is", "simple"] res = "" # Convert list to string using a loop for x in a: res += x + " " print(res.strip()) # Remove trailing space
Discussions

Converting list to string
Use the join method on strings: ''.join(password) More on reddit.com
🌐 r/learnpython
14
8
December 22, 2024
python - Conversion of an array of integers to string - Code Review Stack Exchange
I wrote a function that convert an encoded string back to its original string. I am new to Python, so I would like to know whether or not I can simplify my code. The string is encoded in such way that I need to reverse the order of each group of 4 chars. #!/usr/bin/python def arrayOfInt2string(ar... More on codereview.stackexchange.com
🌐 codereview.stackexchange.com
September 3, 2015
How do I convert Numpy Array to a List of Strings?
what is x ? Can you share small snippet of your code? More on reddit.com
🌐 r/learnpython
6
1
December 2, 2020
How to convert a string with comma-delimited items to a list in Python? - Stack Overflow
How do you convert a string into a list? Say the string is like text = "a,b,c". After the conversion, text == ['a', 'b', 'c'] and hopefully text[0] == 'a', text[1] == 'b'? More on stackoverflow.com
🌐 stackoverflow.com
People also ask

What is the easiest way to convert an array into a string in Python?
The simplest way is to use the join() method with your desired separator, such as space, comma, or hyphen. This method combines all array elements into a single text.
🌐
wscubetech.com
wscubetech.com › resources › python › programs › array-to-string
Convert Array to String in Python (6 Programs)
How to convert a multidimensional array into a string in Python?
For multidimensional arrays, you can use nested join() calls or list comprehensions to flatten them before joining. Alternatively, use str(array) for a structured string format.
🌐
wscubetech.com
wscubetech.com › resources › python › programs › array-to-string
Convert Array to String in Python (6 Programs)
Is it possible to convert only part of an array into a string in Python?
Yes, slice the array first, like array[1:4], then use join() on that slice. This will convert and merge only the selected portion of the array.
🌐
wscubetech.com
wscubetech.com › resources › python › programs › array-to-string
Convert Array to String in Python (6 Programs)
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.array2string.html
numpy.array2string — NumPy v2.2 Manual
String representation of the array. ... If a formatter is specified for a certain type, the precision keyword is ignored for that type. This is a very flexible function; array_repr and array_str are using array2string internally so keywords with the same name should work identically in all three functions. ... >>> import numpy as np >>> x = np.array([1e-16,1,2,3]) >>> np.array2string(x, precision=2, separator=',', ...
🌐
Reddit
reddit.com › r/learnpython › converting list to string
r/learnpython on Reddit: Converting list to string
December 22, 2024 -

Hi everyone, I just wanted to know what is the best way to convert an entire list to a simple string element. I was trying out stuff I learned by coding a (very) simple password generator and I am stuck at figuring out how to output the password as a single string (without the empty spaces and brackets) instead of getting the entire array.

My code :

import string
import random
letters = list(string.ascii_lowercase)

#Generates a random password with default value of 3 numbers and 3 letters (x and y arguments)
def password_gen(x=3, y=3):

  char_list = []

  #Returns a random letter
  def add_letter() :
  return random.choice(letters)

  #Returns a random number (0-9)
  def add_number() :
  return int(random.random()*10)

  #Loops to add the correct number of random characters
  i = 0
  while i < x :
    char_list.append(add_letter())
    i+=1
    i=0
  while i < y :
    char_list.append(add_number())
    i+=1
  
  #Shuffles the list and returns it as the generated password
  random.shuffle(char_list)
  return char_list

password = str(password_gen(5, 5))
print(password)

Currently the output gives something looking like this : ['p', 3, 5, 9, 'o', 'v', 'a', 9, 't', 3] and I want it to look like this p359ova9t3

🌐
WsCube Tech
wscubetech.com › resources › python › programs › array-to-string
Convert Array to String in Python (6 Programs)
September 15, 2026 - Learn how to convert an array to a string in Python using 6 different programs. Explore methods with join(), for loops, list comprehension, and more with examples.
Find elsewhere
🌐
DataCamp
datacamp.com › home › tutorials › python › how to convert a list to a string in python
How to Convert a List to a String in Python | DataCamp
April 12, 2024 - If you're in a rush and need a quick solution to convert a list into a string, here are some handy methods. The str() function can convert the whole list, including its structure, into a string format. This is particularly useful for logging or displaying the list as is.
Top answer
1 of 2
5

If you are going to be playing with string representations of native types, you probably want to take a look at the struct standard library package.

With it, you can quickly process your r as follows:

>>> import struct
>>> ''.join(struct.pack('i', rr) for rr in r)
'012345678\x00\x00\x00\x00\x00\x00\x00'

And you can play with the ordering of the characters by explicitly describing the endianess of your input:

>>> ''.join(struct.pack('>i', rr) for rr in r)  # big endian
'32107654\x00\x00\x008\x00\x00\x00\x00'
2 of 2
4

First, your function name could use a more clear name. arrayOfInt2string contains a lot of information but none of it makes it clear what the user of function actually is. decode_str is more clear. Likewise, array is a bad parameter. The data you're passing is a tuple, not an array. But regardless the type is not a good indicator of what it is. encoded_str would be clearer. As much as possible, come up with relevant names to make your script easier to read.

You can initialise b just using b = []. But even better would be to use a list comprehension. A list comprehension collapses a for loop into a single line expression to generate a list. In your case, you can create your whole b list in just one line like this:

        b = [chr(int(a[i:i+2], 16)) for i in range(0, len(a), 2)]

It basically flips the syntax of your for loop around. You have your expression for each element of b followed by the syntax of your for loop. If you really wanted to, both for loops could be collapsed this way, but that would probably read as too complicated to follow.

For your s assignment, you can use the += operator. It basically says to add whatever is on the right of the operator to the current value of s. It's shorthand to essentially do what your code already does.

        s += ''.join(b.reverse())

Using ''.join(b.reverse()) is not strictly necessary. You can use the slicing operator to reverse it in a shorter line. You've used the slicing operator elsewhere, but you may or may not be aware of the third parameter it takes called the step parameter. It basically tells Python what amount to increment each step by, similar to the third parameter you pass to range. You can pass a negative value to start at the end and go backwards, so you can flip b like this: b[::-1].

🌐
StrataScratch
stratascratch.com › blog › python-list-manipulation-convert-lists-to-strings
Python List Manipulation: Convert Lists to Strings - StrataScratch
April 4, 2025 - Python’s join() method can’t handle None directly – it expects all items to be strings. If you pass a list with None, it’ll throw a TypeError. The solution is to use a comprehension or map() to convert all items to strings.
🌐
Python Morsels
pythonmorsels.com › turn-a-list-into-a-string
Convert a list to a string in Python - Python Morsels
December 6, 2021 - Because we know that the join method accepts any iterable of strings, we could write a generator expression that converts each of our items into a string by passing it to the built-in str function: >>> " ".join(str(n) for n in numbers) '2 1 3 4 7 11 18' So even if you're working with an iterable of non-strings, you can join them together as long as you do a little bit of pre-processing first. ... This works on any iterable-of-strings, not just lists of strings. Now make sure you remember what you just watched: Daily Recall shows you a few Python questions a day and brings each one back right before you’d forget it.
🌐
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 - " has both leading and trailing whitespace, whitespace between the words "Learning" and "Python", and whitespace between the word "Python" and the exclamation mark. When the string is converted to a list of characters, every whitespace is treated as an individual character and that's why you see empty spaces, ' ', as list items.
🌐
Stack Overflow
stackoverflow.com › questions › 5387208 › how-to-convert-a-string-with-comma-delimited-items-to-a-list-in-python
How to convert a string with comma-delimited items to a list in Python? - Stack Overflow
How do you convert a string into a list? Say the string is like text = "a,b,c". After the conversion, text == ['a', 'b', 'c'] and hopefully text[0] == 'a', text[1] == 'b'?
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-program-convert-string-list
Convert String to a List in Python - GeeksforGeeks
Given a string, the task is to convert it into a list. Depending on the requirement, we can convert the string into a list of characters, words, or values separated by a specific delimiter. For Example: Input: text = "Python is easy" Output: ['Python', 'is', 'easy']
Published: July 16, 2026
🌐
Vultr Docs
docs.vultr.com › python › third party › numpy › fromstring()
Python Numpy fromstring() - Convert String to Array
November 18, 2024 - Process these values into a floating-point array. ... In this example, each space-separated value in data is converted into a float, which can be particularly useful when dealing with decimal numbers in data analysis. Illustrate a scenario where hexadecimal numbers stored as a string need to be converted.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-ways-to-convert-array-of-strings-to-array-of-floats
Python | Ways to convert array of strings to array of floats - GeeksforGeeks
July 11, 2025 - Sometimes in a competitive coding environment, we get input in some other datatypes and we need to convert them into other forms this problem is the same as when we have an input in the form of string and we need to convert it into floats. Let's discuss a few ways to convert an array of strings in Python to an array of floats.
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
🌐
Python
docs.python.org › 3 › builtins › stdtypes.html
Built-in Types — Python 3.15.2 documentation
Some operations are supported by several object types; in particular, practically all objects can be compared for equality, tested for truth value, and converted to a string (with the repr() function or the slightly different str() function).
🌐
TutorialsPoint
tutorialspoint.com › article › python-program-to-convert-the-array-of-characters-into-the-string
Python Program to convert the array of characters into the string
March 27, 2026 - char_array = ['B', 'l', 'a', 'c', 'k'] result_string = "" i = 0 while i < len(char_array): result_string += char_array[i] i += 1 print("The string is:", result_string)
🌐
ConvertCSV
convertcsv.com › json-to-csv.htm
JSON To CSV Converter
THIS CONVERSION is NOW AVAILABLE as an API at ConvertCsv.io · Your JSON input should contain an array of objects consistings of name/value pairs. It can also be a single object of name/value pairs or a single object with a single property with an array of name/value pairs.