-2

i have a list :

cpt=0
list=["dermato","bioderma", "gatoderma"]
for l in list:
 if re.findall(".*derma.*",l):
     cpt=cpt+1

So, instead of putting directly 'derma' as regex , i want :

a= "derma"   #initialize
list=["dermato","bioderma", "gatoderma"]
for l in list:
if re.findall(".*a.*",l):
     cpt=cpt+1
ruohola
  • 21,987
  • 6
  • 62
  • 97
  • 1
    Use string concatenation, `str.format`, f-strings, etc. – Wiktor Stribiżew Oct 30 '20 at 21:35
  • 1
    The regex still has to be compiled, doesn't matter where you put the string. You would like to save time, but it doesn't matter how it's done takes the same time. –  Oct 30 '20 at 21:48

1 Answers1

-2

You can use an f-string:

a=  "derma"
list = ["dermato","bioderma", "gatoderma"]
for l in list:
if re.findall(f".*{a}.*", l):
     cpt += 1
ruohola
  • 21,987
  • 6
  • 62
  • 97