By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,199 Members | 1,120 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,199 IT Pros & Developers. It's quick & easy.

Question about how to solve this problem (Change char)

P: 20
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)

Expand|Select|Wrap|Line Numbers
  1. import re
  2.  
  3. def changeCharacters(stringToChange):
  4.  
  5.     string = ''.join(stringToChange).split(' ')
  6.  
  7.     regex_uppers = re.compile('[A-Z]$')
  8.     regex_lowers = re.compile('[a-z]$')
  9.     result = []
  10.  
  11.     for word in string:
  12.         for letter in word:
  13.             if re.search(regex_uppers, letter) != None:
  14.                 letter.lower()
  15.                 result.append(letter)
  16.             elif re.search(regex_lowers, letter) != None:
  17.                 letter.upper()
  18.                 result.append(letter)
  19.     print(result)
  20.  
  21.  
  22.  
  23. def main():
  24.     changeCharacters('My name is benjamin and i Loveee someone')
  25.  
  26. if __name__ == '__main__':
  27.     main()
  28.  
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:

Expand|Select|Wrap|Line Numbers
  1.     for word in string:
  2.         for letter in word:
  3.             if letter == letter.isupper():
  4.                 result.append(letter.lower())
  5.             elif letter == letter.islower():
  6.                 result.append(letter.upper())
  7.     print(result)
  8.  
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!
Aug 19 '11 #1

✓ answered by dwblas

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).
Expand|Select|Wrap|Line Numbers
  1.     result = []
  2.     for word in string_to_test:  ## "string" is a reserved word in Python
  3.         print "testing word", word
  4.         for letter in word:
  5.             print "     ", letter
  6.             if letter.isupper():
  7.                 letter = letter.lower()
  8.             elif letter.islower():
  9.                 letter = letter.upper()
  10.             result.append(letter)
  11.     print(result) 

Share this Question
Share on Google+
3 Replies


Expert 100+
P: 624
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).
Expand|Select|Wrap|Line Numbers
  1.     result = []
  2.     for word in string_to_test:  ## "string" is a reserved word in Python
  3.         print "testing word", word
  4.         for letter in word:
  5.             print "     ", letter
  6.             if letter.isupper():
  7.                 letter = letter.lower()
  8.             elif letter.islower():
  9.                 letter = letter.upper()
  10.             result.append(letter)
  11.     print(result) 
Aug 20 '11 #2

P: 10
To Ephexeve:
The method .isupper() returns a bool. The method .upper() returns a copy of your string converted to upper case.
http://www.tutorialspoint.com/python/string_isupper.htm
Aug 20 '11 #3

P: 20
I knew that, thank you anyway guys!
Nov 17 '11 #4

Post your reply

Sign in to post your reply or Sign up for a free account.