It is necessary to format a string (preferably using the format method). A string, for example, is:

string = "words{hello||hello1}words" 

And before this line, the parameter s=True or s=False , and if s=True , then the output should be this:

 string = 'wordshellowords' 

, and if s=False , then:

 string = 'wordshello1words' 

How to do it?

5 answers 5

Algorithm step by step:

 string = "words{hello||hello1}words" start = string.index('{') + 1 end = string.index('}') replace_str = string[start: end] print(replace_str) # hello||hello1 var_replace = replace_str.split('||') print(var_replace) # ['hello', 'hello1'] string = string.replace(replace_str, '') print(string) # words{}words s = True if s: new_string = string.format(var_replace[0]) print(new_string) # wordshellowords else: new_string = string.format(var_replace[1]) print(new_string) # wordshello1words 

In the form of a function:

 def foo(string: str, s=True) -> str: start, end = string.index('{') + 1, string.index('}') replace_str = string[start: end] var_replace = replace_str.split('||') string = string.replace(replace_str, '') # Можно схитрить, т.к. True -- 1, False -- 0 # return string.format(var_replace[not s]) return string.format(var_replace[0] if s else var_replace[1]) string = "words{hello||hello1}words" print(foo(string, True)) # wordshellowords print(foo(string, False)) # wordshello1words 

Option through the regular season:

 import re def foo(string: str, s=True) -> str: def _on_match(match): var_replace = match.group()[1:-1].split('||') return var_replace[0] if s else var_replace[1] return re.sub('{.+?}', _on_match, string) string = "words{hello||hello1}words" print(foo(string, True)) # wordshellowords print(foo(string, False)) # wordshello1words 
  • Is it possible to make several {hello || hello1} ??? PS It turns out with only one ((( - Ivan
  • @Ivan, it is possible, but this is a task change, and a code :) I can prompt a quick solution for the regular program: change it to '{.+?}' , Then the processing will be lazy (not greedy) and for string = "words{hello||hello1}words_{hello||hello1}_" get: wordshello1words_hello1_ - gil9red

To select when replacing the first alternative in the template, when first=True :

 import re format_spec = "words{AAA||BBB}words" first = True print(re.sub( "{([^}]+)}", lambda match: match.group(1).split("||")[not first], format_spec, )) 

Result:

 wordsAAAwords 

It uses True == 1 and False == 0 , so Boolean variables can be used as indices. For example, [1, 2][True] returns 2 , and [1, 2][False] returns 1 .

     string = "wordshello{}words" s = True print(string.format(int(s) or '')) # 'wordshello1words' s = False print(string.format(int(s) or '')) # 'wordshellowords' 
       string = 'words{}words' subs = ['hello1', 'hello'] s = True print(string.format(subs[s])) # wordshellowords 

      The trick is that when converting to integer, True becomes 1 , and False - 0 , and thus select an item by index.

      • Moreover, False == 0 , True == 1 - insolor
       import re string = 'words{hello||hello1}words' parts = re.split(r'[{|}]', string) if s == True: print(parts[0] + parts[1] + parts[-1]) else: print(parts[0] + parts[-2] + parts[-1])