• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 423
  • Last Modified:

removing whitespace in concatenation of data during output

Hello,

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:

0100

Is there a way to remove this whitespace?

Thanks,
Brian
0
bnblazer
Asked:
bnblazer
  • 4
  • 2
1 Solution
 
b0lsc0ttCommented:
bnblazer,

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)

b0lsc0tt
0
 
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.
0
 
bnblazerAuthor Commented:
Additionally, if I just replace the first comma with a +, it adds the values of the two index positions.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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)
0
 
b0lsc0ttCommented:
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.
0
 
ramromconsultant Commented:
Even better: print '%s%s%s' % (colors.index(color1), colors.index(color2), '0'*colors.index(color3))
0
 
bnblazerAuthor Commented:
Ding!

I like that one ramrom.  How pythonic of you :)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now