fb pixel

Introduction to Python

  • Profound Academy

    • Status
      • 1
        Input and Output
      • 2
        Variables and Integer Arithmetic
      • 3
        Conditional Operators
      • 4
        Advanced Conditions - Nesting
      • 5
        Types and Variables
      • 6
        Strings
      • 7
        Lists
      • 8
        For Loops
      • 9
        While Loops
      • 10
        continue, break, while...else
      • 11
        String and List methods
      • 12
        Nested Loops
      • 13
        List Comprehension
      • 14
        Tuples and Sets
      • 15
        dict
      • 16
        Functions
      • 17
        Functions 2
      • 18
        Lambda and higher-order functions
      • 19
        Files

  • string checking methods

    When dealing with strings, it’s also important to check if some conditions hold. Below are several popular methods that are used to check for some conditions in a string:
    Method
    Description
    Examples
    Results
    isupper()
    Check if all the letters are uppercase
    'Anna'.isupper() 'ANNA'.isupper() 'AnnA'.isupper()
    False True False
    islower()
    Check if all the letters are lowercase
    'Anna'.islower() 'anna'.islower() 'AnnA'.islower()
    False True False
    istitle()
    Check if all the words start with uppercase and are followed by lowercase letters
    'Anna'.istitle() 'anna'.istitle() 'AnnA'.istitle() '19'.istitle()
    True False False False
    isdigit()
    Checks if all the characters are numbers
    'Anna'.isdigit() '1997'.isdigit() 'An97'.isdigit() '19'.isdigit()
    False True False True
    isalpha()
    Checks if all the characters are alphabetical
    'Anna'.isalpha() '1997'.isalpha() 'An97'.isalpha() 'Hi Anna'.isalpha()
    True False False False
    isalnum()
    Checks if all the characters are either alphabetical or numeric
    'Anna'.isalnum() '1997'.isalnum() 'An97'.isalnum() 'Hi Anna'.isalnum()
    True True True False
    startswith('xxx')
    Check if the string starts with 'xxx'
    'Anna'.startswith('A') 'Anna'.startswith('a') 'Anna'.startswith('') 'Anna'.startswith('An')
    True False True True
    endswith('xxx')
    Check if the string ends with 'xxx'
    'Anna'.endswith('A') 'Anna'.endswith('a') 'Anna'.endswith('') 'Anna'.endswith('nA')
    False True True False
    isspace()
    Checks if all the characters are whitespace characters (\t, \n, space, etc)
    'Anna'.isspace() ' '.isspace() ' \t \n'.isspace() ' \t hi'.isspace()
    False True True False
    Bear in mind that memorizing these methods is not necessary. Despite the names being very intuitive, a short googling will lead you to the method you actually need. This small table is just a fraction of the useful methods to demonstrate what is possible with a single line of Python code.
    Note that string does not have contains() method as there is already an in keyword present which checks if one string is contained in another.

    Challenge

    Given a string, count the number of uppercase, the number of lowercase, and the number of space letters.
    The input contains a single line of text which needs to be analyzed.
    The program should print a single line with 3 numbers - the number of uppercase letters, the number of lowercase letters, and the number of space letters.
    Input
    Output
    Hey, how are you doing today Anna?
    2 24 6
    Amazing job with the infrastructure setup Bob!
    2 37 6
     
    To check your solution you need to sign in
    Sign in to continue