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]
print l[100]
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]
s = 'abcd'
print s[::2] #step:2 - 각 원소들 사이의 거리가 인덱스 기준으로 2가 됨
print s[::-1] #step:-1 - 왼쪽 방향으로 1칸씩
s = 'abc' + 'def'
print s
L = [1,2,3] + [4,5,6]
print L
s = 'abc'
print s * 4
L = [1,2,3]
print L * 2
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
print 'ab' in 'abcd'
print 'ad' in 'abcd'
print ' ' in 'abcd'
print ' ' in 'abcd '
s = 'abcde'
l = [1,2,3]
t = (1, 2, 3, 4)
print len(s)
print len(l)
print len(t)
for c in 'abcd':
print c,
s = ''
str1 = 'Python is great!'
str2 = "Yes, it is."
str3 = "It's not like any other languages"
str4 = 'Don\'t walk. "Run"'
print str4
long_str = "This is a rather long string \
containing back slash and new line.\nGood!"
print long_str
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
이스케이프 문자 | 의미 |
---|---|
\ \ | \ |
\' | ' |
\" | " |
\b | 백스페이스 |
\n | 개행 |
\t | 탭 |
\0nn | 8진법 수 nn |
\xnn | 16진법 수 nn |
print '\\abc\\'
print
print 'abc\tdef\tghi'
print
print 'a\nb\nc'
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)]
str1[0] = 'f'
str1[0:3] = 'abc'
s = 'spam and egg'
s = s[:4] + ', cheese, ' + s[5:]
print s
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
print u'Spam \uB610 Egg' # 문자열 내에 유티코드 이스케이프 문자인 \uHHHH 사용가능, HHHH는 4자리 16진수 (unicode 포맷)
a = unicode('한글', 'utf-8') # '한글' 문자열의 인코딩 방식을 'utf-8'형태로 인식시키면서 해당 문자열을 unicode로 변환
print type(a)
print a
print len('한글과 세종대왕')
print len(unicode('한글과 세종대왕', 'utf-8')) #유니코드 타입의 문자열은 한글 문자열 길이를 올바르게 반환함
print len(u'한글과 세종대왕')
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일