Python 2.7.6 help with white spaces?

  • Thread starter Scott W Dunning
  • Start date
S

Scott W Dunning

I am having trouble figuring out how to remove spaces….

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

I’m VERY new to python and coding in general and this is a question for a class I’m in for a test review. So, the most basic answer would be appreciated.

Thanks for any help!!

Scott
 
R

Roy Smith

Scott W Dunning said:
I am having trouble figuring out how to remove spacesŠ.

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

print "%d:%02d" % (minutes, seconds)

The "02" for the seconds specifier makes sure you get exactly two
digits, with a leading zero if needed.
 
S

Scott W Dunning

what exactly is the “%d:%02d”% saying?



print "%d:%02d" % (minutes, seconds)

The "02" for the seconds specifier makes sure you get exactly two
digits, with a leading zero if needed.
 
R

Roy Smith

what exactly is the “%d:%02d”% saying?

Python uses string format specifiers similar to C's printf()

%d means, "convert an integer to a decimal string"

%2d means the same, plus, "make the result 2 columns wide"

and, finally, %02d means, "and, if it would normally be less than 2 columns, zero-pad it on the left".

So, putting that all together, we've got:

"%d" --> decimal integer
":" --> a literal ":"
"%02d" --> another decimal integer, this time zero-padded to two columns
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top