compare 2 string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • python101
    New Member
    • Sep 2007
    • 90

    compare 2 string

    I have 2 strings

    name1 = 'john smith'

    name2 = 'John smith'

    What command should I use to check whether name2 is a capitalized version of name1? (the return value is True)
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by python101
    I have 2 strings

    name1 = 'john smith'

    name2 = 'John smith'

    What command should I use to check whether name2 is a capitalized version of name1? (the return value is True)
    [code=Python]>>> name1 = 'john smith'
    >>> name2 = 'John smith'
    >>> name1.lower() == name2.lower()
    True
    >>> import string
    >>> name2[0] in string.ascii_up percase
    True
    >>> [/code]HTH

    Comment

    • python101
      New Member
      • Sep 2007
      • 90

      #3
      Originally posted by bvdet
      [code=Python]>>> name1 = 'john smith'
      >>> name2 = 'John smith'
      >>> name1.lower() == name2.lower()
      True
      >>> import string
      >>> name2[0] in string.ascii_up percase
      True
      >>> [/code]HTH
      Thank you very much. I figured out just a few hours ago.
      [code=python]
      name2==name1.ca pitalize()
      [/code]
      Very simple but I am an absolute beginner +_+

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by python101
        Thank you very much. I figured out just a few hours ago.
        [code=python]
        name2==name1.ca pitalize()
        [/code]
        Very simple but I am an absolute beginner +_+
        Yep. That's the way I went, too. But looking at bvdet's example, I see that it does a far more complete job of making sure that the case of all the letters are the same.

        Comment

        Working...