Skip to content
Advertisement

Execute f-string in function

I have a

string = 'long company name with technologies in it'

and want to replace all tokens starting with

search_string ='techno'

with a new token

replace_string = 'tech'.

I wrote a function:

def group_tokens(company_name, string_search, string_replace):   
try:
    x = company_name.split(" ") 
    print(f"x = [re.sub('^{string_search}.*', '{string_replace}', i) for i in x]")
    exec(f"x = [re.sub('^{string_search}.*', '{string_replace}', i) for i in x]")
    x = " ".join(x)
    x = " ".join(re.split("s+", x, flags=re.UNICODE))
    return(x)
except:
        return np.nan

If I execute the lines separately it works. But the function itself doesn’t work.

group_tokens('long company name with technologies in it', 'techno', 'tech') = 'long company name with technologies in it'

I’d expect

group_tokens('long company name with technologies in it', 'techno', 'tech') = 'long company name with tech in it'

How can I “exec” f-string in a function?

Advertisement

Answer

You are overcomplicating this. Simply reassign x:

def group_tokens(company_name, string_search, string_replace):   
  try:
    x = company_name.split(" ") 
    x = [re.sub(f'^{string_search}.*', string_replace, i) for i in x])
    x = " ".join(x)
    x = " ".join(re.split("s+", x, flags=re.UNICODE))
    return x
  except:
    return np.nan

But it’s probably easier to rewrite the function similar to the following:

def group_tokens(company_name, string_search, string_replace):   
  return re.sub(f'b{string_search}S*s*', f'{string_replace} ', company_name, flags=re.UNICODE);
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement