newbie question: getting rid of space in string :(
Martin Franklin
mfranklin1 at gatwick.westerngeco.slb.com
Fri Jul 12 12:59:05 EDT 2002
On Friday 12 Jul 2002 5:52 am, Joseph Youssef wrote:
> Hello, I'm new to python and I'm writing a very small script which takes
> each letter of a string and adds text to it (I know this is not a good
> explanation but you'll understand below :) )
>
> anyway, this is my current code:
>
> def textColor(string, color1, color2):
> color = color1
> for a in string:
> print "<" +color+ "><b>" +a+ "</b></" +color+ ">",
Joseph,
The comma at the end of the print statment adds a space to the 'string' you
are printing. There are several ways to avoid this, perhaps building the
string up then printing it in one go at the end.. like so:
def textColor(string, color1, color2):
wholeString=""
color = color1
for a in string:
wholeString=wholeString+"<" +color+ "><b>" +a+ "</b></" +color+ ">"
if color == color1:
color = color2
else:
color = color1
print wholeString
as a matter of taste I would use this:-
wholeString=wholeString+"<%s><b>%s</b></%s>" %(color, a, color)
but as I say this is a matter of taste!
Regards
Martin
> so when I run let's say textColor("testing","black","red")
> it returns:
> <black><b>t</b></black> <red><b>e</b></red> <black><b>s</b></black>
> <red><b>t</b></red> <black><b>i</b></black> <red><b>n</b></red>
> <black><b>g</b></black>
>
> now this works just fine exept that space in between each block of text, I
> can't get it to stick together, now I know this is a very easy question but
> I need some quick help
>
> thanks
More information about the Python-list
mailing list