Select to view content in your preferred language

Best Way to Shorten a Numeric Label

10-13-2016 09:53 PM
New Contributor

Hello everyone,

I am not new to ArcMap, however I am in need of some help. I want to shorten a numeric label. Specifically I want to shorten 3001522708 to 22708. This data is tabulated into an attributes table, and is displayed easily by the labels tab in properties, however it is to long. I have already looked up other methods to shorten and/or change the labels using VBScript, Python, or JScript but I do not know the specific code to remove the first 5 numbers. 

0 Kudos
5 Replies
MVP Alum

Here's background on what one can do with labeling:

Building label expressions—Help | ArcGIS for Desktop 

As for your specific request, in VB Script I believe one would employ a RIGHT function and in Python one would use SLICING.

Right Function VB Script

Cutting and slicing strings in Python - Python Central 

Chris Donohue, GISP

MVP Emeritus

The trouble with conversions is whether the conditions are already met.  In your case you indicate a slicing off of the first 5 positions.  This implies that

  • there will always be more than 5 characters
  • the returned result will be of the same form as the input

Python examples, since they tend to be simple to understand ...

>>> a = 3001522708
>>> str(a)[5:]
>>> int(str(a)[5:])

line 1   your input is a number

line 2   conversion to string is required for slicing, and keep only the 'positions' after the 5th

line 3   the result is a conversion of type to string with the first 5 removed

line 4   assuming that the original numeric format is needed, this needs to be returned to integer

line 5   the result after conversion to string, slicing, conversion back

An example of an exception and what to do, can get complex, 

>>> int(str(a)[5:]) if len(str(a))>5 else -999

So attention to the small details is important.

MVP Notable Contributor

Not sure if you got this figured out, but this is one way.

this will label with the last 5 digits of the numeric field as long as there are 5 or more digits.  If less than 5, it will just label with however many it can get.

>>> a = 3001522708
>>> str(a)[-5:]
>>> a = 1234
>>> str(a)[-5:]
MVP Emeritus

You shouldn't really mark your own thread helpful... it draws moderator attention as well as those that posted in the thread

0 Kudos
MVP Notable Contributor

Thanks, didn't realize I did that.

For that matter, didn't know that you could.

Apparently I can't "un-mark" it?


0 Kudos