1

What is the best way to convert the following string to the expected string in Python.

  1. "PowerEdgeSystem" => Power Edge System
  2. "VMwareSystem" => VMware System
  3. VMwareSystemEQUIPMENT => VMware System EQUIPMENT

Tried:

s = 'PowerEdgeSystem'
s = ''.join([' ' + c if i != 0 and c.isupper() else c for i, c in enumerate(s)])

First string is fine, but not as good for second string. Still we can check the case of 'both' side of the particular character and add space based on that as below,

s = ''.join([' ' + c if i != 0 and c.isupper() and not c[i+1].isupper() else c for i, c in enumerate(s)])

But the code will become more messy. I'm expecting some thing more clear way (if anything available). Thank you,

Jai K
  • 375
  • 1
  • 4
  • 12
  • Your solution works? – wwii Jul 10 '20 at 20:39
  • @wwii "But the code will become more messy" OP is looking for cleaner / better solution. which I think is reasonable. – Tadhg McDonald-Jensen Jul 10 '20 at 20:40
  • 1
    Other than the learning experience for better way, if you have a solution that doesn't seem intuitive generally I'd recommend just putting it in a function and well document it, so calls to the function are clean and the documentation of the function is clean, then the body of the function can be messy as you need it to be. – Tadhg McDonald-Jensen Jul 10 '20 at 20:42
  • `i` is undefined? – Ruzihm Jul 10 '20 at 21:12
  • second code now results in index out of range because `c` is of length 1, and `i+1` > 0 - Do you mean `s = ''.join([' ' + c if i != 0 and c.isupper() and i+1 – Ruzihm Jul 10 '20 at 21:17
  • 1
    @Ruzihm Corrected, Pl add `i, ` after `for` keyword, For second code we have to add Exception handler also, so that asked for an efficient method. Thanks. – Jai K Jul 10 '20 at 21:17

2 Answers2

3

I think you will want something like this, using Regular expression:

>>> re.sub(r"(\w)([A-Z])", r"\1 \2", "WordWordWord")
'Word Word Word'

Unfortunately this does not succeed on "VMwareSystem".

>>> re.sub(r"(\w)([A-Z])", r"\1 \2", "VMwareSystem")
'V Mware System'
Ruzihm
  • 19,749
  • 5
  • 36
  • 48
1

You can use a regular expression with substitution for this, greedily matching at least one non-uppercase character followed by at least one uppercase character, then inserting a space in between:

>>> matchPattern = r"([^A-Z]+)([A-Z]+)"
>>> replacePattern = r"\1 \2"
>>> re.sub(matchPattern, replacePattern, "VMwareSystemVMwareSystem")
'VMware System VMware System'
>>> re.sub(matchPattern, replacePattern, "PowerEdgeSystem")
'Power Edge System'
Ruzihm
  • 19,749
  • 5
  • 36
  • 48
  • 1
    Working for `VMwareSystemEQUIPMENT` too. – Jai K Jul 10 '20 at 21:27
  • @JaiK If this answer helped please consider [accepting it](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – Ruzihm Jul 10 '20 at 21:29