본문 바로가기

[+] Information/[-] Python

두번째 연습문제.

1. spam and egg 문자열을 슬라이싱을 이용하여 spam egg로 변경하여라.

s = 'spam and egg'
>>> s = s[:5] + s[9:]

출력 결과:
'spam egg'


2. 인덱싱과 슬라이싱을 이용하여 s = 'spam and ham'을 'ham spam'으로 바꾸시오.

s = 'spam and ham'
>>> s = s[9:] + ' ' + s[:4]

출력 결과:
'ham spam'


3. 파이썬 경로명 s = '/usr/local/bin/python'에서 각각의 디렉토리 경로명을 분리하여 출력하시오.

s = '/usr/local/bin/python'
for a in s.split('/'):
 if a == '':
  print a,

출력 결과 :
usr local bin python


4.파이썬 경로명 s = '/usr/local/bin/python'에서 디렉토리 경로명과 파일명을 분리하여 출력하시오.

s = '/usr/local/bin/python'
a = s.split('/')
print '/'.join(a[:4]), a[-1]

출력 결과 :
/usr/local/bin python



5. 주어진 문자열에서 모든 대문자를 소문자로 변환하고, 문자 ',' 와 '.'를 없앤 후에 각 단어를 순서대로 출력하시오.

We propose to start by making it possible to teach programming in Python, an existing scripting language, and to focus on creating a new development evnironment and teaching materials for it.

>>> s = '''We propose to start by making it possible to teach programming in Python, an existing scripting language, and to focus on creating a new development evnironment and teaching materials for it.'''
>>> a = s.replace('.', '').replace(',', '').split()
>>> for x in a:
 print x.lower(),

 출력 결과 :
python we a an and and by creating development evnironment existing focus for in it it language making materials new on possible programming propose scripting start teach teaching to to to
(정확한 출력 값은 아닙니다. 제가 뒤죽박죽 해버려서 ㅡㅡ;;)


6. 위 문제에서 각 단어가 몇 번씩 나타났는가도 함께 출력해 보아라.

>>> for x in a:
 print x, x.count(x)

 
Python 1
We 1
a 1
an 1
and 1
and 1
by 1
creating 1
development 1
evnironment 1
existing 1
focus 1
for 1
in 1
it 1
it 1
language 1
making 1
materials 1
new 1
on 1
possible 1
programming 1
propose 1
scripting 1
start 1
teach 1
teaching 1
to 1
to 1
to 1

'[+] Information > [-] Python' 카테고리의 다른 글

level 0  (0) 2011.10.23
열혈강의 파이썬 잘못된 소스부분.  (2) 2009.08.23
파이썬 연습문제.(while)  (0) 2009.07.16
파이썬 실행 환경 설정.  (0) 2009.07.15