A tuple is given that contains several lines, for example: ('right', rightleft ',' left ',' stop '). You need to get one line in the end and that all the 'right' are replaced by 'left'. Like this: 'left, leftleft, left, stop'

def Aс (*phrases) : k = str(phrases) t = k.replace('right', 'left') # вот на этом я застрял print(t) 
  • one
    remove the second line and replace the third line with t=', '.join(*phrases).replace('right', 'left') . Isn't it? - Edward Izmalkov
  • TypeError: sequence item 0: expected str instance, tuple found - D.Ryksd
  • bring your code completely, which is passed to the function - Edward Izmalkov
  • All clear. If you enter into a function a tuple in double brackets (()), then you need to remove - * with phrase. Then everything works. - D.Ryksd

2 answers 2

The tuple and strings are immutable objects in Python. The only way to "change" an immutable object is to create a new one.

To replace the 'right' substring with 'left' :

 phrases = tuple(s.replace('right', 'left') for s in phrases) 

To replace only whole lines:

 phrases = tuple('left' if s == 'right' else s for s in phrases) 

To print lines separated by commas:

 print(*phrases, sep=',') 

If you need a string:

 text = ','.join(phrases) 
     def A(phrases) : return(','.join(phrases).replace('right', 'left'))