Proper case for names beginning with Mc, like McDavid

438
6
Jump to solution
02-13-2024 10:09 AM
Shauna-RaeBrown
Occasional Contributor

I have a featureclass of street names where all of the names are in uppercase.  I can set a label class using an expression STN begins with "MC%", but I want python to label the streets starting with MC to convert the label to proper case, for example, MCDAVID would be McDavid.  Does anyone have a suggestion for using python to do this?  I've already tried using [STN].title(), but that yields Mcdavid.  Thanks for the suggestions.

0 Kudos
1 Solution

Accepted Solutions
Shauna-RaeBrown
Occasional Contributor

Your little script is almost correct.  I had to change nam[0:1] to nam[0:2], because the "c" got skipped and yielded MDavid.  The following works, and yields McDavid.  Thanks so much!!!

def FindLabel ( [STN] ):
    nam = [STN].lower()
    nam = nam.replace("mc", "Mc")
    nam = nam[0:2] + nam[2].capitalize() + nam[3:]
    return nam

 

View solution in original post

6 Replies
AlfredBaldenweck
MVP Regular Contributor

You could always do a replace?

STN.replace("MC", "Mc")
0 Kudos
Shauna-RaeBrown
Occasional Contributor

That partially works.

STN.replace("MC", "Mc")

returns McDAVID.  What I want is McDavid.  Thanks for your help.  I'll keep trying. 

0 Kudos
AlfredBaldenweck
MVP Regular Contributor

Ohhh it's all in caps. Something like this should work?

I'm not putting in an if statement because you have it handled by the Label class query.

 

nam = STN.lower()
nam = nam.replace("mc", "Mc")
nam = nam[0:2] + nam[2].capitalize() + nam[3:]
return nam

 

Edited to change my slicing since I mixed it up a bit.

Shauna-RaeBrown
Occasional Contributor

Your little script is almost correct.  I had to change nam[0:1] to nam[0:2], because the "c" got skipped and yielded MDavid.  The following works, and yields McDavid.  Thanks so much!!!

def FindLabel ( [STN] ):
    nam = [STN].lower()
    nam = nam.replace("mc", "Mc")
    nam = nam[0:2] + nam[2].capitalize() + nam[3:]
    return nam

 

AlfredBaldenweck
MVP Regular Contributor

That's what I get for posting without testing lol. Glad it works.

0 Kudos
RhettZufelt
MVP Frequent Contributor

Probably a better way, but his is how I've handled that in the past.

I have a dictionary of the names that get whacked out when title casing and substitute the value when has a matching key.

subDict = { 
                "MCMURRAY ST": "McMurray St",
                "MCMURRAY AVE": "McMurray Ave",
                "MCINTOSH CT": "McIntosh Ct",
                "MCEWAN DR": "McEwan Dr",
                "MCPHERSON AVE": "McPherson Ave",
                "MCCLELLAN ST": "McClellen St",
                "BY-PASS": "Bypass Hwy         SR 240",
                "O'CONNOR ST":"O'Connor St",
                "MCMURRAY": "McMurray",
                "MCINTOSH": "McIntosh",
                "MCEWAN": "McEwan",
                "MCPHERSON": "McPherson",
                "MCCLELLAN": "McClellen",
                "O'CONNOR":"O'Connor"
                }
def FindLabel ( [Street] ):
      st = [Street]
      label = [Street].title()
      if st in subDict.keys():
             label = subDict[st]
      return label

R_

 

0 Kudos