0

I need to strip leading and trailing whitespace, split string into list to remove excess whitespace between words and uppercase all values within one Python code block and calculation in Pro field calculator. I have the following code block started (below). I'm trying to identify the best way to account for all these stored variables before return of the final string value.

address =
fieldcalc(!address!)


def fieldcalc(address):
    str1 = address.upper()
    str2 = address.strip()
    str3 = address.split(" ")
    finalstr = ???
    return finalstr

Example bad string value: " String is fixed " Final String Value: "STRING IS FIXED"

2
  • It is always good to give some examples so people can validate whether they understand your question correctly.
    – bixb0012
    Feb 8 at 21:47
  • Why are you making all those extra variables and not using them?
    – Vince
    Feb 9 at 3:06

1 Answer 1

1

Not sure I understand the question completely, i.e., all edge cases depending on data, but the following should/might work in the expression box (no need for creating function):

" ".join(s.upper() for s in !address!.split(" ") if s)
2
  • I don't see .strip() used here? And I need to use .upper(), not .title(), correct?
    – Ms05
    Feb 8 at 21:56
  • You don't need str.strip because the combination of splitting the string based on spaces and using a generator expression with a condition if s drops all the empty spaces. And yes, you can change s.title() to s.upper(). I will update the code sample.
    – bixb0012
    Feb 8 at 22:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.