ActivePython 에 따라오는 Tutorial 을 요약했다. 내 기억을 돕는 수준의 요약이니 다른이가 봐도 모를걸.
String 의 표현
대강의 스트링 사용과, escape sequence 의 사용을 알수 있다.
>>> '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.'
\n 이 C 와 같이 newline 으로 쓰이고 있고, \ 를 이용해서 문장이 계속된다고 인터프리터에 알릴수 있다.
hello = "This is a rather long string containing\n\ several lines of text just as you would do in C.\n\ Note that whitespace at the beginning of the line is\ significant." >>>print hello This is a rather long string containing several lines of text just as you would do in C. Note that whitespace at the beginning of the line is significant.
이건 아주 맘에 드는 기능인데, raw string 이라는 넘이 있다. 스트링내의 문자들을 데이타로만 인식한다.
hello = r"This is a rather long string containing\n\ several lines of text much as you would do in C." >>>print hello This is a rather long string containing\n\ several lines of text much as you would do in C.
CRLF 여럿을 포함하는 스트링을 쉽게 표현하기 위해서, """ 또는 ''' 로 감쌀수도 있다.
>>>print """ Usage: thingy [OPTIONS] -h Display this usage message -H hostname Hostname to connect to """ Usage: thingy [OPTIONS] -h Display this usage message -H hostname Hostname to connect to
String 의 연산
+ 를 통해 concat 이, * 를 통해 repeat 이 가능하다.
>>> word = 'Help' + 'A' >>> word 'HelpA' >>> '<' + word*5 + '>' '<HelpAHelpAHelpAHelpAHelpA>'
연속된 두개의 string literals 는 concat 된다.
>>> import string >>> 'str' 'ing' # <- This is ok 'string' >>> string.strip('str') + 'ing' # <- This is ok 'string' >>> string.strip('str') 'ing' # <- This is invalid: this only works with two literals, not with arbitrary string expressions File "<stdin>", line 1, in ? string.strip('str') 'ing' ^ SyntaxError: invalid syntax
String 의 indexing 또는 substring
[ ] 를 통해서 indexed 되거나, slice 될 수 있다.
>>> word[4] 'A' >>> word[0:2] 'He' >>> word[2:4] 'lp'
C의 스트링과 달리 Python 의 string1은 값을 바꿀수 없다.
>>> word[0] = 'x' Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: object doesn't support item assignment >>> word[:1] = 'Splat' Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: object doesn't support slice assignment
스트링의 값을 바꾸려면, 새로운2 스트링을 만들어야 한다.
>>> 'x' + word[1:] 'xelpA' >>> 'Splat' + word[4] 'SplatA'
Slice 에서 값을 생략하는 표현이 유용하게 쓰인다.
>>> word[:2] # The first two characters 'He' >>> word[2:] # All but the first two characters 'lpA'
>>> word[:2] + word[2:] 'HelpA' >>> word[:3] + word[3:] 'HelpA'
slice 할때 boundary 를 넘어가는 인자의 경우 python 이 적당히 처리해준다. 에러를 내지 않는다. upper bound 가 lower bound 보다 큰경우엔 empty string 을 돌려준다.
>>> word[1:100] 'elpA' >>> word[10:] '' >>> word[2:1] ''
인자를 음수로 주면 오른쪽부터 카운팅을 의미한다.
>>> word[-1] # The last character 'A' >>> word[-2] # The last-but-one character 'p' >>> word[-2:] # The last two characters 'pA' >>> word[:-2] # All but the last two characters 'Hel'
그러나 -0 은 0 과 같으므로, 오른쪽부터 카운팅되지 않는것에 주의.
>>> word[-0] # (since -0 equals 0) 'H'
Out-of-range negative slice indices are truncated, but don't try this for single-element (non-slice) indices
>>> word[-100:] 'HelpA' >>> word[-10] # error Traceback (most recent call last): File "<stdin>", line 1, in ? IndexError: string index out of range
다음과 같이 이해하면 쉽다.
+---+---+---+---+---+ | H | e | l | p | A | +---+---+---+---+---+ 0 1 2 3 4 5 -5 -4 -3 -2 -1
String 의 길이
built-in function len() 를 쓰면 알 수 있다.
>>> s = 'supercalifragilisticexpialidocious' >>> len(s) 34
