#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2

    Python simple doubt.


    Suppose, we are given a string, say,
    s = 'striNG'
    Now, the condition is, if any of the element in the string is in uppercase, then the string is invalid else it's not.
    How shall we write a code for that?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2
    OK, OK, OK, OK; I got it! Ha, ironic.
    Thanks, anyway.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2
    And kindly requesting, someone delete this thread.
  6. #4
  7. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    611
    Rep Power
    65
    The most readable way would be ...
    Code:
    def has_cap(s):
        '''
        return true if string s has an uppercase character
        '''
        for c in s:
            if c.isupper():
                return True
        return False
    
    # testing ...
    
    s = 'strinG'
    result = has_cap(s)
    print(result)  # True
    
    s = 'string'
    result = has_cap(s)
    print(result)  # False
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    I always think these types of questions serve as a good chance to show off the any function.

    python Code:
    def has_cap(s):
        return any(c.isupper() for c in s)

    -Mek

IMN logo majestic logo threadwatch logo seochat tools logo