Home > Software engineering >  How can I put multiple statements in one line in python?without using ; and exec
How can I put multiple statements in one line in python?without using ; and exec

Time:06-05

I want to write this code in one line without using ; and exec

input_string = str(input())
array = []
for i in range(len(input_string)):
    if (ord(input_string[i]) - 97) % 2 == 0:
       array.append(input_string[i])
    else:
       array.append(input_string[i].upper())
array.sort(reverse=True)
answer = ' '.join(array)
print(answer)

and couldn't do that so i came up with 4 line like this

input_string = str(input())
array = []
for i in range(len(input_string)): array.append(input_string[i]) if (ord(input_string[i]) -97) % 2 == 0 else array.append(input_string[i].upper())
print(' '.join(sorted(array,reverse=True)))

please help me to write this code in one line. thank you all in advance.

CodePudding user response:

Done.

print(' '.join(sorted([letter if (ord(letter) -97) % 2 == 0 else letter.upper() for letter in str(input())],reverse=True)))
  • Related