removing whitespace in concatenation of data during output


I am re-factoring a script I have that used to use a dictionary but now is a list.  I need to be able to print the index value of positions in the list without any whitespace between them.  Here is a code snippet:

#!/usr/bin/env python

def getOhms (color1, color2, color3):
    """Version 2 of original getOhms using a list instead of a dictionary"""
    colors = ['black', 'brown', 'red', 'orange', 'yellow', 'green', 'blue',
              'violet', 'grey', 'white']
    print colors.index(color1), colors.index(color2), '0'*colors.index(color3)
getOhms('black', 'brown', 'red')

Run this way the output is:

0 1 00

I need it to be:


Is there a way to remove this whitespace?

Who is Participating?
ramromconsultant Commented:
Even better: print '%s%s%s' % (colors.index(color1), colors.index(color2), '0'*colors.index(color3))
b0lsc0ttIT ManagerCommented:

Try using the plus sign (+) instead of the comma.  The comma adds the space and you should be able to use the plus sign in its place in that spot in your code.

print colors.index(color1) + colors.index(color2)

bnblazerAuthor Commented:
I tried that, but I get this error:

TypeError: unsupported operand type(s) for +: 'int' and 'str'

I think that it is grousing about the fact that the index of the list is an int and that the final portion of the statement produces a string.
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

bnblazerAuthor Commented:
Additionally, if I just replace the first comma with a +, it adds the values of the two index positions.
bnblazerAuthor Commented:
I found the answer I needed to do some string conversions:

print '%s' %colors.index(color1) + '%s' %colors.index(color2) + '0'*colors.index(color3)
b0lsc0ttIT ManagerCommented:
Sorry I wasn't quicker with the reply and answer.  Good job on the answer.  You can post a question in Community Support and have them PAQ the question, accept your answer, and refund the points.  Don't forget to include the URL to this question in that post.  You probably already know this but if you do have a question then let me know.
bnblazerAuthor Commented:

I like that one ramrom.  How pythonic of you :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.