if str(MyLayer.name[-1]).isDigit(): newName = str(MyLayer.name[:-1]) elif str(MyLayer.name[-2:].isDigit(): newName = str(MyLayer.name[:-2])
Solved! Go to Solution.
s='7-Eleven 2364' while s[-1:].isdigit() or s[-1:]==" ": s=s[:-1]
s='7-Eleven 2364' while s[-1:].isdigit() or s[-1:]==" ": s=s[:-1]
Hello John,
There are probably 1000 good ways to do this. One way is to use regular expressions, but since RegEx has complicated syntax (in my option), I would do something like this:s='7-Eleven 2364' while s[-1:].isdigit() or s[-1:]==" ": s=s[:-1]
Note it also gets rid of any tailing spaces.
Good luck!
re.sub(" \d+$", "", s)
You know, it's so rare for me to encounter a condition where a while-loop is warranted that I didn't even think to use it. Suppose that's a testament to my infantile coding abilities, lol. Thanks Joshua!
itemName = "Starbucks 1234_" while str(itemName[-1:]).isdigit() or str(itemName[-1:]) == "_" or str(itemName[-1:]) == " ": itemDisplayName = itemName[:-1]
itemName = "Starbucks 1234_" while str(itemName[-1:]).isdigit() or str(itemName[-1:]) == "_" or str(itemName[-1:]) == " ": itemName = itemName[:-1]
itemName = "Starbucks 1234_" itemDisplayName = itemName while str(itemDisplayName[-1:]).isdigit() or str(itemDisplayName[-1:]) == "_" or str(itemDisplayName[-1:]) == " ": itemDisplayName = itemDisplayName[:-1]
i = 1 while not itemName[-i].isalpha(): itemDisplayName = itemName[:-i] i+=1
In that case the loop is checking the last char in itemName repeatedly since you're only checking the last value of itemName while not removing it. Therefore the loops gets "_" over and over, however if you update itemName inside the loop it removes the last character one by one.
if you wanted to use that solution you would have to do something like:i = 1 while not itemName[-i].isalpha(): itemDisplayName = itemName[:-i] i+=1
Then itemDisplayName should work as expected.