Python ÇÁ·Î±×·¡¹Ö > Python - ³»ÀåÇÔ¼ö (¹®ÀÚ¿ °ü·Ã)
µî·ÏÀÏ : 2017-08-04 17:13
Á¶È¸¼ö : 53,743
³»ÀåÇÔ¼ö´Â Python ÀÌ ±âº»ÀûÀ¸·Î °¡Áö°í ÀÖ´Â ÇÔ¼ö ÀÔ´Ï´Ù.
¿ÜºÎ ¸ðµâÀÇ import ¾øÀÌ »ç¿ëÀÌ °¡´ÉÇÕ´Ï´Ù.
À̹ø Àå¿¡¼´Â ¹®ÀÚ¿°ü·Ã ³»ÀåÇÔ¼ö¸¦ Á¤¸®ÇÒ °èȹÀÔ´Ï´Ù.
1. len()
- ¹®ÀÚ¿ÀÇ ±æÀ̸¦ ±¸ÇÕ´Ï´Ù.
>>> s = "abcdefg"
>>> len(s) // Àüü ¹®ÀÚ¿ ±æÀÌ
°á°ú : 7
2. count()
- ¹®ÀÚ¿Áß Æ¯Á¤¹®ÀÚÀÇ °¹¼ö¸¦ ±¸ÇÕ´Ï´Ù.
>>> s = "abcdefg"
>>> s.count('a') // ¹®ÀÚ¿ Áß 'a'ÀÇ °¹¼ö
°á°ú : 1
3.split()
- ¹®ÀÚ¿À» ƯÁ¤¹®ÀÚ¸¦ ±âÁØÀ¸·Î ³ª´«´Ù.
- ³ª´©¾îÁø ¹®ÀÚ¿Àº ¸®½ºÆ® ÀÚ·áÇüÀ¸·Î ¹ÝȯµÈ´Ù.
>>> str = "test1, test2, test3, test4"
>>> str.split(',')
°á°ú : ['test1', ' test2', ' test3', ' test4']
4. find(), index()
- ƯÁ¤¹®ÀÚÀÇ À§Ä¡¸¦ ¾Ë·ÁÁØ´Ù.
- index() ´Â ã´Â ¹®ÀÚ°¡ ¾øÀ» °æ¿ì error ¸¦ ¹ÝȯÇÑ´Ù.
>>> str = "Python is fun for me."
>>> str.find('i') // ã´Â ¹®ÀÚ¿ÀÌ ¾øÀ¸¸é (-1) ¹ÝȯÇÑ´Ù.
7
>>> str.index('i') // ã´Â ¹®ÀÚ¿ÀÌ ¾øÀ¸¸é ¿¡·¯¸¦ ¹ÝȯÇÑ´Ù.
7
5. join()
- split() ¿Í ¹Ý´ëµÇ´Â ±â´ÉÀ¸·Î ¹®ÀÚ¿À» °áÇÕÇÑ´Ù.
- string.join(list)ÇüÅ·Π°áÇÕÇÑ´Ù.
>>> list = ['test','test1','test2','test3']
>>> ",".join(list)
'test,test1,test2,test3'
6. replace()
- ¹®ÀÚ¿¿¡¼ ƯÁ¤ ¹®Á¦¸¦ ƯÁ¤ ¹®ÀÚ·Î ¹Ù²Û´Ù.
python_define = 'Python is a widely used high-level, general-purpose, interpreted, dynamic programming language.'
# ¹®ÀÚ¿¿¡¼ ,¸¦ and·Î ¸ðµÎ ¹Ù²Ù´Â °æ¿ì
>>> python_define.replace(',', ' and')
'Python is a widely used high-level and general-purpose and interpreted and dynamic programming language.'
# ¹®ÀÚ¿¿¡¼ ,¸¦ and·Î 2¹ø¸¸ ¹Ù²Ù´Â °æ¿ì
>>> python_define.replace(',', ' and',2)
'Python is a widely used high-level and general-purpose and interpreted, dynamic programming language.'
7. startswith() / endswith()
- ½ÃÀ۵Ǵ ´Ü¾î, ³¡³ª´Â ´Ü¾î ¿©ºÎ¸¦ È®ÀÎÇÒ ¼ö ÀÖ´Ù. (True / False) ¹ÝȯÇÑ´Ù.
>>> python_define = 'Python is a widely used high-level, general-purpose, interpreted, dynamic programming language.'
>>> python_define.startswith('Python')
True
>>> python_define.endswith('language.')
True
8. ¹®ÀÚ¿ Àε¦½Ì ¶Ç´Â ÃßÃâ¹æ¹ý
- ¹®ÀÚ¿¿¡¼ Çѹ®ÀÚ¸¦ ¾ò±â À§Çؼ ¹®ÀÚ¿ À̸§µÚ¿¡ ´ë°ýÈ£([ ])·Î, ¹è¿°°ÀÌ offsetÀ» ÁöÁ¤ÇÕ´Ï´Ù.
- ù¹ø°´Â [0] À̸ç, ¸¶Áö¸·Àº [-1]À̸ç, [-2], [-3] ¼ø¼·Î ÁøÇàµË´Ï´Ù.
>>> str = 'abcdefghijklmnopqrstuvwxyz'
>>> str[0]
'a'
>>> str[1]
'b'
>>> str[-1]
'z'
>>> str[-2]
'y'
9. ¹®ÀÚ¿ ½½¶óÀ̽º
¹®ÀÚ¿ ½½¶óÀ̽º´Â [start:end:step] À¸·Î Ç¥ÇöÇÒ ¼ö ÀÖ½À´Ï´Ù.
[:] : óÀ½ºÎÅÍ ³¡±îÁö Àüü ¹®ÀÚ¿À» ÃßÃâÇÕ´Ï´Ù.
[start:] : start ºÎÅÍ ³¡±îÁö ÃßÃâÇÕ´Ï´Ù.
[:end] : óÀ½ºÎÅÍ end-1±îÁö ÃßÃâÇÕ´Ï´Ù.
[start:end] : startºÎÅÍ end-1±îÁö ÃßÃâÇÕ´Ï´Ù.
[start:end:step] : step¸¸²û Á¡ÇÁÇÏ¸é¼ start¿¡¼ end-1±îÁö ÃßÃâÇÕ´Ï´Ù.
>>> alphabet = 'abcdefghijklmnopqrstuvwxyz'
>>> alphabet[:]
'abcdefghijklmnopqrstuvwxyz'
>>> alphabet[20:]
'uvwxyz'
>>> alphabet[:10]
'abcdefghij'
>>> alphabet[12:15]
'mno'
>>> alphabet[-4:]
'wxyz'
>>> alphabet[18:-3]
'stuvw'
>>> alphabet[::7]
'ahov'
>>> alphabet[4:20:3]
'ehknqt'
>>> alphabet[19::4]
'tx'
>>> alphabet[:21:5]
'afkpu'
>>> alphabet[::-1]
'zyxwvutsrqponmlkjihgfedcba'