s = 'abcdef'
L = [100, 200, 300]
t = ('tuple', 'object', 1, 2)
s = 'abcdef'
s_ = 0
l = [100, 200, 300]
s_ = s[0:3]
print(len(s_))
print(s[0:3])
print(s)
print(s[1])
print(s[-1])
print()
print(l[1])
l[1] = 900
print(l[1])
3 abc abcdef b f 200 900
print(l[100])
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-3-683522e35835> 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)
for s_ in s:
print(s_)
abcdef [1, 2, 3, 4, 5, 6] a b c d e f
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, end=" ")
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\n')
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
print('f' + str1[1:])
first String
str1[0:3] = 'abc'
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-19-d50725fd3c78> 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)
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 <class 'str'> a <class 'str'> bc <class 'str'> abc
print('Spam \uB610 Egg') # 문자열 내에 유티코드 이스케이프 문자인 \uHHHH 사용가능, HHHH는 4자리 16진수 (unicode 포맷)
Spam 또 Egg
#a = unicode('한글', 'utf-8') # '한글' 문자열의 인코딩 방식을 'utf-8'로 인식시키며 unicode로 변환 --> python3 부터는 지원되지 않음
a = str('한글')
print(type(a))
print(a)
# <type 'unicode'>
# 한글
<class 'str'> 한글
print(len('한글과 세종대왕'))
#print(len(unicode('한글과 세종대왕', 'utf-8'))) #python 3 에서 지원하지 않음
print(len(u'한글과 세종대왕'))
8 8
# u = unicode('한글과 세종대왕', 'utf-8') #python 3에서 지원하지 않음
u = str('한글과 세종대왕') #python 3 부터는 임의의 str은 유니코드이기 때문에 한글이 포함된 str에 대해서도 인덱싱 및 슬라이싱이 올바르게 수행됨
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일