String
   1.1 Sequence Type
   1.2 Indexing
   1.3 Slicing
   1.4 Expansion Slicing
   1.5 connection
   1.6 Repeat
   1.7 Membership Test
   1.8 Length
   movie
   Solve Problem
   Practice

문자열

01 시퀀스 자료형의 특성

1-1 시퀀스 자료형이란?

  • 저장된 각 요소를 정수 Index를 이용하여 참조가 가능한 자료형
  • 시퀀스(Sequence) 자료형: 문자열, 리스트, 튜플
  • 시퀀스 자료형이 가지는 공통적인 연산
    • 인덱싱 (Indexing)
    • 슬라이싱 (Slicing)
    • 확장 슬라이싱 (Extended Slicing)
    • 연결 (Concatenation)
    • 반복 (Repitition)
    • 멤버쉽 테스트 (Membership Test)
    • 길이 정보 (Length)
    • for ~ in 문



1-2 인덱싱

1 s = 'abcdef'
2 l = [100,200,300]
3 print s[0]
4 print s[1]
5 print s[-1]
6 print
7 print l[1]
8 l[1] = 900
9 print l[1]
a
b
f

200
900



1-3 슬라이싱

 1 s = 'abcdef'
 2 L = [100, 200, 300]
 3 
 4 print s[1:3]
 5 print s[1:]
 6 print s[:]
 7 print s[-100:100]
 8 print
 9 print L[:-1]     # L[:2] 와 동일
10 print L[:2]
bc
bcdef
abcdef
abcdef

[100, 200]
[100, 200]



1-4 확장 슬라이싱

1 s = 'abcd'
2 print s[::2]   #step:2 - 오른쪽 방향으로 2칸씩
3 print s[::-1]  #step:-1 - 왼쪽 방향으로 1칸씩
ac
dcba



1-5 연결하기

1 s = 'abc' + 'def'
2 print s
3 
4 L = [1,2,3] + [4,5,6]
5 print L
abcdef
[1, 2, 3, 4, 5, 6]



1-6 반복하기

1 s = 'Abc'
2 print s * 4
3 
4 L = [1,2,3]
5 print L * 2
AbcAbcAbcAbc
[1, 2, 3, 1, 2, 3]



1-7 멤버십 테스트

1 s = 'abcde'
2 print 'c' in s
3 
4 t = (1,2,3,4,5)
5 print 2 in t
6 print 10 in t
7 print 10 not in t
True
True
False
True
1 print 'ab' in 'abcd'
2 print 'ad' in 'abcd'
3 print ' ' in 'abcd'
4 print ' ' in 'abcd '
True
False
False
True



1-8 길이 정보

1 s = 'abcde'
2 l = [1,2,3]
3 t = (1, 2, 3, 4)
4 print len(s)
5 print len(l)
6 print len(t)
5
3
4



참고 동영상





문제 풀어보기

1 s = 'hello, python'
2 print s[5]

1. 위의 코드 결과로 알맞은 것은??

(1) ,

(2) h

(3) 공백

(4) W


1 s1 = 'hello'
2 s2 = 'World'
3 print s1 + s2

2. 위의 코드 결과로 알맞은 것은??

(1) Hello,World

(2) Hello World

(3) HelloWorld

(4) Hello



파이썬 연습하기

파이썬 코드를 입력 후 RUN 버튼을 누르세요.

Morning Fog Emerging From Trees by A Guy Taking Pictures, on Flickr.


Link.