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)
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:
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!
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()
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)
ps: Sorry for the English.
Thanks in advance!
Comment