String comparisons

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • psychofish25
    New Member
    • Jul 2007
    • 60

    String comparisons

    I am making a program that requires me to test whether a certain character in a string is capital. I was wondering if there was anyway I could do this like:

    string is A-Z

    That would determine whether the string is any letter from A to Z
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by psychofish25
    I am making a program that requires me to test whether a certain character in a string is capital. I was wondering if there was anyway I could do this like:

    string is A-Z

    That would determine whether the string is any letter from A to Z
    [code=python]
    >>> import string
    >>> 'g' in string.lowercas e
    True
    >>> 'D' in string.lowercas e
    False
    [/code]

    Comment

    • psychofish25
      New Member
      • Jul 2007
      • 60

      #3
      Originally posted by ilikepython
      [code=python]
      >>> import string
      >>> 'g' in string.lowercas e
      True
      >>> 'D' in string.lowercas e
      False
      [/code]
      Okay thanks, but is there a way to determine whether a letter is a vowel or not?
      kinda like string=['a','e','i','o' ,'u']

      Comment

      • ilikepython
        Recognized Expert Contributor
        • Feb 2007
        • 844

        #4
        Originally posted by psychofish25
        Okay thanks, but is there a way to determine whether a letter is a vowel or not?
        kinda like string=['a','e','i','o' ,'u']
        Well...
        [code=python]
        >>> vowels = "aeiou"
        >>> 'e' in vowels
        True
        >>> 'f' in vowels
        False
        [/code]

        Comment

        • psychofish25
          New Member
          • Jul 2007
          • 60

          #5
          Originally posted by ilikepython
          Well...
          [code=python]
          >>> vowels = "aeiou"
          >>> 'e' in vowels
          True
          >>> 'f' in vowels
          False
          [/code]
          Okay that makes sense now. But is there anyway to make it like a to z without typing each letter?

          Comment

          • bartonc
            Recognized Expert Expert
            • Sep 2006
            • 6478

            #6
            Originally posted by psychofish25
            Okay that makes sense now. But is there anyway to make it like a to z without typing each letter?
            [CODE=python]
            >>> from string import letters
            >>> "B" in letters
            True
            >>> [/CODE]

            Comment

            • psychofish25
              New Member
              • Jul 2007
              • 60

              #7
              Originally posted by bartonc
              [CODE=python]
              >>> from string import letters
              >>> "B" in letters
              True
              >>> [/CODE]
              Okay excellent, thanks.

              Comment

              • ghostdog74
                Recognized Expert Contributor
                • Apr 2006
                • 511

                #8
                Code:
                >>> "B".isupper()
                True
                >>> "b".isupper()
                False
                >>>

                Comment

                • bartonc
                  Recognized Expert Expert
                  • Sep 2006
                  • 6478

                  #9
                  Originally posted by ghostdog74
                  Code:
                  >>> "B".isupper()
                  True
                  >>> "b".isupper()
                  False
                  >>>
                  Thanks, GD. Strings do have quite a few handy methods hung on them.
                  I also meant to point out that the string module is useful for the constants defined there, but the functions which are now string methods should not be used.

                  Comment

                  Working...