Question about how to solve this problem (Change char)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ephexeve
    New Member
    • May 2011
    • 20

    Question about how to solve this problem (Change char)

    Hey all,

    I am a beginner, and I would like to code a Python script which it will change the string to lower if the string is upper and change the upper to lower if the string is upper.

    I've been trying to write this way (it might look horrible)

    Code:
    import re
    
    def changeCharacters(stringToChange):
    
        string = ''.join(stringToChange).split(' ')
    
        regex_uppers = re.compile('[A-Z]$')
        regex_lowers = re.compile('[a-z]$')
        result = []
    
        for word in string:
            for letter in word:
                if re.search(regex_uppers, letter) != None:
                    letter.lower()
                    result.append(letter)
                elif re.search(regex_lowers, letter) != None:
                    letter.upper()
                    result.append(letter)
        print(result)
                    
                
        
    def main():
        changeCharacters('My name is benjamin and i Loveee someone')
    
    if __name__ == '__main__':
        main()
    But when I run this, I get all the letters appended to the list and it didn't convert anything. So I tried making the for loop like this:

    Code:
        for word in string:
            for letter in word:
                if letter == letter.isupper():
                    result.append(letter.lower())
                elif letter == letter.islower():
                    result.append(letter.upper())
        print(result)
    But then I got a empty list. Can someone give me some tips on what am I missing here? Just tips!

    ps: Sorry for the English.

    Thanks in advance!
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    Regarding the last code, you have to define the list before the for() loop for it to be seen outside of the for loop. I would also suggest that you print word and letter as I don't think they contain what you expect (even though the code works).
    Code:
        result = []
        for word in string_to_test:  ## "string" is a reserved word in Python
            print "testing word", word
            for letter in word:
                print "     ", letter
                if letter.isupper():
                    letter = letter.lower()
                elif letter.islower():
                    letter = letter.upper()
                result.append(letter)
        print(result)

    Comment

    • milesmajefski
      New Member
      • Aug 2011
      • 10

      #3
      To Ephexeve:
      The method .isupper() returns a bool. The method .upper() returns a copy of your string converted to upper case.

      Comment

      • Ephexeve
        New Member
        • May 2011
        • 20

        #4
        I knew that, thank you anyway guys!

        Comment

        Working...