-1

trying to write a function that returns ONLY the Capital letters within my string. if I use print inside the function it reports all of them but would result in a double print given that print has to be used when the function is called.

How do I use "return" instead of print ? at the moment it returns the first letter only.

def get_capitals(string):
    for i in string:
        try:
            if ord(i) in range(65, 91):
                print (i, end="")  
        except:
              pass
print(get_capitals("CS1301"))
print(get_capitals("Georgia Institute of Technology"))
David
  • 208,112
  • 36
  • 198
  • 279
Mike
  • 13
  • 3
  • Where in the function are you returning anything? *Presumably* you simply replaced `print (i, end="")` with a `return` statement, which would expectedly return on the first iteration of the loop. It sounds like you want to build up a string value in the loop and *return* that string value *after* the loop. – David Mar 10 '23 at 12:18
  • Hint: you can only `return` _once_. Is there a way to collect those letters up and then return them when you're done? – ChrisGPT was on strike Mar 10 '23 at 12:18
  • Does this answer your question? [What is the purpose of the return statement? How is it different from printing?](https://stackoverflow.com/questions/7129285/what-is-the-purpose-of-the-return-statement-how-is-it-different-from-printing) – Jorge Luis Mar 10 '23 at 12:20
  • Instead of print, create a new string variable and append it. after the end of for loop, you can return the variable. – Prudhviraj Mar 10 '23 at 12:21

6 Answers6

1

You could transform your function into a generator by using yield

def get_capitals(string):
    for i in string:
        if ord(i) in range(65, 91):
            yield i


list(get_capitals('AbCdE')) # -> ['A','C','E']
''.join(get_capitals('AbCdE')) # -> "ACE" 
jvx8ss
  • 529
  • 2
  • 12
0

You have to create a string instead of printing each captital directly and use the end=''. That is like a stream, while you want to store your data and return it at the end of the function.

def get_capitals(string):
    return_string = ''
    for i in string:
        try:
            if ord(i) in range(65, 91):
                return_string += i
        except:
              pass
    return return_string
3dSpatialUser
  • 2,034
  • 1
  • 9
  • 18
0

You can create an empty string variable and append the capital letters to it inside the loop. Then, at the end of the loop, you can return the final string that contains all the capital letters.

def get_capitals(string):
    capital_letters = ""
    for i in string:
        try:
            if ord(i) in range(65, 91):
                capital_letters += i  
        except:
              pass
    return capital_letters
0

Instead of print, create a new string variable and append it.

Sample example

def get_capitals(string):
  capital_letters = ""
  for i in string:
    try:
      if ord(i) in range(65, 91):
        capital_letters += i  
    except:
      pass
  return capital_letters

print(get_capitals("CS1301"))
print(get_capitals("Georgia Institute of Technology"))

enter image description here

Prudhviraj
  • 441
  • 3
  • 12
0

Create a empty list and append the Capital letters, then use join to convert that as string return that

def get_capitals(string):
    lis = []
    for i in string:
        try:
            if ord(i) in range(65, 91):
                lis.append(i)
        except:
              pass
     valu = ''.join(lis)
     return valu
print(get_capitals("CS1301"))
print(get_capitals("Georgia Institute of Technology"))
pcbzmani
  • 27
  • 6
0

Return and print are entirely different things.

Return doesn't "print" anything. You cannot replace "print" statements with "return".

When your function reaches a return statement, it will end the function. That is what "return" means. It returns back to the calling scope.

Do you understand the output of your program? It prints

CSNone
GITNone

In your current code, you could say:

get_capitals("CS1301")
print()
get_capitals("Georgia Institute of Technology")

This will result in

CS
GIT

The single print() statement gives you a convenient newline. It's the function call that prints out csand GIT, not your surrounding print statement. Your print statements only prints the return-value of the function, and because your function doesn't return anything, it adds a None to the output generated by the function call.

If you want to use a return in the function and do not want to use print() inside the function, then you have to compose the return value of the function at the places where you use print() at the moment. You compose the return string at these places and return the composed value with a return statement.

Then it makes sense to use ``print(get_capitals("CS1301"))``` the return-value of the function, which would then be defined, with print().

Play around with it a bit, then you will understand what happens.

maddes8cht
  • 569
  • 3
  • 16