How to remove first two and the last two characters from each label in Arcmap?

4363
3
Jump to solution
02-07-2018 12:54 AM
GregoryBrown3
New Contributor

Hello. My too long labels take up too much space in regard to the small counties polygons.

I want to remove first two and the last two characters from each single label.

 

Currently the labels on the map look like this:

PL1607000

PL1816000

LV801200

LT029900

EE700000

etc.

Because in each single label the first two characters are two-letter country code, and the last two characters always two zeros, to make map more clean I want my labels like this:

16070

18160

8012

0299

7000

etc.

 

I use Maplex Label Engine. My label field is CTYCode, What expression should I type in Label expression dialog box?

Is it possible to also make stacked labels from this labels? I want to the first two characters were at the top, and the rest at the bottom. Like this:

16
070

18
160

80
12

02
99

70
00

etc.

If you want help me please add here two different expressions. With and without stacking labels, I do not know about programming at all and I would not know how to change the expression

Greg

0 Kudos
1 Solution

Accepted Solutions
PeteCrosier
Occasional Contributor III

Go in to the expression editor, turn on advanced expressions, set the parser to Python, put this as the expression for stacking:

def FindLabel ( [CTYCode] 😞
  return "%s\r\n%s" % ([CTYCode][2:4], [CTYCode][4:-2])

And without stacking:

def FindLabel ( [CTYCode] 😞
  return ([CTYCode][2:-2])

The best things to read up on would be string substitution (the %s stuff), slicing and indexing (the [2:4], [4:-2], etc. stuff) in Python.

View solution in original post

3 Replies
PeteCrosier
Occasional Contributor III

Go in to the expression editor, turn on advanced expressions, set the parser to Python, put this as the expression for stacking:

def FindLabel ( [CTYCode] 😞
  return "%s\r\n%s" % ([CTYCode][2:4], [CTYCode][4:-2])

And without stacking:

def FindLabel ( [CTYCode] 😞
  return ([CTYCode][2:-2])

The best things to read up on would be string substitution (the %s stuff), slicing and indexing (the [2:4], [4:-2], etc. stuff) in Python.

GregoryBrown3
New Contributor

Thank you for help!

0 Kudos
DanPatterson_Retired
MVP Emeritus

For future reference... if you don't know the length of the object to slice... but you know that you want to slice of 2, but be careful, you may not get what you want

a = 'PL1607000'

(a[2:])[:-2]

'16070'‍‍‍‍‍

a = 'PL1'

(a[2:])[:-2]

''