Python 문자열 실습

1 개념[ | ]

Python string
파이썬 문자열
  • 파이썬은 문자열을 지원한다.
  • 작은 따옴표와 큰 따옴표 사이에 넣어 처리한다.
  • 따옴표를 표현하기 위해서는 \(역슬래쉬)를 사용한다.
  • 문자열은 출력시 외곽에 작은 따옴표로 감싸며 출력된다.
  • print()를 사용하여 출력시 외곽에 따옴표를 없이 출력하여 쉽게 읽을 수 있다.
  • print()에서 \n를 만나면 다음줄로 넘겨준다.
  • print()에서 r이 추가되어 있으면 \n와 같은 특별한 문자도 있는 그대로 처리 한다.

2 기초실습[ | ]

Joos-MacBook-Pro:~ SuperStar$ python
Python 2.7.10 (default, Jul 14 2015, 19:46:27)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> abc
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'abc' is not defined
>>> 'abc'
'abc'
>>>
>>> '안녕하세요'
'\xec\x95\x88\xeb\x85\x95\xed\x95\x98\xec\x84\xb8\xec\x9a\x94'
>>> print('안녕하세요')
안녕하세요
>>>

3 단순 문자열 처리 예제[ | ]

>>> 'spam eggs'  # 작은 따옴표
'spam eggs'
>>> 'doesn\'t'  # 역슬래쉬로 작은 따옴표를 표현
"doesn't"
>>> "doesn't"  # 큰 따옴표로 역슬래쉬 없이 표현
"doesn't"
>>> '"Yes," he said.'
'"Yes," he said.'
>>> "\"Yes,\" he said."
'"Yes," he said.'
>>> '"Isn\'t," she said.'
'"Isn\'t," she said.'

4 작은 따옴표, 큰 따옴표, print() 함수 예제[ | ]

>>> '"Isn\'t," she said.' # 작은 따옴표로 문자열 처리, 출력 작은 따옴표가 추가 되어 출력
'"Isn\'t," she said.'
>>> print('"Isn\'t," she said.') # 따옴표 없이 출력한다.
"Isn't," she said.
>>> s = 'First line.\nSecond line.'  # \n means newline
>>> s  # without print(), \n is included in the output
'First line.\nSecond line.'
>>> print(s)  # with print(), \n produces a new line
First line.
Second line.

5 r이 추가된 print() 함수 예제[ | ]

>>> print('C:\some\name')  # \n은 다음 줄로 넘어가라는 의미
C:\some
ame
>>> print(r'C:\some\name')  # r은 있는 그대로 표현 하라는 의미
C:\some\name

6 여러줄 문자열 예제[ | ]

>>> print("""\
... Usage: thingy [OPTIONS]
...      -h                        Display this usage message
...      -H hostname               Hostname to connect to
... """)

7 문자열 연결[ | ]

>>>'abc' + 'def'
'abcdef'

8 문자열 반복[ | ]

>>> 3*'abc'
'abcabcabc'
문자열 연결
>>> x = 'Py'
>>> x + 'thon'
'Python'

9 괄호로 긴문장 연결하기[ | ]

>>> text = ('I know one guy '
...         'who knows everything.')
>>> text
'I know one guy who knows everything.'

10 인덱스로 문자 지정[ | ]

>>> word = 'Python'
>>> word[0]  # character in position 0
'P'
>>> word[5]  # character in position 5
'n'
>>> word = 'Python'
>>> word[-1]  # last character
'n'
>>> word[-2]  # second-last character
'o'
>>> word[-6]
'P'

11 문자열 자르기[ | ]

>>> word = 'Python'
>>> word[-1]  # last character
'n'
>>> word[-2]  # second-last character
'o'
>>> word[-6]
'P'
>>> word[0:2]  # characters from position 0 (included) to 2 (excluded)
'Py'
>>> word[2:5]  # characters from position 2 (included) to 5 (excluded)
'tho'
>>> word[:2] + word[2:]
'Python'
>>> word[:4] + word[4:]
'Python'
>>> word[:2]   # character from the beginning to position 2 (excluded)
'Py'
>>> word[4:]   # characters from position 4 (included) to the end
'on'
>>> word[-2:]  # characters from the second-last (included) to the end
'on'

12 문자열 길이[ | ]

>>> s = 'My name is Tom.'
>>> len(s)
15

13 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}