1

I have a text as follows.

mytext = "This is AVGs_ABB and NMN_ABB and most importantly GFD_ABB This is so important that you have to CLEAN the lab everyday"

I want to convert it to lowercase, except the words that has _ABB in it.

So, my output should look as follows.

 mytext = "this is AVGs_ABB and NMN_ABB and most importantly GFD_ABB this is so important that you have to clean the lab everyday"

My current code is as follows.

splits = mytext.split()
newtext = []
for item in splits:
 if not '_ABB' in item:
 item = item.lower()
 newtext.append(item)
 else:
 newtext.append(item)

However, I want to know if there is any easy way of doing this, possibly in one line?

asked Dec 20, 2017 at 8:53
3
  • 1
    Sure, one could invent one-liners for this, but what exactly is wrong with your version? It is significantly more readable. Commented Dec 20, 2017 at 8:56
  • 1
    @Kendas I wouldn't say so; nobody would expect to see so much code for that task. Creates too many variables too Commented Dec 20, 2017 at 9:05
  • mytext.replace("_ABB","_abb") Commented Dec 20, 2017 at 9:08

3 Answers 3

11

You can use a one liner splitting the string into words, check the words with str.endswith() and then join the words back together:

' '.join(w if w.endswith('_ABB') else w.lower() for w in mytext.split())
# 'this is AVGs_ABB and NMN_ABB and most importantly GFD_ABB this is so important that you have to clean the lab everyday'

Of course use the in operator rather than str.endswith() if '_ABB' can actually occur anywhere in the word and not just at the end.

answered Dec 20, 2017 at 8:57

2 Comments

May use ... if '_ABB' in w, since the question asks for "words that has _ABB in it". It doesn't have to be at the end
Still I'd write it as if '_ABB' in w, just so that it applies to what OP asked for words that has _ABB in it
3

Extended regex approach:

import re
mytext = "This is AVGs_ABB and NMN_ABB and most importantly GFD_ABB This is so important that you have to CLEAN the lab everyday"
result = re.sub(r'\b((?!_ABB)\S)+\b', lambda m: m.group().lower(), mytext)
print(result)

The output:

this is AVGs_ABB and NMN_ABB and most importantly GFD_ABB this is so important that you have to clean the lab everyday

Details:

  • \b - word boundary
  • (?!_ABB) - lookahead negative assertion, ensures that the given pattern will not match
  • \S - non-whitespace character
  • \b((?!_ABB)\S)+\b - the whole pattern matches a word NOT containing substring _ABB
answered Dec 20, 2017 at 9:27

Comments

0

Here is another possible(not elegant) one-liner:

mytext = "This is AVGs_ABB and NMN_ABB and most importantly GFD_ABB This is so important that you have to CLEAN the lab everyday"
print(' '.join(map(lambda x : x if '_ABB' in x else x.lower(), mytext.split())))

Which Outputs:

this is AVGs_ABB and NMN_ABB and most importantly GFD_ABB this is so important that you have to clean the lab everyday

Note: This assumes that your text will only seperate the words by spaces, so split() suffices here. If your text includes punctuation such as",!.", you will need to use regex instead to split up the words.

answered Dec 20, 2017 at 9:27

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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