0

How to return multiple values each in a new line!

hello everyone, I need your advise on how to return multiple values from a function and print the output one by one in new line.. def sortString(mystring): upper = "" lower = "" punch = "" spacecount = 0 try: for i in mystring: if ord(i) in range(65, 91): upper += i elif ord(i) in range(97,123): lower += i elif ord(i) in range(33, 65): punch += i elif ord(i) == 32: spacecount += 1 return upper \n lower \n punch \n spacecount except: return "Not a string!" print(sortString("ZOMG Hello, CS1301!!")) My code should print as follows; #ZOMGHCS #ello #,1301!! #2 Any thoughts?

9th Sep 2020, 5:56 PM
NG_
NG_ - avatar
1 Answer
+ 7
[edited] You can create an f-string that will be returned and finaly printed. Just replace the complete line of your return statement by this line: return f'{upper}\n{lower}\n{punch}\n{spacecount}'
9th Sep 2020, 6:07 PM
Lothar
Lothar - avatar