자료구조를 활용하기 위해서는 메서드를 활용한다.
데이터 구조. 메서드() 형태로 활용한다. 객체의 기능을 사용하는 것이다.
문자열 메서드
print(dir('string'))
dir()으로 문자열 메서드를 확인할 수 있다.
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__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', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
str.find(x)
x의 첫번째 위치 반환, 없으면 -1 반환한다. (오류가 나지 않는다.)
print('apple'.find('p')) #1
print('apple'.find('b')) #-1
apple이라는 문자열에 p가 인덱스 1에 있으므로 1이 반환된다.
b는 없기 때문에 -1이 반환된다.
str.index(x)
x의 첫번째 위치 반환, 없으면 오류 발생
print('apple'.index('p')) #1
print('apple'.index('b')) #ValueError
apple에 b가 없기때문에 오류가 발생한다.
find, index 차이는 존재하지 않을 때 -1을 반환하는지 오류를 발생하는지에 대한 차이이다.
str.isalpha()
문자 여부
print('abc'.isalpha()) #True
print('ㄱㄴㄷ'.isalpha()) #True
알파벳인지 확인하는 것으로 여기서 알파벳은 유니코드 letter으로 영어뿐만아니라 한글을 포함한다.
숫자인지 문자인지를 구분하는 것이다.
문자만 있으면 True를 반환한다.
str.isupper() / str.islower()
대문자로만 이루어져있는지, 소문자로만 이루어져있는지
print('abc'.islower()) #True
print('abc'.isupper()) #False
str.istitle()
타이틀 형식 여부
print('Hello world'.istitle()) #True
타이틀은 처음만 대문자, 마지막은 소문자를 말한다.
str.replace(old, new[,count])
old를 new로 변환한다.
print('hello'.replece('l','k')) #hekko
print('hello'.replace('l','k',1)) #heklo
count 값은 몇개를 수정할지 결정한다.
str.split()
문자열을 구분자로 쪼개어 리스트에 저장된다. 구분자 기본값은 공백이다.
print('a,b,c'.split(',')) #['a','b','c']
print('a b c'.split()) #['a','b','c']
str.strip([chars])
[]안의 구분자를 기분으로 공백이나 특정 문자를 제거한다.
' naver '.strip() # 'naver'
' na ver '.strip() # 'na ver'
'www.naver.com'.strip('w.cmo') # 'naver'
strip() 기본값은 양쪽 공백을 제거한다.
특정문자들을 넣었을 경우, 양끝에서 그 문자가 없어질 때 까지 제거한다.
str.join([iterable])
반복가능한 iterable 형태의 값을 인자로 가지고 요소들을 구분자로 합쳐 문자열로 반환한다.
print('@'.join('abc')) #a@b@c
print('@'.join(['a','b','c'])) #a@b@c
print(','.join(['a','b','c'])) #a,b,c
str.capitalize()
가장 첫글자를 대문자로 변경한다.
str.title()
첫번째는 대문자, 나머지는 소문자로 변경한다.
isdecimal() ⊂ isdigit() ⊂ isnumeric()
isdecimal() : 10진수
isdigit() : 수
isnumeric() : 숫자로 볼 수 있는것
숫자인지 확인하려면 isdecimal, 소수점까지 확인하려면 isdigit을 사용한다.
'Language > Python' 카테고리의 다른 글
셋(set) 메서드 - add, update, remove, discard (0) | 2022.07.26 |
---|---|
리스트 메서드 - append, extend, insert, remove, pop.... (0) | 2022.07.26 |
할당, 얕은 복사, 깊은 복사 (0) | 2022.07.26 |
파이썬 sort(), sorted() 차이 (0) | 2022.07.26 |
파이썬 - 재귀 함수 (recursive function) (0) | 2022.07.21 |