-2

This sounds simple but how can i display the capital letters of a string like:

string = "Hey There"

Then displays

'HT'

Josh G
  • 1
  • 1

1 Answers1

2

Try this

''.join([word[0] for word in s.split() if word[0].isupper()]) 

s being the string you want to find the capital letters, you split the string into words and choose those which have a upper case first letter.

In case you want all the capital letters off the string

''.join(c for c in s if c.isupper()) 

In this we check if every character c is upper case and join all such characters.

Matthias
  • 12,873
  • 6
  • 42
  • 48
Atul Shanbhag
  • 636
  • 5
  • 13
  • 1
    Explain your solution. Just giving one liner without explaining is no good except just giving the output. Without explanation, it's hard to make it transferable to other similar problems – Sheldore Sep 23 '18 at 12:21