파이썬 문자열 & 한글관련

http://biohackers.net/wiki/PythonAndHangul

 

capitalize()
첫 문자를 대문자로, 나머지 문자를 소문자로 바꿔준다.

1 "PYTHON is powerful".capitalize()
2 'Python is powerful'

count(parameter, [start, [end]])
parameter가 몇 번 포함되어 있는지 알려준다.

1 "python is powerful".count('p')
2 2

encode([encoding, [error]])
str class는 기본적으로 모두 유니코드이다.

1 "가나다".encode('utf-8')
2 b'\xea\xbo\x80\xeb\x82\x98\xeb\x8b\xa4'

endswith(postfix, [start,[end]])
postfix로 문자열이 끝나면 True를 반환하고, 그 외의 경우에는 False를 반환

1 "python.py".endswith('.py')
2 True

postfix에는 tuple로도 사용가능

1 "python.py".endswith(('.txt','.py'))
2 True

expandtabs([tabsize])
tab을 공백으로 치환합니다. default tabsize는 8.

1 "python\tis\tpowerful".expandtabs()
2 'python        is        powerful'
3 "python\tis\tpowerful".expandtabs(1)
4 'python is powerful'

find(keyword, [start, [end]])
문자열 keyword가 나타나는 첫 번째 인덱스를 반환한다.

1 "python is powerful".find('p')
2 0
3 "python is powerful".find('pa')
4 -1

isalnum()
알파벳과 숫자로 이루어져 있으면 True, 그 이외의 것은 False

1 "python3".isalnum()
2 True

join(sequence)
iterate 가능한 입력인 sequence형 변수를 지정된 문자열로 연결하여 반환합니다.

1 list = ["python", "is", "powerful"]
2 ",".join(str(s) for s in list)
3 'python,is,powerful'

lstrip([chars])
문자열의 왼쪽을 잘라냅니다.

1 "#python".lstrip("#")
2 'python'

replace(old, new, [count])
old를 new로 대체한 결과를 반환합니다. count를 입력인자로 준 경우에는 count 만큼의 횟수만 대체한다.

1 "python is powerful".replace("p", "P")
2 'Python is Powerful'

rstrip([chars])
문자열의 오른쪽을 잘라냅니다. chars가 지정되지 않으면 공백문자를 제거

1 "python.py".rstrip(".py")
2 'python'

split([separator, [maxsplit]])
문자열을 separator로 분리한다.

1 "python is powerful".split()
2 ['python', 'is', 'powerful']

startswith(prefix, [start, [end]])
prefix로 문자열이 시작하면 True를 반환하고 그 외의 경우에는 False를 반환한다.

1 "python is powerful".startswith('py')
2 True

strip([chars])
문자열의 양쪽 끝을 잘라낸다.

1 "    python    ".strip()
2 'python'
출처 : http://snowple.tistory.com/271

Leave a Reply

Your email address will not be published.