0

I am trying to mimic 'i am your'.title() string function with the following code to get the output as I Am Your. But it gives the same i am your as output without capitalizing the words

re.sub(r"\b'\w", lambda m: m.group().upper(), 'i am your')  

During the process of verfiying it, I came across this following snippet in the internet but not able to comprehend how the lambda gets the input from/as:

re.sub("[ab]", lambda x: x.group(0).upper(), "charly") # chArly

because,

>>> f = lambda x: x.group(0).upper()
>>> f("[ab]")
AttributeError: 'str' object has no attribute 'group'

>>> f(["a","b"])
AttributeError: 'list' object has no attribute 'group'

To understand it better, I tried to decode it with Python disassembler, but still it seems vague

>>> dis.dis('re.sub("[ab]", lambda x: x.group(0).upper(), "charly")')
          0 POP_JUMP_IF_FALSE 11877
          3 POP_JUMP_IF_TRUE 25205
          6 STORE_SLICE+0
          7 <34>
          8 DELETE_NAME     25185 (25185)
         11 FOR_ITER        11298 (to 11312)
         14 SLICE+2
         15 IMPORT_NAME     28001 (28001)
         18 DELETE_GLOBAL   24932 (24932)
         21 SLICE+2
         22 SETUP_LOOP       8250 (to 8275)
         25 SETUP_LOOP      26414 (to 26442)
         28 POP_JUMP_IF_FALSE 30063
         31 JUMP_IF_TRUE_OR_POP 12328
         34 STORE_SLICE+1
         35 <46>
         36 <117>           28784
         39 LOAD_NAME       10354 (10354)
         42 STORE_SLICE+1
         43 <44>
         44 SLICE+2
         45 <34>
         46 DUP_TOPX        24936
         49 POP_JUMP_IF_FALSE 31084
         52 <34>
         53 STORE_SLICE+1
1
  • I would use regular str functions instead of regex: ' '.join(s[:1].upper() + s[1:] for s in 'i am your'.split()). Commented Oct 2, 2020 at 14:26

1 Answer 1

1
  • You need to use upper() to get the first char in caps
  • There is a ' between word break and word escape literals, you need to remove that.
  • And you need to use m.group(0) or m[0] to get matched string.
>>> re.sub(r"\b\w",lambda m: m[0].upper(),"i am your")
'I Am Your'
Sign up to request clarification or add additional context in comments.

4 Comments

Ah, silly mistakes! :) Thank you Liju. I replaced lower() to upper() in my original posting and left others as it is. Between, I noticed m.group(0) works in py2.7 and m[0] works in py3
Liju or anyone, can please explain how the lambda works in the 2nd code snippet of my post? I have made the question bold.
Because you have given 0 in group() which you didn't do in first snippet.
Suppose if I write lambda function this way, f = lambda x: x.group(0).upper() then how to pass the argument pattern and the source string to match the pattern with n this case?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.