Python/Python Basic Lesson
Python Programming Basic - 5. 문자열 메소드
Pydole
2020. 3. 2. 20:35
str 클래스 메서드 목록 보기
dir(str)
----------------------------------------------------------------------------------------
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__',
'__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__',
'__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__',
'__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__',
'__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize',
'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format',
'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier',
'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust',
'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust',
'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase',
'title', 'translate', 'upper', 'zfill']
capitalize() : 첫 문자를 대문자로, 나머지 문자는 소문자로 변경
s = 'this is pyTHon'
s.capitalize()
'This is python'
count(self, x, __start, __end) : 지정된 키워드가 몇번이나 포함되어 있는지
s = 'this is pyTHon'
s.count('p') # p가 몇번 포함되는지 검색
1
s = 'this is pyTHon'
print(s.count('is', 0, 4)) # 슬라이싱 이용 0 ~ 3 까지 검색
1
s.count('is', 0, 10) # 슬라이싱 이용 0 ~ 9 까지 검색
2
find(self, sub, __start, __end) : 지정된 문자나 문자열이 위치하는 첫번째 인덱스를 반환
s = 'this is pyTHon'
s.find('i') # 'i' 문자열의 첫번째 인덱스
2
s = 'this is pyTHon'
s.find('TH') # 'TH' 문자열의 첫 번째 인덱스 위치
10
s = 'this is pyTHon'
s.find('i', 3) # 3번 인덱스 이후부터 'i' 문자열의 첫번째 인덱스
5
s = 'this is pyTHon'
s.find('s', 4, 7) # 4 ~ 6번 인덱스까지 's' 문자열의 첫번째 인덱스
6
s = 'this is pyTHon'
s.find('s', 4, 6) # 4 ~ 5번 인덱스까지 's' 문자열의 첫번째 인덱스
-1
문자나 문자열이 없으면 -1을 반환한다.
isalnum() : 알파벳과 숫자로 이루어져 있으면 True
s = 'pyTHon'
s.isalnum()
True
s = 'pyTHon0'
s.isalnum()
True
s = 'pyTHon??'
s.isalnum()
False
isalpha() : 알파벳으로만 이루어져 있으면 True
s = 'pyTHon'
s.isalpha()
True
s = 'pyTHon0'
s.isalpha()
False
문자열에 있는 모든 알파벳을 대문자로 변환
'abcDE'.upper()
'ABCDE'
문자열에 있는 모든 알파벳을 소문자로 변환
'abcDE'.lower()
'abcde'
join(self, iterable) : 문자열 연결
s = 'python'
'.'.join(s) # 문자열 사이에 '.' 삽입
p.y.t.h.o.n
s = ('this','is','python')
'.'.join(s)) # 튜플이나 리스트 등 (iterable)
this.is.python
split(self, sep) : 문자열을 sep로 분리하고, 리스트 타입으로 리턴
s = 'this is python'
s.split() # 공백으로 분리
['this', 'is', 'python']
s = 'this,is,python'
s.split(',') # 콤마로 분리
['this', 'is', 'python']
s = 'this is python'
s.split(' ',1) # sep로 count 만큼만 분리
['this is', 'python']
parttion(self, sep) : 문자열을 sep로 나눔
s = 'this a python'
s.partition('a') # 결과값은 튜플로 반환
('this ', 'a', ' python')
replace(self, old, new, count) : old를 new로 대체(변환)
s = 'this is python'
s.replace('is', 'si') # 모든 'is' 를 'si' 로 변경
thsi si python
s = 'this is python'
s.replace('is', 'si', 1) # is' 를 'si' 로 count 수만큼만 변경
thsi is python