๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • ์žฅ์›์ต ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ
๐Ÿคท๐Ÿผ‍โ™€๏ธ Etc.../- C, C++, Python, Android

Python ๋ฌธ์ž์—ด ์กฐ์ž‘

by Wonit 2019. 12. 2.

๋ฌธ์ž์—ด์˜ ์กฐ์ž‘

๋ฌธ์ž์—ด์€ ์—ฌ๋Ÿฌ ์—ฐ์‚ฐ์ž์— ์˜ํ•ด์„œ ์กฐ์ž‘๋  ์ˆ˜ ์žˆ๋Š”๋ฐ ํฌ๊ฒŒ ๋‹ค์Œ๊ณผ ๊ฐ™์€ 9๊ฐ€์ง€๋กœ ๋‚˜๋‰œ๋‹ค.

  • ๊ฒฐํ•ฉ
  • ๋ณต์ œ
  • ๋ฌธ์ž ์ถ”์ถœ
  • ์Šฌ๋ผ์ด์Šค
  • ๋ฌธ์ž์—ด ๊ธธ์ด ์„ธ๊ธฐ
  • ๋ฌธ์ž์—ด ๋‚˜๋ˆ„๊ธฐ
  • ๋ฌธ์ž์—ด ๊ฒฐํ•ฉ
  • ๋ฌธ์ž์—ด์˜ ๋Œ€๋ฌธ์ž ์†Œ๋ฌธ์ž
    ๋กœ ๋‚˜๋‰œ๋‹ค. ํ•˜๋‚˜์”ฉ ํ•˜๋‚˜์”ฉ ์ฒœ์ฒœํžˆ ์ •๋ณตํ•ด๋ณด์ž


๊ฒฐํ•ฉ : +

+ ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•˜๋ฉด ๋ฆฌํ„ฐ๋Ÿด ๋ฌธ์ž์—ด ๋˜๋Š” ๋ฌธ์ž์—ด ๋ณ€์ˆ˜๋ฅผ ๊ฒฐํ•ฉ ํ•  ์ˆ˜ ์žˆ๋‹ค.

>>> 'Hello' + 'world'
'Hello world'

๋ณต์ œ : *

* ๋ฌธ์ž๋ฅผ ์ด์šฉํ•œ๋‹ค๋ฉด ๋ฌธ์ž์—ด์„ ๋ณต์ œํ•  ์ˆ˜ ์žˆ๋‹ค.

>>> str = 'Hello '
>>> new_str = str * 4
'Hello Hello Hello Hello '

๋ฌธ์ž ์ถ”์ถœ : []

๋ฌธ์ž์—ด์—์„œ ํ•œ ๋ฌธ์ž๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด ๋Œ€๊ด„ํ˜ธ[]์˜ ์˜คํ”„์…‹์„ ์ง€์ •ํ•œ๋‹ค. ์˜คํ”„์…‹์€ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋ฉฐ ๊ฐ€์žฅ ๋ ์˜คํ”„์…‹์€ ์Œ์ˆ˜ -1์ด๋‹ค

>>> my_name = 'Jang won ik'
>>> my_name[0]
'J'
>>> My_name[-1]
'k'

์Šฌ๋ผ์ด์Šค : [start:end:step]

์Šฌ๋ผ์ด์Šค๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ•œ ๋ฌธ์ž์—ด์—์„œ ๋ฌธ์ž์—ด ์ผ๋ถ€๋ฅผ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ๋‹ค. []๋Œ€๊ด„ํ˜ธ๋ฅผ ์ด์šฉํ•˜์—ฌ




start : ์‹œ์ž‘ ์˜คํ”„์…‹ end : ๋ ์˜คํ”„์…‹ step : ์˜ต์…˜์œผ๋กœ ์Šฌ๋ผ์ด์Šค ์ •์˜


๋ผ๋Š” ๊ธฐ๋Šฅ์„ ํ•˜๋Š”๋ฐ ํŠน์ • ๊ธฐ๋Šฅ์„ ์œ„ํ•œ ๋ฐฉ๋ฒ•์€

  • [:] : ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ์ „์ฒด ์‹œํ€€์Šค๋ฅผ ์ถ”์ถœ
  • `[start:] : start์˜คํ”„์…‹๋ถ€ํ„ฐ ๋๊นŒ์ง€ ์‹œํ€€์Šค ์ถ”์ถœ
  • [:end] : ์ฒ˜์Œ๋ถ€ํ„ฐ (end - 1) ์˜คํ”„์…‹๊นŒ์ง€ ์ถ”์ถœ
  • `[start:end] : start ์˜คํ”„์…‹๋ถ€ํ„ฐ (end - 1)์˜คํ”„์…‹๊นŒ์ง€ ์ถ”์ถœ
  • `[start:end:step] : step๋งŒํผ ๋ฌธ์ž๋ฅผ ๊ฑด๋„ˆ ๋›ฐ๋ฉด์„œ, start์˜คํ”„์…‹๋ถ€ํ„ฐ (end -1) ์˜คํ”„์…‹๊นŒ์ง€ ์‹œํ€€์Šค ์ถ”์ถœ
์‹คํ–‰ ์˜ˆ์ œ
>>> str[:]
'hello'
>>> str[2:]
'llo'
>>> str[1:3]
'el'
>>> str[-1]
'o'
>>> str[1:4:1]
'ell'
>>>

๋ฌธ์ž์—ด ๊ธธ์ด : len(_)

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

>>> len('hello')
5

๋ฌธ์ž์—ด ๋‚˜๋ˆ„๊ธฐ : split(_)

์ด ํ•จ์ˆ˜๋Š” ๊ตฌ๋ถ„์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด์„ ์ž‘์€ ๋ฌธ์ž์—ด๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋กœ ๋‚˜๋ˆ„๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.


๊ตฌ๋ถ„์ž๋Š” ๋ฌธ์ž์—ด์„ ๋‚˜๋ˆ„๊ธฐ ์œ„ํ•œ ๊ตฌ๋ถ„ ๋ฌธ์ž๋กœ ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค

>>> name_list = 'jang,won,ik''
>>> name_list.split(',')
['jang', 'won', 'ik']

ํŠน์ • ๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๋Š”๊ฐ€? : startswith(_)

>>> my_name = 'jang won ik'
>>> my_name.startswith('jang')
True

ํŠน์ • ๋ฌธ์ž๋กœ ๋๋‚˜๋Š”๊ฐ€? : endwith(_)

>>> my_name = 'jang won ik'
>>> my_name.endwith('ik')
True

ํŠน์ • ๋ฌธ์ž๊ฐ€ ์žˆ๋Š” ์˜คํ”„์…‹์€? : find(_)

>>> my_name = 'jang won ik'
>>> my_name.find('won')
5

ํŠน์ • ๋ฌธ์ž๊ฐ€ ๋งˆ์ง€๋ง‰์œผ๋กœ ๋‚˜์˜ค๋Š” ์˜คํ”„์…‹์€? : rfind(_)

>>> my_name = 'jang won ik'
>>> my_name.rfind('won')
5

ํŠน์ • ๋ฌธ์ž๊ฐ€ ๋ช‡ ๋ฒˆ ๋‚˜์˜ค๋Š”๊ฐ€? : count(_)

>>> my_name = 'jang won ik'
>>> my_name.count('n')
2

๊ธ€์ž์™€ ์ˆซ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋Š”๊ฐ€? : isalnum()

>>> my_name = 'jang won ik'
>>> my_name.istalnum()
False

์–‘ ๋์—์„œ. ์‹œํ€€์Šค ์‚ญ์ œ : strip()

>>> my_name = 'jang won ik.'
>>> my_name.strip(' ')
'jang won ik'

์ฒซ ๋ฒˆ์งธ ๋‹จ์–ด๋ฅผ ๋Œ€๋ฌธ์ž๋กœ : capitalize()

>>> my_name = 'jang won ik'
>>> my_name.capitalize()
'Jang won ik'

๋ชจ๋“  ๋‹จ์–ด์˜ ์ฒซ ๊ธ€์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ : title()

>>> my_name = 'jang won ik'
>>> my_name.title()
'Jang Won Ik'

๋ชจ๋“  ๊ธ€์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ : upper()

>>> my_name = 'jang won ik'
>>> my_name.upper()
'JANG WON IK'

๊ธ€์ž๋ฅผ ๋ชจ๋‘ ์†Œ๋ฌธ์ž๋กœ : lower()

>>> my_name = 'jang won ik'
>>> my_name.
'jang won ik'

๋Œ€๋ฌธ์ž๋Š” ์†Œ๋ฌธ์ž๋กœ, ์†Œ๋ฌธ์ž๋Š” ๋Œ€๋ฌธ์ž๋กœ : swapcase()

>>> my_name = 'jang won ik'
>>> my_name.
'JANG WON IK'

๋Œ“๊ธ€