• upper() – 대문자로 변경
    • “Hello World”.upper() = “HELLO WORLD”
  • lower() – 소문자로 변경
    • “Hello World”.lower() = “hello world”
  • swapcase() – 대문자는 소문자로, 소문자는 대문자로 변경
    • “Hello World”.swapcase() = “hELLO wORLD”
  • capitalize() – 첫 글자만 대문자로 변경
    • “hello WORLD”.capitalize() = “Hello world”
  • title() – 단어 별 첫 글자만 대문자로 변경
    • “hello WORLD”.title() = “Hello World”
  • count(char [, start, end]) – 문자열 index가 start이상 end미만인 위치에 char가 몇 개 있는지 반환
    • “hello world”.count(‘l’) = 3
    • “heLLo world”.count(‘l’) = 1
    • “hello world”.count(‘l’,3) = 2
    • “hello world”.count(‘l’,3,9) = 1
  • find(char[, start, end]) – 문자열 index가 start이상 end미만인 위치에 char의 인덱스를 반환
    • 내부에 char가 여러 개인 경우 첫 인덱스를 반환
    • char가 없으면 -1 반환
    • “hello world”.find(‘rl’) = 8
    • “hello world”.find(‘rl’,3) = 8
    • “hello world”.find(‘rl’,3,9) = -1
  • index(char[, start, end]) – 기본 사용법은 find와 같음
    • 리스트와 튜플에서도 사용 가능
    • 내부에 char가 없으면 ValueError발생
    • (“hello”, “world”).index(“world”) = 1
  • rfind(char[, start, end]) – 문자열 index가 start이상 end미만인 위치에 char의 인덱스를 반환
    • 내부에 char가 여러 개인 경우 마지막 인덱스를 반환
    • char가 없으면 -1 반환
    • “hello world”.rfind(‘o’) = 7
    • “hello world”.rfind(‘o’,8) = -1
    • “hello world”.rfind(‘o’,3,7) = 4
  • center(length [,fillChar] ) – length 길이에서 중앙 정렬. fillChar로 나머지 부분 채움(생략 시 공백)
    • length가 문자열보다 작은 숫자일 때, 문자열을 그대로 반환한다.
    • 좌 우 fillChar 갯수가 다른 경우, 오른쪽이 하나 더 많게 반환한다.
    • “hello world”.center(16,’*’) = “**hello world***”
  • strip() – 문자열 양쪽의 공백을 제거한다.
    • ” hello world “.strip() = “hello world”
  • rstrip() – 문자열 우측의 공백을 제거한다.
    • ” hello world “.rstrip() = ” hello world”
  • lstrip() – 문자열 좌측의 공백을 제거한다.
    • ” hello world “.lstrip() = “hello world “
  • replace(old, new [,count] ) – old를 new로 변경. 앞에서부터 count갯수 만큼
    • “su su su super nova”.replace(‘su’,”*”) = “* * * *per nova”
    • “su su su super nova”.replace(‘su’,”*”,2) = “* * su super nova”
  • split(sep=None, maxsplit=-1) – 문자열을 sep기준으로 나누어 배열로 반환. 분할 횟수는 maxsplit이며 해당 옵션이 -1인 경우 모두 분할한다.
    • sep이 None인 경우 공백(스페이스, 탭, 줄 바꿈)을 기준으로 분할한다.
    • sep으로 지정한 문자(문자열)는 사라진다.
    • “hello world”.split() = [“hello”, “world”]
    • “hello world hello world hello world”.split(‘hello’,2) = [”, ‘ world ‘, ‘ world hello world’]

Posted in

댓글 남기기

호랑이는 죽어서 가죽을 남기고, 사람은 죽어서 블로그를 남긴다.에서 더 알아보기

지금 구독하여 계속 읽고 전체 아카이브에 액세스하세요.

계속 읽기