Avatar ·

The best way to swap letters "A" and "B"

📁 function, Advanced Python Topics, example

There is a function that takes an argument as a string consisting of "A", "a", "B" and "b".

Each "B" should be replaced with "A", each "A" with "B", and the same for lowercase.

Example:

>> swap('ААббББаа')'ББааААбб'

I wrote this weak code, but it works:

def swap(text):    new_text = []    for c in text:        if c == 'а':            new_text.append('б')        elif c == 'б':            new_text.append('а')        elif c == 'А':            new_text.append('Б')        el