-5

say

str(q)=hello
str(w)=332
str(e)=nasa
str(r)=21

What should I do in order to print <hello,332,nasa,21>? Note that brackets and commas are required

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Peter Jiang
  • 117
  • 7

5 Answers5

3

Use format:

print("<{},{},{},{}>".format(q,w,e,r))
Raju Pitta
  • 606
  • 4
  • 5
0

Use string format.

result = "<%s,%s,%s,%s>" % (q, w, e, r)

Sraw
  • 18,892
  • 11
  • 54
  • 87
0
Python 2.7.13 (default, Dec 18 2016, 07:03:39)
>>> q = 'hello'
>>> w = 332
>>> e = 'nasa'
>>> r = 21
>>> print('<{}>'.format(','.join(map(str, [q, w, e, r]))))
<hello,332,nasa,21>

This also helps when you have the variables to print already in a list, or a lot of them.

warvariuc
  • 57,116
  • 41
  • 173
  • 227
0

You can add all elements to a list,which will be a more clear method:

myList = ["hello","332","nasa","21"];
myList = ','.join(map(str, myList))
print "<" + myList + ">"

As you can iterate over it in future.

utkarsh
  • 33
  • 2
  • 10
0

If you are using Python 3, you can use the following format:

print(f'<{q},{w},{e},{r}>')

<hello,332,nasa,21>

Source: https://www.youtube.com/watch?v=bQQqxysLIGE&list=PLlrxD0HtieHhS8VzuMCfQD4uJ9yne1mE6&index=11