bnblazer
asked on
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
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
ASKER
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.
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.
ASKER
Additionally, if I just replace the first comma with a +, it adds the values of the two index positions.
ASKER
I found the answer I needed to do some string conversions:
print '%s' %colors.index(color1) + '%s' %colors.index(color2) + '0'*colors.index(color3)
print '%s' %colors.index(color1) + '%s' %colors.index(color2) + '0'*colors.index(color3)
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ding!
I like that one ramrom. How pythonic of you :)
I like that one ramrom. How pythonic of you :)
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