-4

I have a variable below:

weatherWind = int(round(c["windSpeed"])), "mph", bearing_to_direction(c["windBearing"])

at the moment windSpeed and windBearing are value taken from the Dark Sky weather API. However when I print this out it has the brackets and quotations in it:

(8, 'mph', 'W')

All my other variables print out correctly. i would like it to print out like this:

8 mph W
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Ross
  • 2,463
  • 5
  • 35
  • 91
  • 7
    That's not a string; it's a tuple. You may have gotten mixed up by the `print` syntax, which can take multiple objects to print, none of which are required to be strings. – user2357112 Jul 07 '16 at 19:43
  • Quite Quite similar to [python print array without brackets in a single row](http://stackoverflow.com/q/11178061), Try to Use `' '.join(map(str,weatherWind))` (you'll understand if you refer to the other linked post) – Bhargav Rao Jul 07 '16 at 19:43
  • @ user2357112 Ahh thank you. Manage to fix it with that information. @m_callens no I am very new. I like to set myself project to learn – Ross Jul 07 '16 at 19:46
  • 2
    why the hate for a newer user? everyone here was new at this at some point in their life... – Aaron Jul 07 '16 at 19:46
  • @Aaron The question shows a considerable lack of research, in the form of not knowing very basic Python concepts. It also lacks the code needed to reproduce the error: what is being used to print the variable? – chepner Jul 07 '16 at 19:51
  • @chepner my comment was directed at another comment now deleted... – Aaron Jul 07 '16 at 19:59
  • @chepner: I had no problem reproducing the issue. The OP does show roughly what they did, and what output they expected and what they got instead. As someone that is currently working in unfamiliar programming languages, I can sympathise how hard certain things that seem basic to us old-hands can be. I still have to be told to use `!== null` and not `is_null()` in PHP / Hack, for example, but can't tell you why at this point ;-) – Martijn Pieters Jul 07 '16 at 20:00

2 Answers2

2

You created a tuple with 3 values in it, an integer, the string 'mph' and the string 'W'. If you wanted to make this one string, use string formatting:

weatherWind = '{:.0f} mph {}'.format(c["windSpeed"], bearing_to_direction(c["windBearing"]))

This inserts the windspeed and bearing information into {..} slots in a string with mph already there. The .0f formatting instruction rounds your floating point windspeed to show just the integer portion, rounded.

You can then print that string without any further quotes or parentheses:

>>> bearing_to_direction = lambda d: 'NSWE'[d]
>>> c = {'windSpeed': 7.92, 'windBearing': 2}
>>> print '{:.0f} mph {}'.format(c["windSpeed"], bearing_to_direction(c["windBearing"]))
8 mph W
>>> c = {'windSpeed': 3.14, 'windBearing': 1}
>>> print '{:.0f} mph {}'.format(c["windSpeed"], bearing_to_direction(c["windBearing"]))
3 mph S
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thank you. so I quickly changed it to `weatherWind = str(int(round(c["windSpeed"]))) + " mph " + bearing_to_direction(c["windBearing"])` would your way be better in terms of coding correctly? – Ross Jul 07 '16 at 19:51
  • 2
    @RossWatson: using `str.format()` is clearer and more flexible. It is easier to move things around and add more slots, for example. – Martijn Pieters Jul 07 '16 at 19:52
2

Somewhat confusingly,

print a, b, c

does something completely different from

x = a, b, c
print x

When you do print a, b, c, Python calls str on each object separately to get string representations of the objects and prints those strings, separated by spaces.

When you do x = a, b, c, you're building a tuple. No strings are produced at this time. When you do print x, Python then calls str on the tuple and prints the result. The str representation of a tuple is very different from the print a, b, c output.

If you want a string that looks like the output of print a, b, c, the generic way to do that is

x = ' '.join(map(str, [a, b, c]))

Depending on what you're printing, there may be more convenient ways to do it; for example, string formatting, as suggested by Martijn.

user2357112
  • 260,549
  • 28
  • 431
  • 505