Python 2.7.6 help with white spaces?

Chris Angelico rosuav at gmail.com
Thu Feb 6 20:37:46 EST 2014


On Fri, Feb 7, 2014 at 12:22 PM, Scott W Dunning <swdunning at cox.net> wrote:
> Assume variables exist for minutes and seconds. Each variable is an integer.
> How would you create a string in the format,
>
> 3:11
>
> with no spaces. where 3 is minutes and 11 is seconds.
>
>
> Obviously when I…
>
> print minutes, “:”, seconds
>
> I get 3 : 11
>
> how can I get it to print with no spaces?
>
> 3:11

The print statement isn't right for what you're doing here. You need
to create a single string with that result, which you could then print
out.

There are two easy ways to do this. First one is to create a string
from the number of minutes, create a string from the number of
seconds, and concatenate them, with a third string (the colon) in
between. Do you know how to turn an integer into a string, and do you
know how to add strings together?

The second way is to format the values directly into a single string.
You can use printf codes for this. Check this out:

http://docs.python.org/2/library/stdtypes.html#string-formatting-operations

ChrisA



More information about the Python-list mailing list