1
def WhichAreCaps(Word):
    for w in Word:
        if w.isupper() == True:
          print (w)

WhichAreCaps("IJkdLenMd")

Result:

I
J
L
M

So I'm trying to build a code that finds Capitalized letters in a string and print out the letters. The problem is that I want the result to come out as one line of string, not four. Is there a way to do so? (I'm just a beginner;;)

Mazdak
  • 100,514
  • 17
  • 155
  • 179
ChikkinMan
  • 23
  • 4
  • 2
    You `''.join(..)` the characters together... – Willem Van Onsem Mar 17 '17 at 12:20
  • 2
    See the [`print()`](https://docs.python.org/3/library/functions.html#print) documentation (especially the `end` argument). – glibdud Mar 17 '17 at 12:21
  • @WillemVanOnsem I've tries doing print (" ".join(w)), still the same probelm – ChikkinMan Mar 17 '17 at 12:25
  • BTW, your function would be more useful if it returns the results as a string instead of printing them. That way, the caller could print the results themself, or do some other processing on the results before printing them. – PM 2Ring Mar 17 '17 at 12:28
  • @ChikkinMan Change approach: `"".join(c for c in word if c.isupper())`. – farsil Mar 17 '17 at 12:29
  • You _could_ do this: `print(''.join(filter(str.isupper, word)))`, but you probably shouldn't. :) – PM 2Ring Mar 17 '17 at 12:36
  • @PM2Ring How would it look like if it was returned? I have a hard time converting print() to return() – ChikkinMan Mar 17 '17 at 12:37
  • Well, you put a `return` statement at the end of the function, once you've finished the processing and have collected the results into a list or string. Eg, you can change the last line of [Akshay Apte's answer](http://stackoverflow.com/a/42857468/4014959) from `print(ans)` to `return ans`. And then you could call it like `print(WhichAreCaps("IJkdLenMd"))`. Or doing it on separate lines `s = WhichAreCaps("IJkdLenMd")` `print(s)`. That way you can do other stuff with `s`. – PM 2Ring Mar 17 '17 at 12:42
  • If one of the answers below fixes your issue, you should accept it (click the check mark next to the appropriate answer). That does two things. It lets everyone know your issue has been resolved to your satisfaction, and it gives the person that helps you credit for the assist. See [here](http://meta.stackexchange.com/a/5235) for a full explanation. – PM 2Ring Mar 17 '17 at 12:43
  • @PM2Ring Oh thank you, I didn't know the check mark thingy! – ChikkinMan Mar 17 '17 at 12:55

2 Answers2

2

print function accepts an end keyword argument to specifies the end of the string after printing. By default it's set to new line (\n), you can simply use a space. Also note that checking the truths value of any expression with == True is wrong since True interprets as 1 and every thing that returns 1 will be interpreted as True. You can simply use if w.isupper():

def WhichAreCaps(Word):
    for w in Word:
        if w.isupper():
          print(w, end=' ')

Another way is yielding the vowels in your function and make it a generator:

def WhichAreCaps(Word):
    for w in Word:
        if w.isupper():
            yield w

Then you can join the result in any way you like and print it:

print(','.join(WhichAreCaps(Word))) 

After all, as a more pythonic approach for this ask you can simply use a list comprehension within str.join:

print(' '.join([w for w in Word if w.isupper()]))
Mazdak
  • 100,514
  • 17
  • 155
  • 179
1

You can Just append the letters to an empty string and then return it.

def WhichAreCaps(Word):
    ans=''
    for w in Word:
        if w.isupper() == True:
          ans+=w
    print(ans)
Akshay Apte
  • 1,411
  • 7
  • 22
  • 2
    I do not understand the dv.. Can the downvoter explain? – Ma0 Mar 17 '17 at 12:29
  • 1
    @Ev.Kounis Maybe it's because it's a code-only answer. Or maybe the down-voter voted on the early version of the answer which used the Python 2 `print` statement and didn't notice that Akshay fixed that. – PM 2Ring Mar 17 '17 at 12:32
  • Thanks guys. I'm relatively new to SO. – Akshay Apte Mar 17 '17 at 12:36
  • I downvoted because 1) it's a code only answer, 2) `== True` is redundant, and mostly 3) I don't condone string concatenation because it's underperformant. – Jean-François Fabre Mar 17 '17 at 15:00