How to swap a string by taking characters in uppercase and coverting in lowr one or vice versa | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to swap a string by taking characters in uppercase and coverting in lowr one or vice versa

Phython

30th Jan 2019, 4:24 PM
Whack Hack
Whack Hack - avatar
5 Answers
+ 3
The easiest way would be to use str.swapcase(). A manual solution could look something like this: s = 'Hello World' print(''.join([c.lower() if c == c.upper() else c.upper() for c in s])) # hELLO wORLD
30th Jan 2019, 6:08 PM
Anna
Anna - avatar
0
Robert Carsten but can't we take two string as uppercase and lowercase and swap them by using swap function
30th Jan 2019, 4:56 PM
Whack Hack
Whack Hack - avatar
0
Robert Carsten sorts of but I understand thanks
30th Jan 2019, 5:07 PM
Whack Hack
Whack Hack - avatar
- 1
If you mean just making the letters uppercase or lowercase you could use the appropriate string method "String".upper() "String".lower()
30th Jan 2019, 4:54 PM
Robert Carsten
Robert Carsten - avatar
- 1
Perhaps I'm misunderstanding so tell me if this is the output you are looking for a = "STRING" b = "string" a,b = b,a print(a,b) >> string STRING
30th Jan 2019, 5:04 PM
Robert Carsten
Robert Carsten - avatar