s = 'abcdef'
L = [100,200,300]
t = ('tuple', 'object', 1, 2)
s = 'abcdef'
l = [100, 200, 300]
print s[0]
print s[1]
print s[-1]
print
print l[1]
l[1] = 900
print l[1]
a b f 200 900
print l[100]
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-2-dd85adc9a089> in <module>() ----> 1 print l[100] IndexError: list index out of range
s = 'abcdef'
L = [100, 200, 300]
print s[1:3]
print s[1:]
print s[:]
print s[-100:100]
print
print L[:-1] # L[:2] 와 동일
print L[:2]
bc bcdef abcdef abcdef [100, 200] [100, 200]
s = 'abcd'
print s[::2] #step:2 - 각 원소들 사이의 거리가 인덱스 기준으로 2가 됨
print s[::-1] #step:-1 - 왼쪽 방향으로 1칸씩
ac dcba
s = 'abc' + 'def'
print s
L = [1,2,3] + [4,5,6]
print L
abcdef [1, 2, 3, 4, 5, 6]
s = 'abc'
print s * 4
L = [1,2,3]
print L * 2
abcabcabcabc [1, 2, 3, 1, 2, 3]
s = 'abcde'
print 'c' in s
t = (1,2,3,4,5)
print 2 in t
print 10 in t
print 10 not in t
True True False True
print 'ab' in 'abcd'
print 'ad' in 'abcd'
print ' ' in 'abcd'
print ' ' in 'abcd '
True False False True
s = 'abcde'
l = [1,2,3]
t = (1, 2, 3, 4)
print len(s)
print len(l)
print len(t)
5 3 4
for c in 'abcd':
print c,
a b c d
s = ''
str1 = 'Python is great!'
str2 = "Yes, it is."
str3 = "It's not like any other languages"
str4 = 'Don\'t walk. "Run"'
print str4
Don't walk. "Run"
long_str = "This is a rather long string \
containing back slash and new line.\nGood!"
print long_str
This is a rather long string containing back slash and new line. Good!
multiline = """ While the rest of the world has been catching on to
the Perl scripting language, the Linux commnunity,
long since past the pleasing shock of Perl's power,
has been catching on to a different scripting animal -- Python."""
print multiline
print
ml = ''' While the rest of the world has been catching on to
the Perl scripting language, the Linux commnunity,
long since past the pleasing shock of Perl's power,
has been catching on to a different scripting animal -- Python.'''
print ml
While the rest of the world has been catching on to the Perl scripting language, the Linux commnunity, long since past the pleasing shock of Perl's power, has been catching on to a different scripting animal -- Python. While the rest of the world has been catching on to the Perl scripting language, the Linux commnunity, long since past the pleasing shock of Perl's power, has been catching on to a different scripting animal -- Python.
이스케이프 문자 | 의미 |
---|---|
\ \ | \ |
' | ' |
" | " |
\b | 백스페이스 |
\n | 개행 |
\t | 탭 |
\0nn | 8진법 수 nn |
\xnn | 16진법 수 nn |
print '\\abc\\'
print
print 'abc\tdef\tghi'
print
print 'a\nb\nc'
\abc\ abc def ghi a b c
str1 = 'First String'
str2 = 'Second String'
str3 = str1 + ' ' + str2
print str3
print str1 * 3
print
print str1[2]
print str1[1:-1]
print len(str1)
print
print str1[0:len(str1)]
First String Second String First StringFirst StringFirst String r irst Strin 12 First String
str1[0] = 'f'
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-47-7213ba3e679f> in <module>() ----> 1 str1[0] = 'f' TypeError: 'str' object does not support item assignment
str1[0:3] = 'abc'
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-48-08d42bad7f22> in <module>() ----> 1 str1[0:3] = 'abc' TypeError: 'str' object does not support item assignment
s = 'spam and egg'
s = s[:4] + ', cheese, ' + s[5:]
print s
spam, cheese, and egg
print u'Spam and Egg'
print
a = 'a'
b = u'bc'
print type(a)
print a
print type(b)
print b
print
c = a + b # 일반 문자열과 유니코드를 합치면 유니코드로 변환
print type(c)
print c
Spam and Egg <type 'str'> a <type 'unicode'> bc <type 'unicode'> abc
print u'Spam \uB610 Egg' # 문자열 내에 유티코드 이스케이프 문자인 \uHHHH 사용가능, HHHH는 4자리 16진수 (unicode 포맷)
Spam 또 Egg
a = unicode('한글', 'utf-8') # '한글' 문자열의 인코딩 방식을 'utf-8'형태로 인식시키면서 해당 문자열을 unicode로 변환
print type(a)
print a
<type 'unicode'> 한글
print len('한글과 세종대왕')
print len(unicode('한글과 세종대왕', 'utf-8')) #유니코드 타입의 문자열은 한글 문자열 길이를 올바르게 반환함
print len(u'한글과 세종대왕')
22 8 8
u = unicode('한글과 세종대왕', 'utf-8') #유니코드 타입의 한글 문자열에 대해서는 인덱싱 및 슬라이싱이 올바르게 수행됨
print u[0]
print u[1]
print u[:3]
print u[4:]
print u[::-1]
print
u2 = u'한글과 세종대왕'
print u2[0]
print u2[1]
print u2[:3]
print u2[4:]
print u2[::-1]
print
u3 = '한글과 세종대왕'
print u3[0]
print u3[1]
print u3[:3]
print u3[4:]
print u3[::-1]
print
한 글 한글과 세종대왕 왕대종세 과글한 한 글 한글과 세종대왕 왕대종세 과글한 � � 한 ��과 세종대왕 ��쀌녢츄� ��ꀸ꜕�
참고 문헌: 파이썬(열혈강의)(개정판 VER.2), 이강성, FreeLec, 2005년 8월 29일