Reputation: 15
I'm really a beginner with Python and I'm trying to modify codes that I have seen in lessons. I have tried the find all uppercase letters in string. But the problem is it only returns one uppercase letter in string even if there are more than one.
def finding_uppercase_iterative(string_input):
for i in range(len(string_input)):
if string_input[i].isupper:
return string_input[i]
return "No uppercases found"
How should I modify this code to give me all uppercase letters in given string. If someone can explain me with the logic behind I would be glad.
Thank you!
Edit 1: Thank to S3DEV I have mistyped the binary search algorithm.
Upvotes: 1
Views: 3191
Reputation: 2602
If you are looking for only small changes that make your code work, one way is to use a generator function, using the yield
keyword:
def finding_uppercase_iterative(string_input):
for i in range(len(string_input)):
if string_input[i].isupper():
yield string_input[i]
print(list(finding_uppercase_iterative('test THINGy')))
If you just print finding_uppercase_iterative('test THINGy')
, it shows a generator object, so you need to convert it to a list in order to view the results.
For more about generators, see here: https://wiki.python.org/moin/Generators
Upvotes: 3
Reputation: 1775
This is the fixed code written out with a lot of detail to each step. There are some other answers with more complicated/'pythonic' ways to do the same thing.
def finding_uppercase_iterative(string_input):
uppercase = []
for i in range(len(string_input)):
if string_input[i].isupper():
uppercase.append(string_input[i])
if(len(uppercase) > 0):
return "".join(uppercase)
else:
return "No uppercases found"
# Try the function
test_string = input("Enter a string to get the uppercase letters from: ")
uppercase_letters = finding_uppercase_iterative(test_string)
print(uppercase_letters)
Here's the explanation:
string_input
as a parameteruppercase
string_input
uppercase
listuppercase
list is more than 0if
] return the list characters all joined together with nothing as the separator (""
)else
] otherwise, return "No uppercases found"test_string
and store it in a variableuppercase_letters
from test_string
print
the uppercase_letters
to the userThere are shorter (and more complex) ways to do this, but this is just a way that is easier for beginners to understand.
Upvotes: 2
Reputation: 3396
The return
statement in a function will stop the function from executing. When it finds an uppercase letter, it will see the return statement and stop.
One way to do this is to append letters to list and return them at the end:
def finding_uppercase_iterative(string_input):
letters = []
for i in range(len(string_input)):
if string_input[i].isupper():
letters.append(string_input[i])
if letters:
return letters
return "No uppercases found"
Upvotes: 2
Reputation: 451
Something simple like this would work:
s = "My Word"
s = ''.join(ch for ch in s if ch.isupper())
return(s)
Inverse idea behind other StackOverflow question: Removing capital letters from a python string
Upvotes: 1