How to remove capitals letter in string where Everything else should be in the same place and order as before? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to remove capitals letter in string where Everything else should be in the same place and order as before?

a="A1B2C3D" should print '123" b="Georgia Institute of Technology" should print "eorgia nstitute of echnology"

10th Apr 2019, 2:20 PM
Bikal Shrestha
4 Answers
+ 4
Bikal, regex would be the preferred way, but as a beginner you can use this: a = 'A1B2C3D' b='Georgia Institute of Technology' new_str1 = '' new_str2 = '' for i in a: if i.isdigit(): new_str1 += i print(new_str1) for i in b: if i.islower() or i.isspace(): new_str2 += i print(new_str2) It’s not the best solution, but from the aspect of readibility for you it might be better in this case.
10th Apr 2019, 4:59 PM
Lothar
Lothar - avatar
+ 3
Bikal Shrestha this is the simplest way I know - and it is just 1 line of code Here are some other ways you can do it. Some are not so simple and some uses the method I used here. Check it and use a convenient one for you 😊 https://stackoverflow.com/questions/49281051/removing-capital-letters-from-a-JUMP_LINK__&&__python__&&__JUMP_LINK-string
10th Apr 2019, 4:06 PM
Seniru
Seniru - avatar
+ 2
You can use the sub method of the re module + regex Syntax: re.sub(pattern, repl, string, [count, flags] ) eg: import re s = "George Institute of Technology" #then substitute to s using the sub method s = re.sub(r"[A-Z]", "", s) print(s) #eorgia nstitute of echnology If you are not comfortable with regex reger to this tutorial https://www.sololearn.com/learn/9704/?ref=app
10th Apr 2019, 2:50 PM
Seniru
Seniru - avatar
+ 1
Is there any simple way?
10th Apr 2019, 4:01 PM
Bikal Shrestha