3

I have a list xline. It is initialized and then appended. But I am getting an error when I run?

xline = []
---#append
----
print (''.join(xline)) # Convert list into string

Run time error

    print (''.join(xline)) # Convert list into string
TypeError: sequence item 0: expected string, int found

What is wrong?

8
  • Can you show us more code please? More specifically, what are you doing with pline? Commented Oct 4, 2015 at 23:31
  • expected string, int found self explanatory. Commented Oct 4, 2015 at 23:31
  • Possible duplicate of Pythonic list to string method Commented Oct 4, 2015 at 23:53
  • print (''.join(map(str, xline))) Commented Oct 5, 2015 at 0:05
  • For debug output, consider the repr() function which should give you a string representation for pretty much anything. Commented Oct 5, 2015 at 4:41

1 Answer 1

5

You can use str() to transform each element:

print ''.join([str(x) for x in xline])
Sign up to request clarification or add additional context in comments.

1 Comment

The above method is more versatile than the map method as I was able to format. I liked map for simplicity but lacks formatting option or I do not know how to format with map.

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.