# Removing first and third zeros of a fieldname in label expression

The fieldname only has three characters. I dont want to display zeros in the first and third values. example below.

010 = 1

100 = 10

01Y = 1Y

10Y = 10Y

I have figured out how to strip out zeros except for the second value.

Use Python as the parser, and check the Advanced box. Then just replace `yourFieldName` with the name of the field you want to label.

``````def FindLabel([yourFieldName]):

value = [yourFieldName]

first = False
third = False

if value == "0":
first = True
if value == "0":
third = True

if first == True and third == False:
return value[1:]

elif first == False and third == True:
return value[0:2]

elif first == True and third == True:
return value[1:2]

else:
return value
``````

For brevity's sake:

``````def FindLabel ([field]):
val = [field]
return ''.join([j for i, j in enumerate(val) if j != '0' or i == 1])
``````

Step through the string and toss away all 0's unless it's in the middle position (index of 1), then join what's left.

• Thank you. This worked great. I am still learning. Going to look into how that third line works. May 21 '15 at 12:57

Just for good measure, here's a way to use python string methods. This would work with strings of variable length, though I know that wasn't a consideration in this case:

``````def FindLabel(value):

if value.endswith("0"):
value = value[:-1]  # slice all but the last character if it's a 0
return value.lstrip("0") # take off any leading zeros
``````

Here's a little more concise way of doing it. Basically convert it to a list and replace list and list with '' if the value is 0

``````def FindLabel([field]):
value = [field]
l_value = [x for x in value]    # turns string to list

no_zero_positions = [0,2]       # where you don't want zeros
for p in no_zero_positions:
if l_value[p] == '0':
l_value[p] = ''         # Replace zero with empty string

return ''.join(l_value)        # list back to string
``````
• So replace value with the fieldname? I am getting errors running this exactly as it is written. May 20 '15 at 18:48
• change it to `for i in no_zero_positions` May 20 '15 at 18:54
• yes, I noticed that and changed it to a p instead of an i. And yes just replace !YOUR_FIELD! with your field name May 20 '15 at 19:09