The best way to change the letters "A" and "B" in places
📁 function, Advanced Python Topics, example
There is a function that takes an argument in the form of a string consisting of "a", "A", "b" and "B".
Each "b" should be replaced by "a", each "a" by "B", and also with the lower case.
>>> swap('aabbbaa')
'BBAAAAABB'
Wrote such a weak code, but it works:
def swap(text): new_text = [] for c in text: if c == 'a': new_text.append('B') elif c == 'b': new_text.append('A') elif c == 'A': new_text.append('b') elif c == 'B': new_text.append('a') return ''.join(new_text)
No answers yet.
Log in to leave an answer