[Python] 문자열 관련 함수 : 문자열 계산 관련
2022. 3. 19. 12:15ㆍPython/Python 기초 정리
반응형
문자열 계산 관련 메서드
len()
- 문자열의 길이를 반환한다
s = 'asdfg'
print(len(s))
결과
13
min() / max()
- 문자열 내 문자, 혹은 숫자의 최솟값/최댓값
- 문자와 숫자가 함께 있으면 문자가 숫자보다 큰 값으로 인식한다
a = 'asdf'
print("min('asdf') >> ", min(a))
print("max('asdf') >> ", max(a))
b = '82917'
print("min('82917') >> ", min(b))
print("max('82917') >> ", max(b))
c = 'asd54cv231'
print("min('asd54cv231') >> ", min(c))
print("max('asd54cv231') >> ", max(c))
d = 82917 # 숫자형은 TypeError: 'int' object is not iterable 에러를 발생시킨다
print("min(82917) >> ", min(d))
print("max(82917) >> ", max(d))
결과
min('asdf') >> a
max('asdf') >> s
min('82917') >> 1
max('82917') >> 9
min('asd54cv231') >> 1
max('asd54cv231') >> v
count()
- 문자열 내에 입력한 문자열이 몇 개 들어있는지 개수를 반환한다
- begin, end 위치 설정이 가능하다
- {문자열}.count("s", [begin:int, [end:int]])
- 아무것도 입력하지 않으면 에러를 발생시킨다
a = "Hello, Welcome to the Python World"
# t의 개수를 반환한다
print("a.count('t') >> ", a.count('t'))
# 0~16번째 문자 중 t의 개수를 반환한다
print("a.count('t', 16) >> ", a.count('t', 16))
# 7~16번째 문자 중 o의 개수를 반환한다
print("a.count('o', 0, 16) >> ", a.count('o', 7, 16))
# to 문자열을 개수를 반환한다
print("a.count('to') >> ", a.count('to'))
# 문자열을 입력하지 않으면 TypeError: count() takes at least 1 argument (0 given) 에러 빌셍
print("a.count() >> ", a.count())
결과
a.count('t') >> 3
a.count('t', 16) >> 2
a.count('o', 0, 16) >> 1
a.count('to') >> 1
Traceback (most recent call last):
File "/Users/minkyuseo/Documents/Project/crudtest/t.py", line 7, in <module>
print("a.count() >> ", a.count()) # to 문자열을 개수를 반환한다
TypeError: count() takes at least 1 argument (0 given)
종료 코드 1(으)로 완료된 프로세스
반응형
'Python > Python 기초 정리' 카테고리의 다른 글
[Python] 문자열 관련 함수 : 숫자 포함 관련 함수 (0) | 2022.03.19 |
---|---|
[Python] 문자열 관련 함수 : 문자 포함 관련 함수 (0) | 2022.03.19 |
[Python] 문자열 관련 함수 (0) | 2022.03.14 |
[Python] 문자열 관련 내용 정리 (0) | 2022.03.14 |
[Python] 데이터 타입 정리 : string, bool, int, float, list, dict, set, tuple (0) | 2022.03.14 |