728x90

์ง‘ํ•ฉ ์ž๋ฃŒํ˜•์€ ์–ด๋–ป๊ฒŒ ๋งŒ๋“ค๊นŒ?

>>> s1 = set([1, 2, 3])
>>> s1
{1, 2, 3}
>>> s2 = set("Hello")
>>> s2
{'e', 'H', 'l', 'o'}
  • set()์˜ ๊ด„ํ˜ธ ์•ˆ์— ๋ฆฌ์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ๋งŒ๋“ค๊ฑฐ๋‚˜ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•˜์—ฌ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

์ง‘ํ•ฉ ์ž๋ฃŒํ˜•์˜ ํŠน์ง•

  • ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.
  • ์ˆœ์„œ๊ฐ€ ์—†๋‹ค(Unordered).
  • set ์ž๋ฃŒํ˜•์€ ์ˆœ์„œ๊ฐ€ ์—†๊ธฐ(unordered) ๋•Œ๋ฌธ์— ์ธ๋ฑ์‹ฑ์„ ํ†ตํ•ด ์š”์†Œ๊ฐ’์„ ์–ป์„ ์ˆ˜ ์—†๋‹ค.
>>> s1 = set([1, 2, 3])
>>> l1 = list(s1)
>>> l1
[1, 2, 3]
>>> l1[0]
1
>>> t1 = tuple(s1)
>>> t1
(1, 2, 3)
>>> t1[0]
1
  • ๋งŒ์•ฝ set ์ž๋ฃŒํ˜•์— ์ €์žฅ๋œ ๊ฐ’์„ ์ธ๋ฑ์‹ฑ์œผ๋กœ ์ ‘๊ทผํ•˜๋ ค๋ฉด ๋ฆฌ์ŠคํŠธ๋‚˜ ํŠœํ”Œ๋กœ ๋ณ€ํ™˜ํ•œ ํ›„์— ์ ‘๊ทผํ•ด์•ผ ํ•œ๋‹ค.

๊ต์ง‘ํ•ฉ, ํ•ฉ์ง‘ํ•ฉ, ์ฐจ์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ

>>> s1 = set([1, 2, 3, 4, 5, 6])
>>> s2 = set([4, 5, 6, 7, 8, 9])

01. ๊ต์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ - &

>>> s1 & s2
{4, 5, 6}
  • &๋ฅผ ์ด์šฉํ•˜๋ฉด ๊ต์ง‘ํ•ฉ์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.
>>> s1.intersection(s2)
{4, 5, 6}
  • intersection ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

02. ํ•ฉ์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ - |

>>> s1 | s2
{1, 2, 3, 4, 5, 6, 7, 8, 9}
  • | ๋ฅผ ์ด์šฉํ•˜๋ฉด ํ•ฉ์ง‘ํ•ฉ์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ค‘๋ณตํ•ด์„œ ํฌํ•จ๋œ ๊ฐ’์€ 1๊ฐœ์”ฉ๋งŒ ํ‘œํ˜„๋œ๋‹ค.
>>> s1.union(s2)
{1, 2, 3, 4, 5, 6, 7, 8, 9}
  • union ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

03. ์ฐจ์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ

>>> s1 - s2
{1, 2, 3}
>>> s2 - s1
{8, 9, 7}
  • -(๋นผ๊ธฐ)๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ฐจ์ง‘ํ•ฉ์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.
>>> s1.difference(s2)
{1, 2, 3}
>>> s2.difference(s1)
{8, 9, 7}
  • difference ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

์ง‘ํ•ฉ ์ž๋ฃŒํ˜• ๊ด€๋ จ ํ•จ์ˆ˜

01. ๊ฐ’ 1๊ฐœ ์ถ”๊ฐ€ํ•˜๊ธฐ - add

>>> s1 = set([1, 2, 3])
>>> s1.add(4)
>>> s1
{1, 2, 3, 4}

02. ๊ฐ’ ์—ฌ๋Ÿฌ ๊ฐœ ์ถ”๊ฐ€ํ•˜๊ธฐ - update

>>> s1 = set([1, 2, 3])
>>> s1.update([4, 5, 6])
>>> s1
{1, 2, 3, 4, 5, 6}

03. ํŠน์ • ๊ฐ’ ์ œ๊ฑฐํ•˜๊ธฐ - remove

>>> s1 = set([1, 2, 3])
>>> s1.remove(2)
>>> s1
{1, 3}
728x90

๋”•์…”๋„ˆ๋ฆฌ๋ž€?

  • ๋”•์…”๋„ˆ๋ฆฌ๋Š” Key์™€ Value๋ฅผ ํ•œ ์Œ์œผ๋กœ ๊ฐ€์ง€๋Š” ์ž๋ฃŒํ˜•
  • ์˜ˆ์‹œ) key๊ฐ€ baseball์ด๋ผ๋ฉด value๋Š” ์•ผ๊ตฌ
  • ์ˆœ์ฐจ์ ์œผ๋กœ ํ•ด๋‹น ์š”์†Œ๊ฐ’์„ ๊ตฌํ•˜์ง€ ์•Š๊ณ  Key๋ฅผ ํ†ตํ•ด Value๋ฅผ ์–ป๋Š”๋‹ค. 

๋”•์…”๋„ˆ๋ฆฌ๋Š” ์–ด๋–ป๊ฒŒ ๋งŒ๋“ค๊นŒ?

{Key1: Value1, Key2: Value2, Key3: Value3, ...}
  • Key์™€ Value์˜ ์Œ ์—ฌ๋Ÿฌ ๊ฐœ๊ฐ€  {}๋กœ ๋‘˜๋Ÿฌ์‹ธ์—ฌ ์žˆ๊ณ  ๊ฐ๊ฐ์˜ ์š”์†Œ๋Š” Key:Value ํ˜•ํƒœ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๊ณ  ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•œ๋‹ค.
>>> dic = {'name': 'pey', 'phone': '010-9999-1234', 'birth': '1118'}
>>> a = {1: 'hi'}
>>> a = {'a': [1, 2, 3]}

๋”•์…”๋„ˆ๋ฆฌ ์Œ ์ถ”๊ฐ€, ์‚ญ์ œํ•˜๊ธฐ

01. ๋”•์…”๋„ˆ๋ฆฌ ์Œ ์ถ”๊ฐ€ํ•˜๊ธฐ

>>> a = {1: 'a'}
>>> a[2] = 'b'
>>> a
{1: 'a', 2: 'b'}

>>> a['name'] = 'pey'
>>> a
{1: 'a', 2: 'b', 'name': 'pey'}

>>> a[3] = [1, 2, 3]
>>> a
{1: 'a', 2: 'b', 'name': 'pey', 3: [1, 2, 3]}

02. ๋”•์…”๋„ˆ๋ฆฌ ์š”์†Œ ์‚ญ์ œํ•˜๊ธฐ

>>> del a[1]
>>> a
{2: 'b', 'name': 'pey', 3: [1, 2, 3]}

del a[key]


๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

01. ๋”•์…”๋„ˆ๋ฆฌ์—์„œ key๋ฅผ ์‚ฌ์šฉํ•ด value ์–ป๊ธฐ

>>> grade = {'pey': 10, 'julliet': 99}
>>> grade['pey']
10
>>> grade['julliet']
99

 

>>> a = {1:'a', 2:'b'}
>>> a[1]
'a'
>>> a[2]
'b'
  • ์—ฌ๊ธฐ์—์„œ a[1]์ด ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์€ ๋ฆฌ์ŠคํŠธ๋‚˜ ํŠœํ”Œ์˜ a[1]๊ณผ๋Š” ์ „ํ˜€ ๋‹ค๋ฅด๋‹ค.
  • ๋”•์…”๋„ˆ๋ฆฌ ๋ณ€์ˆ˜์—์„œ [] ์•ˆ์˜ ์ˆซ์ž 1์€ ๋‘ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ Key์— ํ•ด๋‹นํ•˜๋Š” 1์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. 
  • ๋”•์…”๋„ˆ๋ฆฌ๋Š” ๋ฆฌ์ŠคํŠธ๋‚˜ ํŠœํ”Œ์— ์žˆ๋Š” ์ธ๋ฑ์‹ฑ ๋ฐฉ๋ฒ•์„ ์ ์šฉํ•  ์ˆ˜ ์—†๋‹ค.

02. ๋”•์…”๋„ˆ๋ฆฌ ๋งŒ๋“ค ๋•Œ ์ฃผ์˜ํ•  ์‚ฌํ•ญ

>>> a = {1:'a', 1:'b'}
>>> a
{1: 'b'}
  • ๋”•์…”๋„ˆ๋ฆฌ์—์„œ Key๋Š” ๊ณ ์œ ํ•œ ๊ฐ’์ด๋ฏ€๋กœ ์ค‘๋ณต๋˜๋Š” Key ๊ฐ’์„ ์„ค์ •ํ•ด ๋†“์œผ๋ฉด ํ•˜๋‚˜๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ๊ฒƒ๋“ค์ด ๋ชจ๋‘ ๋ฌด์‹œ๋œ๋‹ค๋Š” ์ ์— ์ฃผ์˜!
  • ๋”•์…”๋„ˆ๋ฆฌ์—๋Š” ๋™์ผํ•œ Key๊ฐ€ ์ค‘๋ณต์œผ๋กœ ์กด์žฌํ•  ์ˆ˜ ์—†๋‹ค.
>>> a = {[1,2] : 'hi'}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'

 

  • Key์— ๋ฆฌ์ŠคํŠธ๋Š” ์“ธ ์ˆ˜ ์—†๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ํ•˜์ง€๋งŒ ํŠœํ”Œ์€ Key๋กœ ์“ธ ์ˆ˜ ์žˆ๋‹ค.
  • ๋ฆฌ์ŠคํŠธ๋Š” ๊ทธ ๊ฐ’์ด ๋ณ€ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— Key๋กœ ์“ธ ์ˆ˜ ์—†๋‹ค.

๋”•์…”๋„ˆ๋ฆฌ ๊ด€๋ จ ํ•จ์ˆ˜

01. key ๋ฆฌ์ŠคํŠธ ๋งŒ๋“ค๊ธฐ - keys

>>> a = {'name': 'pey', 'phone': '010-9999-1234', 'birth': '1118'}
>>> a.keys()
dict_keys(['name', 'phone', 'birth'])
  • a.keys()๋Š” ๋”•์…”๋„ˆ๋ฆฌ a์˜ Key๋งŒ์„ ๋ชจ์•„ dict_keys ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ด
  • ๋งŒ์•ฝ 3.0 ์ดํ›„ ๋ฒ„์ „์—์„œ ๋ฆฌํ„ด๊ฐ’์œผ๋กœ ๋ฆฌ์ŠคํŠธ๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ์—๋Š” list(a.keys())๋ฅผ ์‚ฌ์šฉ
  • ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ๋ณ„ ์ฐจ์ด๋Š” ์—†์ง€๋งŒ, ๋ฆฌ์ŠคํŠธ ๊ณ ์œ ์˜ append, insert, pop, remove, sort ํ•จ์ˆ˜๋Š” ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์—†๋‹ค.

02. value ๋ฆฌ์ŠคํŠธ ๋งŒ๋“ค๊ธฐ - values

>>> a.values()
dict_values(['pey', '010-9999-1234', '1118'])

03. key, value ์Œ ์–ป๊ธฐ - items

>>> a.items()
dict_items([('name', 'pey'), ('phone', '010-9999-1234'), ('birth', '1118')])
  • items ํ•จ์ˆ˜๋Š” Key์™€ Value์˜ ์Œ์„ ํŠœํ”Œ๋กœ ๋ฌถ์€ ๊ฐ’์„ dict_items ๊ฐ์ฒด๋กœ ๋ฆฌํ„ดํ•œ๋‹ค.

04. key: value ์Œ ๋ชจ๋‘ ์ง€์šฐ๊ธฐ - clear

>>> a.clear()
>>> a
{}
  • clear ํ•จ์ˆ˜๋Š” ๋”•์…”๋„ˆ๋ฆฌ ์•ˆ์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์‚ญ์ œํ•œ๋‹ค.

05. key๋กœ value ์–ป๊ธฐ - get

>>> a = {'name': 'pey', 'phone': '010-9999-1234', 'birth': '1118'}
>>> a.get('name')
'pey'
>>> a.get('phone')
'010-9999-1234'
  • get(x) ํ•จ์ˆ˜๋Š” x๋ผ๋Š” Key์— ๋Œ€์‘๋˜๋Š” Value๋ฅผ ๋ฆฌํ„ด
  • a.get('name')์€ a['name']์„ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ์™€ ๋™์ผํ•œ ๊ฒฐ๊ด๊ฐ’์„ ๋ฆฌํ„ด
>>> a = {'name':'pey', 'phone':'010-9999-1234', 'birth': '1118'}
>>> print(a.get('nokey'))
None
>>> print(a['nokey’])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'nokey'
  • a['nokey']์ฒ˜๋Ÿผ ๋”•์…”๋„ˆ๋ฆฌ์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” ํ‚ค๋กœ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋ ค๊ณ  ํ•  ๊ฒฝ์šฐ, a['nokey'] ๋ฐฉ์‹์€ ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ณ  a.get('nokey') ๋ฐฉ์‹์€ None์„ ๋ฆฌํ„ดํ•œ๋‹ค๋Š” ์ฐจ์ด
>>> a.get('nokey', 'foo')
'foo'
  • ๋”•์…”๋„ˆ๋ฆฌ ์•ˆ์— ์ฐพ์œผ๋ ค๋Š” Key๊ฐ€ ์—†์„ ๊ฒฝ์šฐ, ๋ฏธ๋ฆฌ ์ •ํ•ด ๋‘” ๋””ํดํŠธ ๊ฐ’์„ ๋Œ€์‹  ๊ฐ€์ ธ์˜ค๊ฒŒ ํ•˜๊ณ  ์‹ถ์„ ๋•Œ๋Š” get(x, '๋””ํดํŠธ ๊ฐ’')์„ ์‚ฌ์šฉํ•˜๋ฉด ํŽธ๋ฆฌ

06. ํ•ด๋‹น key๊ฐ€ ๋”•์…”๋„ˆ๋ฆฌ ์•ˆ์— ์žˆ๋Š”์ง€ ์กฐ์‚ฌํ•˜๊ธฐ - in

>>> a = {'name':'pey', 'phone':'010-9999-1234', 'birth': '1118'}
>>> 'name' in a
True
>>> 'email' in a
False
  • ๋”ฐ๋ผ์„œ 'name' in a๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์ฐธ(True)์„ ๋ฆฌํ„ดํ•œ๋‹ค. ์ด์™€ ๋ฐ˜๋Œ€๋กœ 'email'์€ a ๋”•์…”๋„ˆ๋ฆฌ ์•ˆ์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” Key์ด๋ฏ€๋กœ ๊ฑฐ์ง“(False)์„ ๋ฆฌํ„ด
728x90

ํŠœํ”Œ์€ ์–ด๋–ป๊ฒŒ ๋งŒ๋“ค๊นŒ?

t1 = ()
t2 = (1,)
t3 = (1, 2, 3)
t4 = 1, 2, 3
t5 = ('a', 'b', ('ab', 'cd'))
  • t2 = (1,)์ฒ˜๋Ÿผ ๋‹จ์ง€ 1๊ฐœ์˜ ์š”์†Œ๋งŒ์„ ๊ฐ€์งˆ ๋•Œ๋Š” ์š”์†Œ ๋’ค์— ์‰ผํ‘œ(,)๋ฅผ ๋ฐ˜๋“œ์‹œ ๋ถ™์—ฌ์•ผ ํ•œ๋‹ค.
  • t4 = 1, 2, 3์ฒ˜๋Ÿผ ์†Œ๊ด„ํ˜ธ(())๋ฅผ ์ƒ๋žตํ•ด๋„ ๋œ๋‹ค.
  • ํŠœํ”Œ๊ณผ ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ€์žฅ ํฐ ์ฐจ์ด๋Š” ์š”์†Ÿ๊ฐ’์„ ๋ณ€ํ™”์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š”์ง€์˜ ์—ฌ๋ถ€์ด๋‹ค. ์ฆ‰, ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Ÿ๊ฐ’์€ ๋ณ€ํ™”๊ฐ€ ๊ฐ€๋Šฅํ•˜๊ณ  ํŠœํ”Œ์˜ ์š”์†Ÿ๊ฐ’์€ ๋ณ€ํ™”๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. 
  • ์š”์†Ÿ๊ฐ’์„ ์‚ญ์ œํ•˜๊ฑฐ๋‚˜ ๋ณ€๊ฒฝํ•˜๋ ค๊ณ  ํ•˜๋ฉด ์˜ค๋ฅ˜ ๋ฐœ์ƒ

ํŠœํ”Œ ๋‹ค๋ฃจ๊ธฐ

01. ์ธ๋ฑ์‹ฑํ•˜๊ธฐ

>>> t1 = (1, 2, 'a', 'b')
>>> t1[0]
1
>>> t1[3]
'b'

02. ์Šฌ๋ผ์ด์‹ฑํ•˜๊ธฐ

>>> t1 = (1, 2, 'a', 'b')
>>> t1[1:]
(2, 'a', 'b')

03. ํŠœํ”Œ ๋”ํ•˜๊ธฐ

>>> t1 = (1, 2, 'a', 'b')
>>> t2 = (3, 4)
>>> t3 = t1 + t2
>>> t3
(1, 2, 'a', 'b', 3, 4)

04. ํŠœํ”Œ ๊ณฑํ•˜๊ธฐ

>>> t2 = (3, 4)
>>> t3 = t2 * 3
>>> t3
(3, 4, 3, 4, 3, 4)

05. ํŠœํ”Œ ๊ธธ์ด ๊ตฌํ•˜๊ธฐ

>>> t1 = (1, 2, 'a', 'b')
>>> len(t1)
4
728x90

๋ฆฌ์ŠคํŠธ๋Š” ์–ด๋–ป๊ฒŒ ๋งŒ๋“ค๊ณ  ์‚ฌ์šฉํ• ๊นŒ?

๋ฆฌ์ŠคํŠธ๋ช… = [์š”์†Œ1, ์š”์†Œ2, ์š”์†Œ3, ...]
>>> a = []
>>> b = [1, 2, 3]
>>> c = ['Life', 'is', 'too', 'short']
>>> d = [1, 2, 'Life', 'is']
>>> e = [1, 2, ['Life', 'is']]

๋ฆฌ์ŠคํŠธ์˜ ์ธ๋ฑ์‹ฑ๊ณผ ์Šฌ๋ผ์ด์‹ฑ

01. ๋ฆฌ์ŠคํŠธ์˜ ์ธ๋ฑ์‹ฑ

>>> a = [1, 2, 3, ['a', 'b', 'c']]
>>> a[0]
1
>>> a[-1]
['a', 'b', 'c']
>>> a[3]
['a', 'b', 'c']

>>> a[-1][0]
'a'
>>> a[-1][1]
'b'
>>> a[-1][2]
'c'

 

  • ํŒŒ์ด์ฌ์€ ์ˆซ์ž๋ฅผ 0๋ถ€ํ„ฐ ์„ธ๊ธฐ ๋•Œ๋ฌธ์— a[1]์ด ๋ฆฌ์ŠคํŠธ a์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๊ฐ€ ์•„๋‹ˆ๋ผ a[0]์ด ๋ฆฌ์ŠคํŠธ a์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ผ๋Š” ๊ฒƒ์„ ๋ช…์‹ฌํ•˜์ž. a[-1]์€ ๋ฌธ์ž์—ด์—์„œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ฆฌ์ŠคํŠธ a์˜ ๋งˆ์ง€๋ง‰ ์š”์†Ÿ๊ฐ’์„ ๋งํ•œ๋‹ค.

02. ๋ฆฌ์ŠคํŠธ์˜ ์Šฌ๋ผ์ด์‹ฑ

>>> a = [1, 2, 3, 4, 5]
>>> b = a[:2]
>>> c = a[2:]
>>> b
[1, 2]
>>> c
[3, 4, 5]
>>> a = [1, 2, 3, ['a', 'b', 'c'], 4, 5]
>>> a[2:5]
[3, ['a', 'b', 'c'], 4]
>>> a[3][:2]
['a', 'b']

๋ฆฌ์ŠคํŠธ ์—ฐ์‚ฐํ•˜๊ธฐ

01. ๋ฆฌ์ŠคํŠธ ๋”ํ•˜๊ธฐ(+)

>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> a + b
[1, 2, 3, 4, 5, 6]

02. ๋ฆฌ์ŠคํŠธ ๋ฐ˜๋ณตํ•˜๊ธฐ(*)

>>> a = [1, 2, 3]
>>> a * 3
[1, 2, 3, 1, 2, 3, 1, 2, 3]

03. ๋ฆฌ์ŠคํŠธ ๊ธธ์ด ๊ตฌํ•˜๊ธฐ

>>> a = [1, 2, 3]
>>> len(a)
3
  • len์€ ๋ฌธ์ž์—ด, ๋ฆฌ์ŠคํŠธ ์™ธ์— ํŠœํ”Œ๊ณผ ๋”•์…”๋„ˆ๋ฆฌ์—๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
>>> a = [1, 2, 3]
>>> a[2] + "hi"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'

>>> str(a[2]) + "hi"
3hi
// ๋งŒ์•ฝ ์ˆซ์ž์™€ ๋ฌธ์ž์—ด์„ ๋”ํ•ด์„œ ๋ฌธ์ž์—ด์„ ๋งŒ๋“ค๊ณ  ์‹ถ๋‹ค๋ฉด ์ˆซ์ž๋ฅผ ๋ฌธ์ž๋กœ ๋ฐ”๊พธ์–ด์•ผ ํ•œ๋‹ค.

๋ฆฌ์ŠคํŠธ์˜ ์ˆ˜์ •๊ณผ ์‚ญ์ œ

01. ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ’ ์ˆ˜์ •ํ•˜๊ธฐ

>>> a = [1, 2, 3]
>>> a[2] = 4
>>> a
[1, 2, 4]

02. del ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ๋ฆฌ์ŠคํŠธ ์š”์†Œ ์‚ญ์ œํ•˜๊ธฐ

del ๊ฐ์ฒด
  • del a[x]๋Š” x๋ฒˆ์งธ ์š”์†Ÿ๊ฐ’์„ ์‚ญ์ œํ•œ๋‹ค.
>>> a = [1, 2, 3]
>>> del a[1]
>>> a
[1, 3]

>>> a = [1, 2, 3, 4, 5]
>>> del a[2:]
>>> a
[1, 2]

๋ฆฌ์ŠคํŠธ ๊ด€๋ จ ํ•จ์ˆ˜

๋ฆฌ์ŠคํŠธ ๋ณ€์ˆ˜ ์ด๋ฆ„ ๋’ค์— ‘.’๋ฅผ ๋ถ™์—ฌ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฆฌ์ŠคํŠธ ๊ด€๋ จ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉ

01. ๋ฆฌ์ŠคํŠธ์— ์š”์†Œ ์ถ”๊ฐ€ํ•˜๊ธฐ - append

>>> a = [1, 2, 3]
>>> a.append(4)
>>> a
[1, 2, 3, 4]

>>> a.append([5, 6])
>>> a
[1, 2, 3, 4, [5, 6]]

02. ๋ฆฌ์ŠคํŠธ ์ •๋ ฌ - sort

>>> a = [1, 4, 3, 2]
>>> a.sort()
>>> a
[1, 2, 3, 4]

>>> a = ['a', 'c', 'b']
>>> a.sort()
>>> a
['a', 'b', 'c']

03. ๋ฆฌ์ŠคํŠธ ๋’ค์ง‘๊ธฐ - reverse

>>> a = ['a', 'c', 'b']
>>> a.reverse()
>>> a
['b', 'c', 'a']

04. ์ธ๋ฑ์Šค ๋ฐ˜ํ™˜ - index

>>> a = [1, 2, 3]
>>> a.index(3)
2
>>> a.index(1)
0
>>> a.index(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 0 is not in list

05. ๋ฆฌ์ŠคํŠธ์— ์š”์†Œ ์‚ฝ์ž… - insert

>>> a = [1, 2, 3]
>>> a.insert(0, 4)
>>> a
[4, 1, 2, 3]

>>> a.insert(3, 5)
>>> a
[4, 1, 2, 5, 3]

 

  • insert(a, b)๋Š” ๋ฆฌ์ŠคํŠธ์˜ a๋ฒˆ์งธ ์œ„์น˜์— b๋ฅผ ์‚ฝ์ž…ํ•˜๋Š” ํ•จ์ˆ˜

06. ๋ฆฌ์ŠคํŠธ ์š”์†Œ ์ œ๊ฑฐ - remove

>>> a = [1, 2, 3, 1, 2, 3]
>>> a.remove(3)
>>> a
[1, 2, 1, 2, 3]

>>> a.remove(3)
>>> a
[1, 2, 1, 2]

 

  • remove(x)๋Š” ๋ฆฌ์ŠคํŠธ์—์„œ ์ฒซ ๋ฒˆ์งธ๋กœ ๋‚˜์˜ค๋Š” x๋ฅผ ์‚ญ์ œํ•˜๋Š” ํ•จ์ˆ˜

07. ๋ฆฌ์ŠคํŠธ ์š”์†Œ ๋„์ง‘์–ด ๋‚ด๊ธฐ - pop

>>> a = [1, 2, 3]
>>> a.pop()
3
>>> a
[1, 2]

>>> a = [1, 2, 3]
>>> a.pop(1)
2
>>> a
[1, 3]
  • pop()์€ ๋ฆฌ์ŠคํŠธ์˜ ๋งจ ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ๋ฆฌํ„ดํ•˜๊ณ  ๊ทธ ์š”์†Œ๋Š” ์‚ญ์ œํ•˜๋Š” ํ•จ์ˆ˜
  • pop(x)๋Š” ๋ฆฌ์ŠคํŠธ์˜ x๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๋ฆฌํ„ดํ•˜๊ณ  ๊ทธ ์š”์†Œ๋Š” ์‚ญ์ œํ•œ๋‹ค.

08. ๋ฆฌ์ŠคํŠธ์— ํฌํ•จ๋œ ์š”์†Œ x์˜ ๊ฐœ์ˆ˜ ์„ธ๊ธฐ - count

>>> a = [1, 2, 3, 1]
>>> a.count(1)
2
  •  count(x)๋Š” ๋ฆฌ์ŠคํŠธ ์•ˆ์— x๊ฐ€ ๋ช‡ ๊ฐœ ์žˆ๋Š”์ง€ ์กฐ์‚ฌํ•˜์—ฌ ๊ทธ ๊ฐœ์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜

09. ๋ฆฌ์ŠคํŠธ ํ™•์žฅ - extend

>>> a = [1, 2, 3]
>>> a.extend([4, 5])
>>> a
[1, 2, 3, 4, 5]
>>> b = [6, 7]
>>> a.extend(b)
>>> a
[1, 2, 3, 4, 5, 6, 7]
  • extend(x)์—์„œ x์—๋Š” ๋ฆฌ์ŠคํŠธ๋งŒ ์˜ฌ ์ˆ˜ ์žˆ์œผ๋ฉฐ ์›๋ž˜์˜ a ๋ฆฌ์ŠคํŠธ์— x ๋ฆฌ์ŠคํŠธ๋ฅผ ๋”ํ•˜๋Š” ํ•จ์ˆ˜
  • a.extend([4,5])๋Š” a += [4,5]์™€ ๋™์ผํ•˜๋‹ค.
728x90
๋ฌธ์ž์—ด(String)

๋ฌธ์ž์—ด(string)์ด๋ž€ ์—ฐ์†๋œ ๋ฌธ์ž๋“ค์˜ ๋‚˜์—ด.

๋ชจ๋“  ์˜ˆ๋ฌธ์ด ํฐ๋”ฐ์˜ดํ‘œ(")๋กœ ๋‘˜๋Ÿฌ์‹ธ์—ฌ ์žˆ๋‹ค.

๋”ฐ์˜ดํ‘œ๋กœ ๋‘˜๋Ÿฌ์‹ธ์—ฌ ์žˆ์œผ๋ฉด ๋ชจ๋‘ ๋ฌธ์ž์—ด์ด๋ผ๊ณ  ๋ณด๋ฉด ๋œ๋‹ค.


๋ฌธ์ž์—ด์€ ์–ด๋–ป๊ฒŒ ๋งŒ๋“ค๊ณ  ์‚ฌ์šฉํ• ๊นŒ?

01. ํฐ๋”ฐ์˜ดํ‘œ๋กœ ์–‘์ชฝ ๋‘˜๋Ÿฌ์‹ธ๊ธฐ

"Hello World"

02. ์ž‘์€๋”ฐ์˜ดํ‘œ๋กœ ์–‘์ชฝ ๋‘˜๋Ÿฌ์‹ธ๊ธฐ

'Python is fun'

03. ํฐ๋”ฐ์˜ดํ‘œ 3๊ฐœ๋ฅผ ์—ฐ์†์œผ๋กœ ์จ์„œ ์–‘์ชฝ ๋‘˜๋Ÿฌ์‹ธ๊ธฐ

"""Life is too short, You need python"""

04. ์ž‘์€๋”ฐ์˜ดํ‘œ 3๊ฐœ๋ฅผ ์—ฐ์†์œผ๋กœ ์จ์„œ ์–‘์ชฝ ๋‘˜๋Ÿฌ์‹ธ๊ธฐ

'''Life is too short, You need python'''

๋ฌธ์ž์—ด ์•ˆ์— ์ž‘์€๋”ฐ์˜ดํ‘œ๋‚˜ ํฐ๋”ฐ์˜ดํ‘œ๋ฅผ ํฌํ•จ์‹œํ‚ค๊ณ  ์‹ถ์„ ๋•Œ

01. ๋ฌธ์ž์—ด์— ์ž‘์€๋”ฐ์˜ดํ‘œ ํฌํ•จํ•˜๊ธฐ

Python's favorite food is perl

>>> food = "Python's favorite food is perl"
>>> food
"Python's favorite food is perl"
  • ๋ฌธ์ž์—ด ์ค‘์— ์ž‘์€๋”ฐ์˜ดํ‘œ ' ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ ๋ฌธ์ž์—ด์„ ํฐ๋”ฐ์˜ดํ‘œ๋กœ ๋‘˜๋Ÿฌ์‹ธ์•ผํ•œ๋‹ค.
  • ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด ๋ณด๋ฉด ๋ณ€์ˆ˜์— ์ €์žฅ๋œ ๋ฌธ์ž์—ด์ด ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
  • ๋ฌธ์ž์—ด์„ ํฐ๋”ฐ์˜ดํ‘œ๊ฐ€ ์•„๋‹Œ ์ž‘์€๋”ฐ์˜ดํ‘œ๋กœ ๋‘˜๋Ÿฌ์‹ผ ํ›„ ์‹คํ–‰ํ•˜๋ฉด ๊ตฌ๋ฌธ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

02. ๋ฌธ์ž์—ด์— ํฐ๋”ฐ์˜ดํ‘œ ํฌํ•จํ•˜๊ธฐ

>>> say = '"Python is very easy." he says.'
  • ๋ฌธ์ž์—ด์— ํฐ๋”ฐ์˜ดํ‘œ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋‹ค๋ฉด ๋ฌธ์ž์—ด์„ ์ž‘์€๋”ฐ์˜ดํ‘œ๋กœ ๋‘˜๋Ÿฌ์‹ธ๋ฉด ๋œ๋‹ค.

03. ์—ญ์Šฌ๋ž˜์‹œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ž‘์€๋”ฐ์˜ดํ‘œ์™€ ํฐ๋”ฐ์˜ดํ‘œ๋ฅผ ๋ฌธ์ž์—ด์— ํฌํ•จํ•˜๊ธฐ

>>> food = 'Python\'s favorite food is perl'
>>> say = "\"Python is very easy.\" he says."
  • ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์€ ์—ญ์Šฌ๋ž˜์‹œ \ ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.
  • ์—ญ์Šฌ๋ž˜์‹œ๋ฅผ ์ž‘์€๋”ฐ์˜ดํ‘œ๋‚˜ ํฐ๋”ฐ์˜ดํ‘œ ์•ž์— ์‚ฝ์ž…ํ•˜๋ฉด ์—ญ์Šฌ๋ž˜์‹œ ๋’ค์˜ ์ž‘์€๋”ฐ์˜ดํ‘œ๋‚˜ ํฐ๋”ฐ์˜ดํ‘œ๋Š” ๋ฌธ์ž์—ด์„ ๋‘˜๋Ÿฌ์‹ธ๋Š” ๊ธฐํ˜ธ์˜ ์˜๋ฏธ๊ฐ€ ์•„๋‹Œ ' ๋‚˜ "์ž์ฒด๋ฅผ ์˜๋ฏธ

์—ฌ๋Ÿฌ ์ค„์ธ ๋ฌธ์ž์—ด์„ ๋ณ€์ˆ˜์— ๋Œ€์ž…ํ•˜๊ณ  ์‹ถ์„ ๋•Œ

01. ์ค„์„ ๋ฐ”๊พธ๊ธฐ ์œ„ํ•œ ์ด์Šค์ผ€์ดํ”„ ์ฝ”๋“œ \n ์‚ฝ์ž…ํ•˜๊ธฐ

>>> multiline = "Life is too short\nYou need python"
  • ์œ„์ฒ˜๋Ÿผ ์ค„ ๋ฐ”๊ฟˆ ๋ฌธ์ž์ธ \n์„ ์‚ฝ์ž…ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์ง€๋งŒ, ์ฝ๊ธฐ๊ฐ€ ๋ถˆํŽธํ•˜๊ณ  ์ค„์ด ๊ธธ์–ด์ง€๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค.

02. ์—ฐ์†๋œ ์ž‘์€๋”ฐ์˜ดํ‘œ 3๊ฐœ ๋˜๋Š” ํฐ๋”ฐ์˜ดํ‘œ 3๊ฐœ ์‚ฌ์šฉํ•˜๊ธฐ

>>> multiline='''
... Life is too short
... You need python
... '''

>>> multiline="""
... Life is too short
... You need python
... """

>>> print(multiline)
Life is too short
You need python
  • \n์˜ ๋‹จ์ ์„ ๊ทน๋ณตํ•˜๊ธฐ ์œ„ํ•ด ์ž‘์€๋”ฐ์˜ดํ‘œ 3๊ฐœ ''' ๋˜๋Š” ํฐ ๋”ฐ์˜ดํ‘œ 3๊ฐœ """๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  • ์—ฌ๋Ÿฌ ์ค„์ธ ๊ฒฝ์šฐ, ์ด์Šค์ผ€์ดํ”„ ์ฝ”๋“œ๋ฅผ ์“ฐ๋Š” ๊ฒƒ๋ณด๋‹ค ๋”ฐ์˜ดํ‘œ 3๊ฐœ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๊น”๋”ํ•˜๋‹ค.

์ด์Šค์ผ€์ดํ”„ ์ฝ”๋“œ๋ž€?

์ด์Šค์ผ€์ดํ”„(escape) ์ฝ”๋“œ: ํ”„๋กœ๊ทธ๋ž˜๋ฐํ•  ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ฏธ๋ฆฌ ์ •์˜ํ•ด ๋‘” '๋ฌธ์ž ์กฐํ•ฉ'

์ฝ”๋“œ ์„ค๋ช…
\n ๋ฌธ์ž์—ด ์•ˆ์—์„œ ์ค„์„ ๋ฐ”๊ฟ€ ๋•Œ ์‚ฌ์šฉ
\t ๋ฌธ์ž์—ด ์‚ฌ์ด์— ํƒญ ๊ฐ„๊ฒฉ์„ ์ค„ ๋•Œ ์‚ฌ์šฉ
\\ \๋ฅผ ๊ทธ๋Œ€๋กœ ํ‘œํ˜„ํ•  ๋•Œ ์‚ฌ์šฉ
\' ์ž‘์€๋”ฐ์˜ดํ‘œ๋ฅผ ๊ทธ๋Œ€๋กœ ํ‘œํ˜„ํ•  ๋•Œ ์‚ฌ์šฉ
\" ํฐ๋”ฐ์˜ดํ‘œ๋ฅผ ๊ทธ๋Œ€๋กœ ํ‘œํ˜„ํ•  ๋•Œ ์‚ฌ์šฉ
\r ์บ๋ฆฌ์ง€ ๋ฆฌํ„ด(์ค„ ๋ฐ”๊ฟˆ ๋ฌธ์ž, ์ปค์„œ๋ฅผ ํ˜„์žฌ ์ค„์˜ ๊ฐ€์žฅ ์•ž์œผ๋กœ ์ด๋™)
\f ํผ ํ”ผ๋“œ(์ค„ ๋ฐ”๊ฟˆ ๋ฌธ์ž, ์ปค์„œ๋ฅผ ํ˜„์žฌ ์ค„์˜ ๋‹ค์Œ ์ค„๋กœ ์ด๋™)
\a ๋ฒจ ์†Œ๋ฆฌ(์ถœ๋ ฅํ•  ๋•Œ PC ์Šคํ”ผ์ปค์—์„œ '์‚‘' ์†Œ๋ฆฌ๊ฐ€ ๋‚œ๋‹ค
\b ๋ฐฑ ์ŠคํŽ˜์ด์Šค
\000 ๋„ ๋ฌธ์ž

→ ์ด ์ค‘์—์„œ ํ™œ์šฉ ๋นˆ๋„๊ฐ€ ๋†’์€ ๊ฒƒ์€ \n, \t, \\, \', \"


๋ฌธ์ž์—ด ์—ฐ์‚ฐํ•˜๊ธฐ

01. ๋ฌธ์ž์—ด ๋”ํ•ด์„œ ์—ฐ๊ฒฐํ•˜๊ธฐ

>>> head = "Python"
>>> tail = " is fun!"
>>> head + tail
'Python is fun!'

02. ๋ฌธ์ž์—ด ๊ณฑํ•˜๊ธฐ

>>> a = "python"
>>> a * 2
'pythonpython'
  • ์œ„ ์†Œ์Šค ์ฝ”๋“œ์—์„œ a * 2๋ผ๋Š” ๋ฌธ์žฅ์€ a๋ฅผ 2๋ฒˆ ๋ฐ˜๋ณตํ•˜๋ผ๋Š” ๋œป์ด๋‹ค.
  • ์ฆ‰, *๋Š” ๋ฌธ์ž์—ด์˜ ๋ฐ˜๋ณต์„ ๋œปํ•˜๋Š” ์˜๋ฏธ๋กœ ์‚ฌ์šฉ๋˜์—ˆ๋‹ค. 

03. ๋ฌธ์ž์—ด ๊ณฑํ•˜๊ธฐ๋ฅผ ์‘์šฉํ•˜๊ธฐ

# multistring.py

print("=" * 50)
print("My Program")
print("=" * 50)
  • IDLE ์—๋””ํ„ฐ๋ฅผ ์—ด์–ด ์†Œ์Šค ์ž…๋ ฅํ•˜๊ณ  C:\doit ๋””๋ ‰ํ„ฐ๋ฆฌ์— multistring.py๋ผ๋Š” ํŒŒ์ผ ์ด๋ฆ„์œผ๋กœ ์ €์žฅํ•˜๊ณ 
  • ์‹คํ–‰ ์ฐฝ ์—ด๊ธฐ(์œˆ๋„+R) → cmd ์ž…๋ ฅ 
C:\Users>cd C:\doit
C:\doit>python multistring.py
==================================================
My Program
==================================================

๋ฌธ์ž์—ด ๊ธธ์ด ๊ตฌํ•˜๊ธฐ

๋ฌธ์ž์—ด ๊ธธ์ด๋Š” len ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

>>> a = "Life is too short"
>>> len(a)
17

๋ฌธ์ž์—ด ์ธ๋ฑ์‹ฑ๊ณผ ์Šฌ๋ผ์ด์‹ฑ

indexing = ๊ฐ€๋ฆฌํ‚จ๋‹ค

slicing = ์ž˜๋ผ ๋‚ธ๋‹ค

01. ๋ฌธ์ž์—ด ์ธ๋ฑ์‹ฑ

>>> a = "Life is too short, You need Python"

  • "Life is too short, You need Python" ๋ฌธ์ž์—ด์—์„œ L์€ ์ฒซ ๋ฒˆ์งธ ์ž๋ฆฌ๋ฅผ ๋œปํ•˜๋Š” ์ˆซ์ž 0, i๋Š” 1 ์ด๋Ÿฐ ์‹์œผ๋กœ ๊ณ„์† ๋ฒˆํ˜ธ๋ฅผ ๋ถ™์ธ ๊ฒƒ์ด๋‹ค. ์ฆ‰, ์ค‘๊ฐ„์— ์žˆ๋Š” short์˜ s๋Š” 12๊ฐ€ ๋œ๋‹ค.
>>> a = "Life is too short, You need Python"
>>> a[3]
'e'
  • a[3]์ด ๋œปํ•˜๋Š” ๊ฒƒ์€ a๋ผ๋Š” ๋ฌธ์ž์—ด์˜ ๋„ค ๋ฒˆ์งธ ๋ฌธ์ž e๋ฅผ ๋งํ•œ๋‹ค.
  • "ํŒŒ์ด์ฌ์€ 0๋ถ€ํ„ฐ ์ˆซ์ž๋ฅผ ์„ผ๋‹ค."
a[0]:'L', a[1]:'i', a[2]:'f', a[3]:'e', a[4]:' ', ...
  • a[๋ฒˆํ˜ธ]๋Š” ๋ฌธ์ž์—ด ์•ˆ์˜ ํŠน์ •ํ•œ ๊ฐ’์„ ๋ฝ‘์•„๋‚ด๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ์ด๋Ÿฌํ•œ ์ž‘์—…์„ ‘์ธ๋ฑ์‹ฑ’

02. ๋ฌธ์ž์—ด ์ธ๋ฑ์‹ฑ ํ™œ์šฉํ•˜๊ธฐ

>>> a = "Life is too short, You need Python"
>>> a[0]
'L'
>>> a[12]
's'
>>> a[-1]
'n'
  • ๋ฌธ์ž์—ด์„ ๋’ค์—์„œ๋ถ€ํ„ฐ ์ฝ๊ธฐ ์œ„ํ•ด -(๋นผ๊ธฐ) ๊ธฐํ˜ธ๋ฅผ ๋ถ™์ธ ๊ฒƒ์ด๋‹ค.
  • ์ฆ‰, a[-1]์€ ๋’ค์—์„œ๋ถ€ํ„ฐ ์„ธ์–ด ์ฒซ ๋ฒˆ์งธ๊ฐ€ ๋˜๋Š” ๋ฌธ์ž๋ฅผ ๋งํ•œ๋‹ค.
  • a์˜ ๊ฐ’์€ "Life is too short, You need Python" ๋ฌธ์ž์—ด์ด๋ฏ€๋กœ ๋’ค์—์„œ๋ถ€ํ„ฐ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž๋Š” ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž 'n'์ด๋‹ค.
>>> a[-0]
'L'
>>> a[-2]
'o'
>>> a[-5]
'y'
  • 0๊ณผ -0์€ ๋˜‘๊ฐ™์€ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— a[-0]์€ a[0]๊ณผ ๋˜‘๊ฐ™์€ ๊ฐ’์„ ๋ณด์—ฌ ์ค€๋‹ค.

03. ๋ฌธ์ž์—ด ์Šฌ๋ผ์ด์‹ฑ

>>> a = "Life is too short, You need Python"
>>> b = a[0] + a[1] + a[2] + a[3]
>>> b
'Life'
>>> a = "Life is too short, You need Python"
>>> a[0:4]
'Life'
  • ์œ„ ์ฝ”๋“œ์™€ ๊ฐ™์ด ๋‹จ์ˆœํ•˜๊ฒŒ ์ ‘๊ทผํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ ํŒŒ์ด์ฌ์—์„œ๋Š” ์Šฌ๋ผ์ด์‹ฑ(slicing) ๊ธฐ๋ฒ•์„ ์ œ๊ณตํ•œ๋‹ค.
  • a[0:4]๋Š” a ๋ฌธ์ž์—ด, ์ฆ‰ "Life is too short, You need Python" ๋ฌธ์ž์—ด์—์„œ ์ž๋ฆฌ ๋ฒˆํ˜ธ 0๋ถ€ํ„ฐ 4๊นŒ์ง€์˜ ๋ฌธ์ž๋ฅผ ๋ฝ‘์•„๋‚ธ๋‹ค๋Š” ๋œป์ด๋‹ค.
>>> a[0:3]
'Lif'
  • ์Šฌ๋ผ์ด์‹ฑ ๊ธฐ๋ฒ•์œผ๋กœ a[์‹œ์ž‘_๋ฒˆํ˜ธ:๋_๋ฒˆํ˜ธ]๋ฅผ ์ง€์ •ํ•  ๋•Œ ๋ ๋ฒˆํ˜ธ์— ํ•ด๋‹นํ•˜๋Š” ๋ฌธ์ž๋Š” ํฌํ•จํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
  • ์ฆ‰, a[0:3]์„ ์ˆ˜์‹์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. 0<= a < 3

04. ๋ฌธ์ž์—ด์„ ์Šฌ๋ผ์ด์‹ฑํ•˜๋Š” ๋ฐฉ๋ฒ•

>>> a[0:5]
'Life '
  • ์œ„ ์˜ˆ๋Š” a[0] + a[1] + a[2] + a[3] + a[4]์™€ ๋™์ผํ•˜๋‹ค.
  • a[4]๋Š” ๊ณต๋ฐฑ ๋ฌธ์ž์ด๊ธฐ ๋•Œ๋ฌธ์— 'Life'๊ฐ€ ์•„๋‹Œ 'Life '๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.
  • ๊ณต๋ฐฑ ๋ฌธ์ž ์—ญ์‹œ L, i, f, e ์™€ ๊ฐ™์€ ๋ฌธ์ž์™€ ๋™์ผํ•˜๊ฒŒ ์ทจ๊ธ‰๋˜๋Š” ๊ฒƒ์„ ์žŠ์ง€ ๋ง์ž.
>>> a[0:2]
'Li'
>>> a[5:7]
'is'
>>> a[12:17]
'short'
  • ์Šฌ๋ผ์ด์‹ฑํ•  ๋•Œ ํ•ญ์ƒ ์‹œ์ž‘ ๋ฒˆํ˜ธ๊ฐ€ 0์ผ ํ•„์š”๋Š” ์—†๋‹ค.
>>> a[19:]
'You need Python'
  • a[์‹œ์ž‘_๋ฒˆํ˜ธ:๋_๋ฒˆํ˜ธ]์—์„œ ๋ ๋ฒˆํ˜ธ ๋ถ€๋ถ„์„ ์ƒ๋žตํ•˜๋ฉด ์‹œ์ž‘ ๋ฒˆํ˜ธ๋ถ€ํ„ฐ ๊ทธ ๋ฌธ์ž์—ด์˜ ๋๊นŒ์ง€ ๋ฝ‘์•„๋‚ธ๋‹ค.
>>> a[:17]
'Life is too short'
  • a[์‹œ์ž‘_๋ฒˆํ˜ธ:๋_๋ฒˆํ˜ธ]์—์„œ ์‹œ์ž‘ ๋ฒˆํ˜ธ๋ฅผ ์ƒ๋žตํ•˜๋ฉด ๋ฌธ์ž์—ด์˜ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋ ๋ฒˆํ˜ธ๊นŒ์ง€ ๋ฝ‘์•„๋‚ธ๋‹ค.
>>> a[:]
'Life is too short, You need Python'
  • a[์‹œ์ž‘_๋ฒˆํ˜ธ:๋_๋ฒˆํ˜ธ]์—์„œ ์‹œ์ž‘ ๋ฒˆํ˜ธ์™€ ๋ ๋ฒˆํ˜ธ๋ฅผ ์ƒ๋žตํ•˜๋ฉด ๋ฌธ์ž์—ด์˜ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ๋ฝ‘์•„๋‚ธ๋‹ค.
>>> a[19:-7]
'You need'
  • ์Šฌ๋ผ์ด์‹ฑ์—์„œ๋„ ์ธ๋ฑ์‹ฑ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ -(๋นผ๊ธฐ) ๊ธฐํ˜ธ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • a[19:-7]์€ a[19]์—์„œ a[-8]๊นŒ์ง€๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ์ด๋•Œ์—๋„ a[-7]์€ ํฌํ•จํ•˜์ง€ ์•Š๋Š”๋‹ค.

05. ์Šฌ๋ผ์ด์‹ฑ์œผ๋กœ ๋ฌธ์ž์—ด ๋‚˜๋ˆ„๊ธฐ

>>> a = "20230331Rainy"
>>> year = a[:4]
>>> day = a[4:8]
>>> weather = a[8:]
>>> year
'2023'
>>> day
'0331'
>>> weather
'Rainy'
  • a[:8]์€ a[8]์„ ํฌํ•จํ•˜์ง€ ์•Š๊ณ  a[8:]์€ a[8]์„ ํฌํ•จํ•˜๊ธฐ ๋•Œ๋ฌธ์— 8์„ ๊ธฐ์ค€์œผ๋กœ ํ•ด์„œ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค.

Pithon ๋ฌธ์ž์—ด์„ Python์œผ๋กœ ๋ฐ”๊พธ๋ ค๋ฉด?

>>> a = "Pithon"
>>> a[:1]
'P'
>>> a[2:]
'thon'
>>> a[:1] + 'y' + a[2:]
'Python'

๋ฌธ์ž์—ด ํฌ๋งคํŒ…์ด๋ž€?

๋ฌธ์ž์—ด ํฌ๋งคํŒ… ๋”ฐ๋ผ ํ•˜๊ธฐ

01. ์ˆซ์ž ๋ฐ”๋กœ ๋Œ€์ž…

>>> "I eat %d apples." % 3
'I eat 3 apples.'
  • ๋ฌธ์ž์—ด ์•ˆ์˜ ์ˆซ์ž๋ฅผ ๋„ฃ๊ณ  ์‹ถ์€ ์ž๋ฆฌ์— %d ๋ฌธ์ž๋ฅผ ๋„ฃ์–ด ์ฃผ๊ณ  ์‚ฝ์ž…ํ•  ์ˆซ์ž๋Š” ๊ฐ€์žฅ ๋’ค์— ์žˆ๋Š” % ๋ฌธ์ž ๋‹ค์Œ์— ์“ด๋‹ค.
  • %d = ๋ฌธ์ž์—ด ํฌ๋งท ์ฝ”๋“œ

02. ๋ฌธ์ž์—ด ๋ฐ”๋กœ ๋Œ€์ž…

>>> "I eat %s apples." % "five"
'I eat five apples.'
  • ๋ฌธ์ž์—ด ์•ˆ์— ๋˜ ๋‹ค๋ฅธ ๋ฌธ์ž์—ด์„ ์‚ฝ์ž…ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํฌ๋งท ์ฝ”๋“œ๋ฅผ %s๋ฅผ ์“ด๋‹ค.

03. ์ˆซ์ž ๊ฐ’์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ณ€์ˆ˜๋กœ ๋Œ€์ž…

>>> number = 3
>>> "I eat %d apples." % number
'I eat 3 apples.'

 

04. 2๊ฐœ ์ด์ƒ์˜ ๊ฐ’ ๋„ฃ๊ธฐ

>>> number = 10
>>> day = "three"
>>> "I ate %d apples. so I was sick for %s days." % (number, day)
'I ate 10 apples. so I was sick for three days.'
  • 2๊ฐœ ์ด์ƒ์˜ ๊ฐ’์„ ๋„ฃ์œผ๋ ค๋ฉด ๋งˆ์ง€๋ง‰ % ๋‹ค์Œ ๊ด„ํ˜ธ ์•ˆ์— ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ๊ฐ๊ฐ์˜ ๊ฐ’์„ ๋„ฃ์–ด ์ฃผ๋ฉด ๋œ๋‹ค.

๋ฌธ์ž์—ด ํฌ๋งท ์ฝ”๋“œ

์ฝ”๋“œ ์„ค๋ช…
%s ๋ฌธ์ž์—ด(string)
%c ๋ฌธ์ž 1๊ฐœ(character)
%d ์ •์ˆ˜(integer)
%f ๋ถ€๋™์†Œ์ˆ˜ (floating-point)
%o 8์ง„์ˆ˜
%x 16์ง„์ˆ˜
%% Literal % (๋ฌธ์ž % ์ž์ฒด)

 

๋ฌธ์ž์—ด ํฌ๋งท ์ฝ”๋“œ์ธ %d์™€ %๊ฐ€ ๊ฐ™์€ ๋ฌธ์ž์—ด ์•ˆ์— ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ, %๋ฅผ ๋‚˜ํƒ€๋‚ด๋ ค๋ฉด ๋ฐ˜๋“œ์‹œ %%๋ฅผ ์จ์•ผ ํ•œ๋‹ค

>>> "Error is %d%%." % 98
'Error is 98%.'

ํฌ๋งท ์ฝ”๋“œ์™€ ์ˆซ์ž ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๊ธฐ

01. ์ •๋ ฌ๊ณผ ๊ณต๋ฐฑ

>>> "%10s" % "hi"
'        hi'

  • %10s๋Š” ์ „์ฒด ๊ธธ์ด๊ฐ€ 10๊ฐœ์ธ ๋ฌธ์ž์—ด ๊ณต๊ฐ„์—์„œ ๋Œ€์ž…๋˜๋Š” ๊ฐ’์„ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ •๋ ฌํ•˜๊ณ  ๊ทธ ์•ž์˜ ๋‚˜๋จธ์ง€๋Š” ๊ณต๋ฐฑ
>>> "%-10sjane." % 'hi'
'hi        jane.'

  • ์™ผ์ชฝ ์ •๋ ฌ์€ %-10s์ด๋‹ค.

02. ์†Œ์ˆ˜์  ํ‘œํ˜„ํ•˜๊ธฐ

  • %0.4f์—์„œ ‘.’๋Š” ์†Œ์ˆ˜์  ํฌ์ธํŠธ, ๊ทธ ๋’ค์˜ ์ˆซ์ž 4๋Š” ์†Œ์ˆ˜์  ๋’ค์— ๋‚˜์˜ฌ ์ˆซ์ž์˜ ๊ฐœ์ˆ˜๋ฅผ ๋งํ•œ๋‹ค. 
  • ์†Œ์ˆ˜์  ํฌ์ธํŠธ ์•ž์˜ ์ˆซ์ž๋Š” ๋ฌธ์ž์—ด์˜ ์ „์ฒด ๊ธธ์ด๋ฅผ ์˜๋ฏธํ•˜๋Š”๋ฐ, %0.4f์—์„œ ์‚ฌ์šฉํ•œ ์ˆซ์ž 0์€ ๊ธธ์ด์— ์ƒ๊ด€ํ•˜์ง€ ์•Š๊ฒ ๋‹ค๋Š” ์˜๋ฏธ
  • %0.4f๋Š” 0์„ ์ƒ๋žตํ•˜์—ฌ %.4f์ฒ˜๋Ÿผ ์‚ฌ์šฉ

format ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ ํฌ๋งคํŒ…

01. ์ˆซ์ž ๋ฐ”๋กœ ๋Œ€์ž…ํ•˜๊ธฐ

>>> "I eat {0} apples".format(3)
'I eat 3 apples'

02. ๋ฌธ์ž์—ด ๋ฐ”๋กœ ๋Œ€์ž…ํ•˜๊ธฐ

>>> "I eat {0} apples".format("five")
'I eat five apples'

03. ์ˆซ์ž ๊ฐ’์„ ๊ฐ€์ง„ ๋ณ€์ˆ˜๋กœ ๋Œ€์ž…ํ•˜๊ธฐ

>>> number = 3
>>> "I eat {0} apples".format(number)
'I eat 3 apples'

04. 2๊ฐœ ์ด์ƒ์˜ ๊ฐ’ ๋„ฃ๊ธฐ

>>> number = 10
>>> day = "three"
>>> "I ate {0} apples. so I was sick for {1} days.".format(number, day)
'I ate 10 apples. so I was sick for three days.'
  • 2๊ฐœ ์ด์ƒ์˜ ๊ฐ’์„ ๋„ฃ์„ ๊ฒฝ์šฐ, ๋ฌธ์ž์—ด์˜ {0}, {1}๊ณผ ๊ฐ™์€ ์ธ๋ฑ์Šค ํ•ญ๋ชฉ์ด formatํ•จ์ˆ˜์˜ ์ž…๋ ฅ๊ฐ’์œผ๋กœ ์ˆœ์„œ์— ๋งž๊ฒŒ ๋ฐ”๋€๋‹ค.

05. ์ด๋ฆ„์œผ๋กœ ๋„ฃ๊ธฐ

>>> "I ate {number} apples. so I was sick for {day} days.".format(number=10, day=3)
'I ate 10 apples. so I was sick for 3 days.'
  • {name} ํ˜•ํƒœ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ, format ํ•จ์ˆ˜์—๋Š” ๋ฐ˜๋“œ์‹œ name=value์™€ ๊ฐ™์€ ํ˜•ํƒœ์˜ ์ž…๋ ฅ๊ฐ’์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.

06. ์ธ๋ฑ์Šค์™€ ์ด๋ฆ„์„ ํ˜ผ์šฉํ•ด์„œ ๋„ฃ๊ธฐ

>>> "I ate {0} apples. so I was sick for {day} days.".format(10, day=3)
'I ate 10 apples. so I was sick for 3 days.'

07. ์™ผ์ชฝ ์ •๋ ฌ

>>> "{0:<10}".format("hi")
'hi        '
  • :<10 ์‚ฌ์šฉํ•˜๋ฉด ์น˜ํ™˜๋˜๋Š” ๋ฌธ์ž์—ด์„ ์™ผ์ชฝ์œผ๋กœ ์ •๋ ฌํ•˜๊ณ  ๋ฌธ์ž์—ด์˜ ์ด์ž๋ฆฟ์ˆ˜๋ฅผ 10์œผ๋กœ ๋งž์ถ˜๋‹ค.

08. ์˜ค๋ฅธ์ชฝ ์ •๋ ฌ

>>> "{0:>10}".format("hi")
'        hi'

09. ๊ฐ€์šด๋ฐ ์ •๋ ฌ

>>> "{0:^10}".format("hi")
'    hi    '
  • :^ = ๊ฐ€์šด๋ฐ ์ •

10. ๊ณต๋ฐฑ ์ฑ„์šฐ๊ธฐ

>>> "{0:=^10}".format("hi")
'====hi===='
>>> "{0:!<10}".format("hi")
'hi!!!!!!!!'
  • ์ •๋ ฌํ•  ๋•Œ ๊ณต๋ฐฑ ๋ฌธ์ž ๋Œ€์‹  ์ง€์ •ํ•œ ๋ฌธ์ž ๊ฐ’์œผ๋กœ ์ฑ„์›Œ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.
  • ์ฑ„์›Œ ๋„ฃ์„ ๋ฌธ์ž ๊ฐ’์€ ์ •๋ ฌ ๋ฌธ์ž < , > , ^ ๋ฐ”๋กœ ์•ž์— ๋„ฃ์–ด์•ผ ํ•œ๋‹ค.

11. ์†Œ์ˆ˜์  ํ‘œํ˜„ํ•˜๊ธฐ

>>> y = 3.42134234
>>> "{0:0.4f}".format(y)
'3.4213'
  • format ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ์†Œ์ˆ˜์ ์„ 4์ž๋ฆฌ๊นŒ์ง€๋งŒ ํ‘œํ˜„

12. { ๋˜๋Š” } ๋ฌธ์ž ํ‘œํ˜„ํ•˜๊ธฐ

>>> "{{ and }}".format()
'{ and }'
  • ๋ฌธ์ž์—ด์„ ํฌ๋งคํŒ…ํ•  ๊ฒฝ์šฐ, {}์™€ ๊ฐ™์€ ์ค‘๊ด„ํ˜ธ ๋ฌธ์ž๋ฅผ ํฌ๋งคํŒ… ๋ฌธ์ž๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ์—๋Š” {{}}์ฒ˜๋Ÿผ 2๊ฐœ๋ฅผ ์—ฐ์†ํ•ด์„œ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

f ๋ฌธ์ž์—ด ํฌ๋งคํŒ…

>>> name = 'ํ™๊ธธ๋™'
>>> age = 30
>>> f'๋‚˜์˜ ์ด๋ฆ„์€ {name}์ž…๋‹ˆ๋‹ค. ๋‚˜์ด๋Š” {age}์ž…๋‹ˆ๋‹ค.'
'๋‚˜์˜ ์ด๋ฆ„์€ ํ™๊ธธ๋™์ž…๋‹ˆ๋‹ค. ๋‚˜์ด๋Š” 30์ž…๋‹ˆ๋‹ค.'
  • ๋ฌธ์ž์—ด ์•ž์— f ์ ‘๋‘์‚ฌ๋ฅผ ๋ถ™์ด๋ฉด f ๋ฌธ์ž์—ด ํฌ๋งคํŒ… ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
>>> age = 30
>>> f'๋‚˜๋Š” ๋‚ด๋…„์ด๋ฉด {age + 1}์‚ด์ด ๋œ๋‹ค.'
'๋‚˜๋Š” ๋‚ด๋…„์ด๋ฉด 31์‚ด์ด ๋œ๋‹ค.'

>>> d = {'name':'ํ™๊ธธ๋™', 'age':30}
>>> f'๋‚˜์˜ ์ด๋ฆ„์€ {d["name"]}์ž…๋‹ˆ๋‹ค. ๋‚˜์ด๋Š” {d["age"]}์ž…๋‹ˆ๋‹ค.'
'๋‚˜์˜ ์ด๋ฆ„์€ ํ™๊ธธ๋™์ž…๋‹ˆ๋‹ค. ๋‚˜์ด๋Š” 30์ž…๋‹ˆ๋‹ค.'
>>> f'{"hi":<10}'  # ์™ผ์ชฝ ์ •๋ ฌ
'hi        '
>>> f'{"hi":>10}'  # ์˜ค๋ฅธ์ชฝ ์ •๋ ฌ
'        hi'
>>> f'{"hi":^10}'  # ๊ฐ€์šด๋ฐ ์ •๋ ฌ
'    hi    '

>>> f'{"hi":=^10}'  # ๊ฐ€์šด๋ฐ ์ •๋ ฌํ•˜๊ณ  '=' ๋ฌธ์ž๋กœ ๊ณต๋ฐฑ ์ฑ„์šฐ๊ธฐ
'====hi===='
>>> f'{"hi":!<10}'  # ์™ผ์ชฝ ์ •๋ ฌํ•˜๊ณ  '!' ๋ฌธ์ž๋กœ ๊ณต๋ฐฑ ์ฑ„์šฐ๊ธฐ
'hi!!!!!!!!'
>>> y = 3.42134234
>>> f'{y:0.4f}'  # ์†Œ์ˆ˜์  4์ž๋ฆฌ๊นŒ์ง€๋งŒ ํ‘œํ˜„
'3.4213'
>>> f'{y:10.4f}'  # ์†Œ์ˆ˜์  4์ž๋ฆฌ๊นŒ์ง€ ํ‘œํ˜„ํ•˜๊ณ  ์ด ์ž๋ฆฌ์ˆ˜๋ฅผ 10์œผ๋กœ ๋งž์ถค
'    3.4213'

>>> f'{{ and }}'
'{ and }'

๋ฌธ์ž์—ด ๊ด€๋ จ ํ•จ์ˆ˜๋“ค

‘๋ฌธ์ž์—ด ๋‚ด์žฅ ํ•จ์ˆ˜’๋ผ๊ณ  ํ•œ๋‹ค. ์ด ๋‚ด์žฅ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋ฌธ์ž์—ด ๋ณ€์ˆ˜ ์ด๋ฆ„ ๋’ค์— ‘.’๋ฅผ ๋ถ™์ธ ํ›„ ํ•จ์ˆ˜ ์ด๋ฆ„์„ ์จ ์ฃผ๋ฉด ๋œ๋‹ค.

01. ๋ฌธ์ž ๊ฐœ์ˆ˜ ์„ธ๊ธฐ - count

>>> a = "hobby"
>>> a.count('b')
2

02. ์œ„์น˜ ์•Œ๋ ค ์ฃผ๊ธฐ 1 - find

>>> a = "Python is the best choice"
>>> a.find('b')
14
>>> a.find('k')
-1
  • ๋ฌธ์ž์—ด ์ค‘ ๋ฌธ์ž b๊ฐ€ ์ฒ˜์Œ์œผ๋กœ ๋‚˜์˜จ ์œ„์น˜๋ฅผ ๋ฐ˜ํ™˜
  • ๋งŒ์•ฝ ์ฐพ๋Š” ๋ฌธ์ž๋‚˜ ๋ฌธ์ž์—ด์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด -1์„ ๋ฐ˜ํ™˜

03. ์œ„์น˜ ์•Œ๋ ค ์ฃผ๊ธฐ 2 - index

>>> a = "Life is too short"
>>> a.index('t')
8
>>> a.index('k')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
  • ๋ฌธ์ž์—ด ์ค‘ ๋ฌธ์ž t๊ฐ€ ๋งจ ์ฒ˜์Œ์œผ๋กœ ๋‚˜์˜จ ์œ„์น˜๋ฅผ ๋ฐ˜ํ™˜
  • ๋งŒ์•ฝ ์ฐพ๋Š” ๋ฌธ์ž๋‚˜ ๋ฌธ์ž์—ด์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒ

04. ๋ฌธ์ž์—ด ์‚ฝ์ž… - join

>>> ",".join('abcd')
'a,b,c,d'
>>> ",".join(['a', 'b', 'c', 'd'])
'a,b,c,d'
  • join ํ•จ์ˆ˜๋กœ abcd ๋ฌธ์ž์—ด์˜ ๊ฐ๊ฐ์˜ ๋ฌธ์ž์—ด ์‚ฌ์ด์— ','๋ฅผ ์‚ฝ์ž…

05. ์†Œ๋ฌธ์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ๋ฐ”๊พธ๊ธฐ - upper

>>> a = "hi"
>>> a.upper()
'HI'

06. ๋Œ€๋ฌธ์ž๋ฅผ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊พธ๊ธฐ - lower

>>> a = "HI"
>>> a.lower()
'hi'

07. ์™ผ์ชฝ ๊ณต๋ฐฑ ์ง€์šฐ๊ธฐ - lstrip

>>> a = " hi "
>>> a.lstrip()
'hi '
  • ๋ฌธ์ž์—ด ์ค‘ ๊ฐ€์žฅ ์™ผ์ชฝ์— ์žˆ๋Š” ํ•œ ์นธ ์ด์ƒ์˜ ์—ฐ์†๋œ ๊ณต๋ฐฑ๋“ค์„ ๋ชจ๋‘ ์ œ๊ฑฐ

08. ์˜ค๋ฅธ์ชฝ ๊ณต๋ฐฑ ์ง€์šฐ๊ธฐ - rstrip

>>> a= " hi "
>>> a.rstrip()
' hi'
  • ๋ฌธ์ž์—ด ์ค‘ ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ์— ์žˆ๋Š” ํ•œ ์นธ ์ด์ƒ์˜ ์—ฐ์†๋œ ๊ณต๋ฐฑ๋“ค์„ ์ œ๊ฑฐ

09. ์–‘์ชฝ ๊ณต๋ฐฑ ์ง€์šฐ๊ธฐ - strip

>>> a = " hi "
>>> a.strip()
'hi'
  • ๋ฌธ์ž์—ด ์–‘์ชฝ์— ์žˆ๋Š” ํ•œ ์นธ ์ด์ƒ์˜ ์—ฐ์†๋œ ๊ณต๋ฐฑ์„ ๋ชจ๋‘ ์ œ

10. ๋ฌธ์ž์—ด ๋ฐ”๊พธ๊ธฐ - replace

>>> a = "Life is too short"
>>> a.replace("Life", "Your leg")
'Your leg is too short'
  • replace ํ•จ์ˆ˜๋Š” replace(๋ฐ”๋€” ๋ฌธ์ž์—ด, ๋ฐ”๊ฟ€ ๋ฌธ์ž์—ด)์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์ž์—ด ์•ˆ์˜ ํŠน์ •ํ•œ ๊ฐ’์„ ๋‹ค๋ฅธ ๊ฐ’์œผ๋กœ ์น˜ํ™˜

11. ๋ฌธ์ž์—ด ๋‚˜๋ˆ„๊ธฐ - split

>>> a = "Life is too short"
>>> a.split()
['Life', 'is', 'too', 'short']
>>> b = "a:b:c:d"
>>> b.split(':')
['a', 'b', 'c', 'd']
  • ๊ด„ํ˜ธ ์•ˆ์— ์•„๋ฌด ๊ฐ’๋„ ๋„ฃ์–ด ์ฃผ์ง€ ์•Š์œผ๋ฉด ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด์„ ๋‚˜๋ˆ„์–ด ์ค€๋‹ค.
  • ๋งŒ์•ฝ ๊ด„ํ˜ธ ์•ˆ์— ํŠน์ • ๊ฐ’์ด ์žˆ์„ ๊ฒฝ์šฐ์—๋Š” ๊ด„ํ˜ธ ์•ˆ์˜ ๊ตฌ๋ถ„์ž๋กœ ํ•ด์„œ ๋ฌธ์ž์—ด์„ ๋‚˜๋ˆ„์–ด ์ค€๋‹ค.

! ๋ฌธ์ž์—ด ํ•จ์ˆ˜๋Š” ๋ฌธ์ž์—ด ์ž์ฒด์˜ ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋ณ€๊ฒฝ๋œ ๊ฐ’์„ ๋ฆฌํ„ดํ•œ๋‹ค !

 

728x90

[๋ฌธ์ œ]


[๋ฌธ์ œ ํ’€์ด]

  • ์ด ๋ฌธ์ œ๋Š” ์‚ฌ์ง„์˜ ์œ„์น˜ ์ •๋ณด๋ฅผ ์•Œ์•„๋‚ด๋Š” ๋ฌธ์ œ๋กœ Forensically ํˆด์„ ์ด์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
  • Geo Tags ์˜ต์…˜์„ ์ด์šฉํ•˜๋ฉด ๋ฐ”๋กœ ์œ„์น˜ ์ •๋ณด๋ฅผ ์•Œ์•„๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!
728x90

[๋ฌธ์ œ]


[๋ฌธ์ œ ํ’€์ด]

  • ๋ณด๋‹ค์‹œํ”ผ ๋ฌธ์ œ์˜ ์ด๋ฏธ์ง€๊ฐ€ ์ œ๋Œ€๋กœ ๋ณด์ด์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๊ทธ๋ž˜์„œ, HxD ํ”„๋กœ๊ทธ๋žจ์„ ์ด์šฉํ•ด์„œ ๋ถ„์„ํ•ด ๋ณด๋‹ˆ header์— JF๋’ค์— IF ํ—ค๋”๊ฐ€ ๋น ์ ธ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ๊ทธ๋ž˜์„œ ํ—ค๋”์— 49 46์„ ์ถ”๊ฐ€๋ฅผ ํ•ด์ค€ ๋’ค ์ €์žฅ์„ ํ–ˆ์Šต๋‹ˆ๋‹ค.
  • ๊ทธ ๊ฒฐ๊ณผ, ํ”Œ๋ž˜๊ทธ๋ฅผ ํš๋“ํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค!

FLAG: ewha{i_love_E_CooK1e}

728x90

[๋ฌธ์ œ]


[๋ฌธ์ œ ํ’€์ด]

  • ์ฒ˜์Œ์—๋Š” ๋จผ์ € ์ด๋ฏธ์ง€๋ฅผ ๋ถ„์„ํ–ˆ์ง€๋งŒ ํ”Œ๋ž˜๊ทธ๋ฅผ ํš๋“ํ•  ์ˆ˜ ์—†์–ด์„œ Steganography Online ํˆด์„ ์ด์šฉํ•ด์„œ ์ด๋ฏธ์ง€๋ฅผ decode ํ•˜๋‹ˆ ์ˆจ๊ฒจ์ง„ ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐœ๊ฒฌํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

 

FLAG: 7her3_IS_@1W4Y$_MOR3_th@N_MEEtS_thE_Eye

 

728x90

[๋ฌธ์ œ]


[๋ฌธ์ œ ํ’€์ด]

 

  • ์ด๋ฒˆ์—๋Š” Image Steganography ํˆด์„ ์ด์šฉํ•ด์„œ ์ด๋ฏธ์ง€๋ฅผ ๋ถ„์„ํ–ˆ๊ณ  ๋ฐ”๋กœ ํ”Œ๋ž˜๊ทธ๋ฅผ ํš๋“ํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

FLAG: ewha{Gr33n1886}

728x90

[๋ฌธ์ œ]


[๋ฌธ์ œ ํ’€์ด]

 

  • ๋จผ์ € ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ํˆด์„ ์ด์šฉํ•ด์„œ ์ด๋ฏธ์ง€๋ฅผ ๋ถ„์„ํ•˜๊ณ  String Extraction๋„ ํ•ด๋ดค์ง€๋งŒ ํ”Œ๋ž˜๊ทธ๋ฅผ ์–ป์„ ์ˆ˜ ์—†์—ˆ์Šต๋‹ˆ๋‹ค.
  • ๊ทธ๋ž˜์„œ, HxD ํ”„๋กœ๊ทธ๋žจ์„ ์ด์šฉํ•ด์„œ png ํŒŒ์ผ์„ ๋ถ„์„ํ•ด ๋ณด๋‹ˆ Header์™€ Footer๊ฐ€ ์ž˜ ์žˆ์ง€๋งŒ footer ๋’ค์— ๋ฌธ์ž์—ด์ด ๋” ์žˆ๋Š” ๊ฑธ ๋ณด๊ณ  ๊ทธ ๋ถ€๋ถ„์„ ๋”ฐ๋กœ ๋นผ์„œ moon.zip ํŒŒ์ผ๋กœ ์ €์žฅ์„ ํ•ด์คฌ์Šต๋‹ˆ๋‹ค.

  • ์••์ถ•ํ•ด์ œ๋ฅผ ํ•˜๋ ค๊ณ  ํ•˜๋‹ˆ ์•”ํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜๋ผ๊ณ  ํ•ด์„œ ๋น„๋ฒˆ์„ ์ฐพ์•„์•ผ ํ–ˆ๋‚˜ ํ–ˆ๋Š”๋ฐ ๋น„๋ฒˆ์€ moon์ด์—ˆ์Šต๋‹ˆ๋‹ค..
  • ์••์ถ•ํ•ด์ œ๊นŒ์ง€ ์™„๋ฃŒํ•˜๋‹ˆ ํ”Œ๋ž˜๊ทธ ํ…์ŠคํŠธ ํŒŒ์ผ์„ ์–ป์„ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

FLAG: sun{0kay_it_is_a_m00n}

 

+ Recent posts