1. 입력받은 문자열 거꾸로 출력하기
yourWord=input("Word : ") newWord="" for s in yourWord: newWord = s + newWord print(newWord)
< 결과 >
Word : 박화요비 비요화박
2. 입력받은 문자열 거꾸로 출력하기 (또다른 방법)
yourWord=input("Word : ") print(yourWord[::-1])
< 결과 >
Word : 소녀시대 대시녀소
3. 숫자 4개를 입력받은 후 숫자를 거꾸로하여 큰 순서대로 정렬
myNums={} for i in range(4): num = input("Number %s : " % (i+1)) myNums[num] = num[::-1] for i in sorted(myNums.values()): print(i)
< 결과 >
Number 1 : 954 Number 2 : 325 Number 3 : 109 Number 4 : 413 314 459 523 901
4. 입력받은 문자를 아스키 코드로 변환하고 코드 상의 다음 문자 출력하기
yourChar=input("Character: ") yourAscii=ord(yourChar) print("Character Ascii Code:",yourAscii) nextChar=yourAscii+1 print("Next Character :",chr(nextChar))
< 결과 >
Character: a Character Ascii Code: 97 Next Character : b
5. 입력받은 문자를 shift하여 출력하기
strMap="abcdefghijklmnopqrstuvwxyz" def enc(word,offset): thisOffset=0 baseAsciiCode=ord('a') toWord="" for s in word: if s in strMap: asciiGap=ord(s)-baseAsciiCode if asciiGap >= (26-offset): thisOffset=abs(asciiGap-26+offset) toWord+=chr(baseAsciiCode+thisOffset) else: thisOffset=offset toWord+=chr(ord(s)+thisOffset) else: toWord+=s return toWord yourWord=input("Word: ").lower() print(enc(yourWord,3)) print(enc(yourWord,5)) print(enc(yourWord,7))
< 결과 >
Word: hello world! khoor zruog! mjqqt btwqi! olssv dvysk!
6. 입력받은 문자를 +1만큼 shift하여 출력하기
toWord="" for s in input("Word: ").lower(): if s >= "a" and s <= "z": toWord+=chr(((ord(s) - 96) % 26)+97) else: toWord+=s print(toWord)
< 결과 >
Word: hello world! ifmmp xpsme!
7. 각종 이스케이프 문자 활용하기
a="This\tis\ttest" b="This\nis\ntest" c="This is \"test\"" d="This is \\test\\" print(a) print(b) print(c) print(d)
< 결과 >
This is test This is test This is "test" This is \test\
8. s라는 변수에 'Sarc Catalina'라는 문자열이 바인딩돼 있다고 했을 때 문자열의 슬라이싱 기능과 연결하기를 이용해 s의 값을 'Catalina Sarc'으로 변경해 보세요.
s="Sarc Catalina" strs=s.split(" ") newS="" for i in range(len(strs)): newS+=strs[len(strs)-(i+1)] newS+=" " print(newS[:-1])
< 결과 >
Catalina Sarc