-1

I need to check if the first letter of a list is upper. For that I wrote this simple code, where obviasly my word "Try" starts with capital "T":

h=[]
h.append("Try")
a = str(h[0])
print(a)
print(a.isupper())

BUT when I print a.isupper I get always False. Should I convert the variable in something, or it should be str object? How can I solve this problem

  • Currently you check if whole string is upper but you must only take the first letter. – Michael Butscher Apr 14 '20 at 11:26
  • I guess you read somewhere that you had to use `a[0].isupper()` but then you confused it with `h[0]` which takes the first string from the list, not the first character from the string? – mkrieger1 Apr 14 '20 at 11:33

3 Answers3

2

You are checking the whole string to be capital, that's why it's False.

When you do print(a[0].isupper()), it checks if the whole string(Try in your case) is capital. Hence, it returns False.

You want to check just the first letter of the string, so do this instead:

In [615]: print(a[0].isupper())                                                                                                                                                                             
True

Where a[0] gives you T.

Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
2

You dont need the list. Just do:

a= "Try"
print(a[0].isupper())
Gabio
  • 9,126
  • 3
  • 12
  • 32
2

You are using h[0] which gives "Try", and when you check for a.isupper() It has both lower case and upper case , please check for a[0] , then you get true if the first letter is capital

Santhosh Reddy
  • 123
  • 1
  • 6