Bugra Yildirim
Bugra Yildirim

Reputation: 15

Python - Finding all uppercase letters in string

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

Answers (4)

Oli
Oli

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

Lakshya Raj
Lakshya Raj

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:

  1. create a function that takes string_input as a parameter
  2. create an empty list called uppercase
  3. loop through every character in string_input
  4. [in the loop] if it is an uppercase letter, add it to the uppercase list
  5. [out of the loop] if the length of the uppercase list is more than 0
  6. [in the if] return the list characters all joined together with nothing as the separator ("")
  7. [in the else] otherwise, return "No uppercases found"
  8. [out of the function] get a test_string and store it in a variable
  9. get the uppercase_letters from test_string
  10. print the uppercase_letters to the user

There are shorter (and more complex) ways to do this, but this is just a way that is easier for beginners to understand.

Upvotes: 2

Mohammad
Mohammad

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

Anton van der Wel
Anton van der Wel

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

Related Questions