1

This one should be quick but for some reason I can't think of it. So say I have a list of elements n length (the number of elements will change at some point in the future). However, let's use this one as an example:

['ESZ12', 'ESH13', 'ESM13', 'ESU13', 'ESZ13']

Now what I would like is to output this into a string that looks like this:

("ESZ12", "ESH13", "ESM13", "ESU13, "ESZ13")

The first one is indeed a list of elements and the second is just a complete string.

Thanks

3
  • 3
    2nd one is not a string, it's a tuple. You can get it using tuple(your_list). Commented Nov 28, 2012 at 17:17
  • The second one is not a string, but a tuple. Commented Nov 28, 2012 at 17:17
  • 1
    .. into a string that looks like a tuple .. Commented Nov 28, 2012 at 17:18

7 Answers 7

4
In [4]: l = ['ESZ12', 'ESH13', 'ESM13', 'ESU13', 'ESZ13']

In [5]: print('(' + ', '.join('"%s"' % el for el in l) + ')')
("ESZ12", "ESH13", "ESM13", "ESU13", "ESZ13")
Sign up to request clarification or add additional context in comments.

3 Comments

I would simply do str(tuple(l)).
@AshwiniChaudhary: The output of that would use different quotes to what's specified in the question. More generally, the format would be entirely at the mercy of the Python implementation, and outside the programmer's control.
How about print str(tuple(lis)).replace("'",'"').
3

If you want a string that looks like a tuple the easiest way is convert to a tuple:

>>> l = ['ESZ12', 'ESH13', 'ESM13', 'ESU13', 'ESZ13']
>>> str(tuple(l))
"('ESZ12', 'ESH13', 'ESM13', 'ESU13', 'ESZ13')"

5 Comments

@Aniket how's it wrong, just do print str(tuple(l)), what you're seeing is a repr() version , that is due to the console.
The output needed "(double quotation) and not single quotation.
("ESZ12", "ESH13", "ESM13", "ESU13", "ESZ13") is required, and the output is "('ESZ12', 'ESH13', 'ESM13', 'ESU13', 'ESZ13')"
@Aniket that can be easily managed by replace() : str(tuple(lis)).replace("'",'"')
@AshwiniChaudhary without the replace() it is wrong output. And that's what my comment was about.
2

use join function :

>>> a = ['ESZ12', 'ESH13', 'ESM13', 'ESU13', 'ESZ13']
>>> '("' + '", "'.join(a) + '")'
'("ESZ12", "ESH13", "ESM13", "ESU13", "ESZ13")'

Comments

0

Turn it into a string?

"".join(my_list)

Turn it into a tuple?

my_tuple = tuple(my_list)

Comments

0

the expected output is a tuple. If you really meant string, then you can use:

strx = ''
strx.join(list)

1 Comment

Please don't name a variable after a builtin
0

Is this what you mean?:

    x = ['ESZ12', 'ESH13', 'ESM13', 'ESU13', 'ESZ13']
    y = str(tuple(x))
    print y

You get:

    "('ESZ12', 'ESH13', 'ESM13', 'ESU13', 'ESZ13')"

As a result.

Comments

0

Although % formatting has been deprecated, there are cases where it is useful. For example, the code

x = ['ESZ12', 'ESH13', 'ESM13', 'ESU13', 'ESZ13']
print '("%s")' % '", "'.join(x)

produces:

("ESZ12", "ESH13", "ESM13", "ESU13", "ESZ13")

Of course, changing the print to

print '("{}")'.format('", "'.join(x))

gives the same result, without using deprecated %.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.