473,394 Members | 1,889 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Formatting numbers with leading zeros

Hi, I'm sort of new to Python and I've just discovered the amazing
formatting using the % operator. One thing I can't figure out is how
to format numbers so that they get leading zeros, for example I want
the output 1x01 instead of 1x1 (used in naming my avi files of shows).
I've used this so far:

print '%sx%s'%(season,episode)

which gives the output 1x1, but I want 1x01 =p Any way to format it
using the % method?
Jul 18 '05 #1
2 4691
On Sun, 19 Sep 2004 12:59:21 -0700, Stian wrote:
One thing I can't figure out is how
to format numbers so that they get leading zeros

... ... ...

print '%sx%s'%(season,episode)

which gives the output 1x1, but I want 1x01


I think you are looking for the string method zfill();
you can find it here in Python's Library Reference:

http://www.python.org/doc/2.3.4/lib/string-methods.html

If e.g. episode = '1', episode.zfill(2) will return '01'
and episode.zfill(3) will return '001' etc. etc.

HTH, Th.

Jul 18 '05 #2
"Stian" wrote:
Hi, I'm sort of new to Python and I've just discovered the amazing
formatting using the % operator. One thing I can't figure out is how
to format numbers so that they get leading zeros, for example I want
the output 1x01 instead of 1x1 (used in naming my avi files of shows).
I've used this so far:

print '%sx%s'%(season,episode)

which gives the output 1x1, but I want 1x01 =p Any way to format it
using the % method?


assuming that season and episode are integers:

print "%dx%02d" % (season, episode)

(if not, use int() to convert from strings to integers)

more info here:

http://docs.python.org/lib/typesseq-strings.html

</F>

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
3
by: cody | last post by:
How can I format numbers with leading zeros? I believe I did this before some time ago but I cannot remember how I did it. I want the output be 100305 for 10:03:05. string fname =...
2
by: Ingo Nolden | last post by:
Hello, I want to format floats ( doubles ) into small strings of 7 characters. I tried to use std::ostream, but I don't get the desired behaviour. Especially in scientific notation I am not...
4
by: Dave Brydon | last post by:
Access 2003 I have a combo box in my personnel table, which draws its data from a trade code table; the original field in the code table, is numeric, Long Integer, and formatted with 5 zero's . ...
5
by: Bilgehan.Balban | last post by:
Hi, I use %#08x to print unsigned integers in hexadecimal format. According to C ref. man. Harbison & Steele, #08 stands for "pad the number with up to 8 zeroes to complete it to 8 digit...
2
by: chris | last post by:
Hi, I have a simple ms access application that allows you to scan barcodes in to a form which stores them in the database. The barcodes are 6 digits in length e.g. 555666 but my handheld...
5
by: Code::Tyr | last post by:
Hello. I have a simple problem and need help. I am developing a simple file system that stores info based on folder names. Now, I only have one problem. How do i format (int i = 2) into a string...
4
by: Vlinder | last post by:
I'm completely new to PHP (jumped ship this week from MSAccess and ASP to MySql and PHP) and I'd appreciate some help with leading zeros in telephone numbers. I've got a field with type...
2
by: lawpoop | last post by:
Hello all -- I'm trying to format decimal numbers for a report. I would have to have the number with leading zeros to the tens' place, and trailing zeros to the thousandths' place. I'm using...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.