[파이썬 초급 연습문제 #15] 단어 배열 뒤집기 (Reverse Word Order)

2019. 8. 28. 10:43컴퓨터_Com/파이썬 초급 연습문제_Exercise for bigginers

From www.practicepython.org 

Exercise 15: 여러개의 단어가 포함된 문장을 입력으로 받아서 단어의 순서를 뒤집는 프로그램을 만들어라 (함수 이용).

My name is Michele --> Michele is name My


string 나누기 (Splitting strings)

string은 리스트로 분류될 수 있으며 split을 이용하면 원하는 문자를 이용하여 분리가 가능하다. 

 

1
2
3
4
5
6
7
8
9
test = "this is a test"
result = test.split("t")
print(result)
['''his is a ''es''']
 
test1 = "this has a lot of spaces and tabs"
result1 = test1.split()
print(result1)
['this''has''a''lot''of''spaces''and''tabs']
cs

 

string 합치기 (Joining strings)

join을 이용하면 string을 합칠 수 있다. 

1
2
3
4
listOfStrings = ['a','b','c']
results = "**".join(listOfStrings)
print(results)
a**b**c
cs

 


내 풀이

 

15_stringReverse.py
0.00MB