क्या यह आपकी स्थिति के लिए काम करेगा?
>>> s = '12abcd405'
>>> result = ''.join([i for i in s if not i.isdigit()])
>>> result
'abcd'
यह एक सूची समझ का उपयोग करता है, और यहां जो कुछ हो रहा है वह इस संरचना के समान है:
no_digits = []
# Iterate through the string, adding non-numbers to the no_digits list
for i in s:
if not i.isdigit():
no_digits.append(i)
# Now join all elements of the list with '',
# which puts all of the characters together.
result = ''.join(no_digits)
जैसा कि @AshwiniChaudhary और @KirkStrauser बताते हैं, आपको वास्तव में वन-लाइनर में कोष्ठक का उपयोग करने की आवश्यकता नहीं है, जिससे कोष्ठक के अंदर का टुकड़ा एक जनरेटर अभिव्यक्ति (एक सूची समझ से अधिक कुशल) हो जाता है। यहां तक कि अगर यह आपके असाइनमेंट के लिए आवश्यकताओं को फिट नहीं करता है, तो यह कुछ ऐसा है जिसे आपको अंततः पढ़ना चाहिए :)
>>> s = '12abcd405'
>>> result = ''.join(i for i in s if not i.isdigit())
>>> result
'abcd'
re:result = re.sub(r'[0-9]+', '', s)