help adding string module to mu or any other ide
help adding string module to mu or any other ide
Hi
New member & new micro Python user here
I am developing a microbit project which needs the zfill function from the string module.
How do I add this module to mu or any other IDE?
Any help would be appreciated
Thanks
New member & new micro Python user here
I am developing a microbit project which needs the zfill function from the string module.
How do I add this module to mu or any other IDE?
Any help would be appreciated
Thanks
Re: help adding string module to mu or any other ide
Can't you just implement that function in Python yourself?
Re: help adding string module to mu or any other ide
Just in case you can't, here is a try:
The difference to the built-in method: instead of
you have to call
And yes, this function may consume more heap space than a built-in method.
Code: Select all
def zfill(s, width):
if len(s) < width:
return ("0" * (width - len(s))) + s
else:
return s
Code: Select all
s.zfill(width)
Code: Select all
zfill(s, width)
Re: help adding string module to mu or any other ide
Many thanks to you both
Robert I am certainly NOT able to , am getting back into a little programming after a long break. Last programming I did was on a commodore pet & zx81!!!
Can't wait to get home and try your code , looks like it will do just what I need
Regards
JKD
Robert I am certainly NOT able to , am getting back into a little programming after a long break. Last programming I did was on a commodore pet & zx81!!!
Can't wait to get home and try your code , looks like it will do just what I need
Regards
JKD
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: help adding string module to mu or any other ide
An alternative (more efficient) approach is:
Code: Select all
def zfill(s, width):
return '{:0>{w}}'.format(s, w=width)
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: help adding string module to mu or any other ide
Hi peter
Many thanks
Many thanks
Re: help adding string module to mu or any other ide
My two cents is it would be nice to use zfill the same way as in the standard python library, especially when using micropython unix.
I also know if I am complaining that I should contribute to the project and add it myself
I also know if I am complaining that I should contribute to the project and add it myself
_______________
Travis Travelstead
Travis Travelstead
Re: help adding string module to mu or any other ide
There is a reason why it's called MicroPython.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: help adding string module to mu or any other ide
Indeed. New users are advised to read this https://github.com/micropython/micropyt ... ifferences.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: help adding string module to mu or any other ide
Many thanks to all the kind & helpful people who have helped with advice & code.
Really appreciate all the time & trouble you went to !
Regards
Jason
Really appreciate all the time & trouble you went to !
Regards
Jason