Jump to content

padding a string woth 000


---
 Share

Recommended Posts

Hello,

I have a string of 1 to 4 characters long.

In the table I need them padded with "0" character.

1 => 0001

25 => 0025

In the expression editor I tried using the python functions zfill and rjust on a string. No success.

How can I achieve this?

Thanks

Johnny

 

Link to comment
Share on other sites

Well I got a solution. Don't know if it is most elegant and efficient.

#@ Create cad point name
number_ = (name).split (".")[0][1:]
len_number = len(number_)
i = 1
while i < ( 5 -len_number):
	number_ = "0" + number_
	i = i +1
user_bmwimport_ipe_type+"_"+name[0]+"_"+number_

 

Link to comment
Share on other sites

  • 2 months later...

in Python 2 (and Python 3) you can do:

print ("%04d" % 1) #4 is the number of digits you want
#print : 0001

For Python 3.+, the same behavior can also be achieved with format:

print("{:04d}".format(1))
#print : 0001

For Python 3.6+ the same behavior can be achieved with f-strings:

print(f"{1:04d}")
#print : 0001

the 3 methods works in gom

Link to comment
Share on other sites

 Share

×
×
  • Create New...