<개인공부> - IT/[Python]
isupper(), islower() 함수이용하여 문자열의 대소문자 변경하기
Aggies '19
2018. 8. 19. 12:45
반응형
def switchcase(string):
switchcaseStr = ""
for index in range(len(string)):
# Condition : lowercase letter, replace lowercase letter to uppercase letter
if (string[index].islower()):
asciiIndex = ord(string[index]) - 32
switchcaseStr += chr(asciiIndex)
# Condition : uppercase letter, replace uppercase letter to lowercase letter
elif (string[index].isupper()):
asciiIndex = ord(string[index]) + 32
switchcaseStr += chr(asciiIndex)
# Condition : non alphabetical letter, leave it the letter
else:
switchcaseStr += string[index]
return switchcaseStr
print (switchcase("Hi Cassie! COLLEGE station weather is REALLY HoT"))
Python 3.6.1 (default, Dec 2015, 13:05:11) [GCC 4.8.2] on linux
반응형