7

I have an array of integer, and I need to transform it into string.

[1,2,3,4] => '\x01\x02\x03\x04'

What function can I use for it? I tried with str(), but it returns '1234'.

string = ""
for val in [1,2,3,4]:
    string += str(val) # '1234'
1
  • 4
    str can't possibly have returned '1234'. str([1,2,3,4]) returns '[1, 2, 3, 4]'. (That's why it's best to show your code, instead of trying to describe it. You might have been very close to the solution, but we have no way of knowing, because we don't know what you did.) Commented Jun 13, 2013 at 0:08

3 Answers 3

12

''.join([chr(x) for x in [1, 2, 3, 4]])

Sign up to request clarification or add additional context in comments.

6 Comments

beat me to the punch by mere seconds. This is the answer.
I like this answer the best since it's basically Python version agnostic. As abarnert's answer shows, each of his solutions are highly dependent on the version of Python you're using.
@leetNightshade: "2.6-2.7 and 3.0+" is not exactly "highly dependent". That includes every version released in over 5 years, and likely for at least the next decade to come. I also gave the 2.x-only and 3.x-only versions because they look simpler and more idiomatic, but you can use the 2.x/3.x version if that's what you care more about. Meanwhile, this version isn't really agnostic, because it gives you a str rather than a bytes in 3.x, which is very unlikely to be what you want.
@abarnert Why would you want it to be bytes? In regards to this question, prosseek asked how to turn an array of integers into a string, not bytes.
@leetNightshade: In Python 2.x, "string" means str, which is used for both text strings and byte strings, so it doesn't matter. When porting to 3.x, you have to make an educated guess at which one is intended. I'm guessing the OP intends bytes because almost every reasonable use for a sequence of control characters—terminal escape sequences, network protocols, etc.—is an 8-bit context, not a Unicode text context. If he were thinking in terms of Unicode codepoints rather than 8-bit characters, U+0001 through U+0004 would be strange choices.
|
4

You can convert a list of small numbers directly to a bytearray:

If it is an iterable, it must be an iterable of integers in the range 0 <= x < 256, which are used as the initial contents of the array.

And you can convert a bytearray directly to a str (2.x) or bytes (3.x, or 2.6+).

In fact, in 3.x, you can even convert the list straight to bytes without going through bytearray:

constructor arguments are interpreted as for bytearray().

So:

str(bytearray([1,2,3,4])) # 2.6-2.7 only
bytes(bytearray([1,2,3,4])) # 2.6-2.7, 3.0+
bytes([1,2,3,4]) # 3.0+ only

If you really want a string in 3.x, as opposed to a byte string, you need to decode it:

bytes(bytearray([1,2,3,4])).decode('ascii')

See Binary Sequence Types in the docs for more details.

1 Comment

Nice answer, I forgot about bytearray()
2

Simple solution

digits = [1,2,3,4]
print(''.join(map(str,digits)))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.