python - string(2)

2019. 12. 22. 01:22·Language/python

문자열 관련 함수들

 

count: 개수


>>> a = "hobby"
>>> a.count('b')
2

find: 문자를 통해 인덱스 위치

 


>>> a = "Python is the best choice"
>>> a.find('b')
14
>>> a.find('k')
-1

 

 

index: 인덱스 위치


>>> a = "Life is too short"
>>> a.index('t')
8
>>> a.index('k')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found

join: 문자열 사이에 해당문자를 삽입


>>> ",".join('abcd') #abcd 문자열의 각각의 문자 사이에 ','를 삽입한다.
'a,b,c,d'

upper: 전부 대문자로


>>> a = "hi"
>>> a.upper()
'HI'

lower: 전부 소문자로


>>> a = "HI"
>>> a.lower()
'hi'

lstrip: 왼쪽 공백 제거


>>> a = " hi "
>>> a.lstrip()
'hi '

rstrip: 오른쪽 공백 제거


>>> a= " hi "
>>> a.rstrip()
' hi'

strip: 양쪽 공백 제거


>>> a = " hi "
>>> a.strip()
'hi'

replace: 문자열 대체


>>> a = "Life is too short"
>>> a.replace("Life", "Your leg")
'Your leg is too short'

split: 공백을 기준으로 문자열 나누기


>>> a = "Life is too short"
>>> a.split()
['Life', 'is', 'too', 'short']
>>> b = "a:b:c:d"
>>> b.split(':')
['a', 'b', 'c', 'd']

출처:https://wikidocs.net/13

'Language > python' 카테고리의 다른 글

python - 정규식을 이용한 문자열 검색  (0) 2019.12.22
python - regex(정규표현식)  (0) 2019.12.22
python - string(1)  (0) 2019.12.22
python - lambda  (0) 2019.12.22
python - 내장함수  (0) 2019.12.22
'Language/python' 카테고리의 다른 글
  • python - 정규식을 이용한 문자열 검색
  • python - regex(정규표현식)
  • python - string(1)
  • python - lambda
dev.wookii
dev.wookii
Effort Maketh Happiness
  • dev.wookii
    습관처럼
    dev.wookii
  • 전체
    오늘
    어제
    • 분류 전체보기 (295)
      • Language (35)
        • python (13)
        • C++ (22)
      • Kaggle (4)
      • Algorithms (112)
        • BOJ (58)
        • programmers (43)
        • SWExpertAcademy (2)
      • Certification (38)
        • Adsp (0)
        • Sqld (28)
        • 정처기 (9)
        • 빅데이터 분석기사 (0)
      • Data Analysis & ML (6)
      • 금융 & 디지털 (65)
      • CS (32)
        • DB (2)
        • SE (3)
        • Web&JSP (1)
        • Network (11)
        • OS (2)
        • Linux&Unix (6)
        • Server (1)
        • UX,UI (1)
        • 보안 (5)
      • 기타 (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    programmers
    funny algorithms
    시뮬레이션
    2020 KAKAO
    Ebay korea #coding test
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
dev.wookii
python - string(2)
상단으로

티스토리툴바