2014년 9월 30일 화요일

python. 문자열


문자열 조작 / list 조작 / split() / join() / strip() / find() / %(forma) 설명..
FIXME: 예제 추가

문자열 조작:
  • a[start:end:shift]
  • repr(): 문법 내에 표현들을 문자열로 표현해줌. 예를들면, 배열이나 숫자, 함수 등. 아무거나 넣으면 error exception

list 조작:
  • documentation의 string section 참고..너무많다.

자르기:
  • split(): 나누기
  • join(): 합치기

삭제:
  • strip(): 원래 문자열에서 공백(arg가 있으면 문자) 제거
  • lstrip(): 원래 문자열에서 왼쪽 여백 제거
  • rstrip(): 원래 문자열에서 오른쪽 여백 제거
--> 여기서 공백/여백은 string.whitespace variable에서 확인 가능함.

치환:
  • replace(): 치환
  • maketrans() / translate():강력한 치환..

검색:
  • find(): 첫 번째 substring의 string에서의 index, 없으면 -1
  • rfind(): reverse find()
  • index() / rindex(): find와 동일하지만 substring이 없으면 -1이 아니라 error return
  • count(): substring이 몇 번 있는지
  • startswith() / endswith(): 시작과 끝을 검색하고 boolean return

format:
  • "xxx{0}yyy{1}".format(":", ";") --> xxx:yyy;
  • 0, 1 등의 정수 대신에 dict 표현도 가능함
  • "%s: test" % "yongk" --> yongk: test
참고: http://lapee79.blogspot.kr/2013/08/python-strings.html

댓글 없음: