0

I have data that looks similar to following:

q = (0,0,0)

I wanted to convert the numeric tuple to a string. Many people on the internet offered a solution that looked like this:

user = 45
permission = (43, 12, 65, 34, 67)
final = "{},'{}'".format(user, ','.join(str(p) for p in permission))
print(final)

Source: Trying to convert a numerical tuple into a string.

Unfortunately, I kept getting an error message that read: TypeError: 'int' object is not callable. So, I tried other solutions like this ','.join(map(str, q)). No avail.

Is there something wrong with my application? Please help. Expected output:

q
"0" "," "0" "," "0"

or the following as string:

0,0,0
5
  • 3
    Could you provide your expected output please? Commented Dec 12, 2020 at 22:12
  • added expected output. Commented Dec 12, 2020 at 22:14
  • you are printing user also, should it be in the expected output or not Commented Dec 12, 2020 at 22:16
  • This answer is from the question you linked: ','.join(str(x) for x in q) and works with your tuple just fine. Commented Dec 12, 2020 at 22:16
  • The code in your question prints 45,'43,12,65,34,67'. What would you like the result of that input be? Commented Dec 12, 2020 at 22:18

2 Answers 2

5

You can use a mix of list comprehension and ','.join():

q = (0,0,0)
output = [j for j  in [','.join([str(x) for x in q])][0]]

This outputs:

['0', ',', '0', ',', '0']

Edit: Given OP accepts the string as is too, you can avoid the outer list comprehension and just use:

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

Comments

1
user = 45
permission = (0,0,0)
final = "'{}'".format(','.join(str(p) for p in permission))
print(final)

this returns 0,0,0

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.