choose two characters a and b in the string. swap a with b if and only if the sum of ascii values a and b are odd
string = 'I am a boy'
a = string[0]
b = string[1]
Total_sum = 0
for i in [a,b]:
Total_sum = sum(map(ord, i))
if Total_sum % 2 != 0:
string1 = string.replace(string[0], string[2])
print(string1)
Comments
Leave a comment