[Python] 문자열 관련 함수
2022. 3. 14. 11:29ㆍPython/Python 기초 정리
반응형

capitalize()
- 첫글자를 대문자로 만들어준다
v_str = "niceman nice"
print("Capitalize: ", v_str.capitalize())
# >>> Capitalize: Niceman nice
endswitch({문자})
- 마지막 문자를 비교하여 bool값을 리턴한다
v_str = "Orange"
print("endswith?: ", v_str.endswith("s"))
# >>> False
print("endswith?: ", v_str.endswith("ge"))
# >>> True
join([{앞},{뒤}])
- 문자 앞 뒤에 값을 입력한다.
v_str = "Niceman"
print("join str: ", v_str.join(["I'm ", "!"]))
# >> join str: I'm Niceman!
print("join str: ", v_str.join(["I'm ", ""]))
# >> join str: I'm Niceman
replace() / replace({n})
- 매칭되는 전체 문자를 변환한다
- 숫자만큼의 개수만큼의 값만 문자를 변환한다 > replace2
v_str = "this is string example....wow!!! this is really string"
print("replace1: ", v_str.replace('is', 'was'))
# >>> replace1: thwas was string example....wow!!! thwas was really string
print("replace2: ", v_str.replace("is", "was", 3))
# >>> replace2: thwas was string example....wow!!! thwas is really string
split('{s}')
- 지정된 문자열로 나누에 list형태로 리턴한다
v_str = "Kim Lee Park Joo"
print("split: ", v_str.split(' '))
# >>> split: ['Kim', 'Lee', 'Park', 'Joo']
sorted
- 문자열을 list형태로 만들여 정렬하여 리턴한다
v_str = "Niceman"
print("sorted: ", sorted(v_str))
# >>> sorted: ['a', 'c', 'e', 'i', 'm', 'n', 'n']
reversed
- 문자열을 거꾸로 뒤집어 리턴한다
- reversed object로 반환하므로 list로 사용하려면 형변환이 필요하다
v_str = "Orange"
print("reversed1: ", reversed(v_str))
# >>> <reversed object at 0x7fe7f8057f70>
# >>> 형 변환이 필요
print("reversed2: ", list(reversed(v_str)))
# >>> ['e', 'g', 'n', 'a', 'r', 'O']
반응형
'Python > Python 기초 정리' 카테고리의 다른 글
[Python] 문자열 관련 함수 : 문자 포함 관련 함수 (0) | 2022.03.19 |
---|---|
[Python] 문자열 관련 함수 : 문자열 계산 관련 (0) | 2022.03.19 |
[Python] 문자열 관련 내용 정리 (0) | 2022.03.14 |
[Python] 데이터 타입 정리 : string, bool, int, float, list, dict, set, tuple (0) | 2022.03.14 |
[Python] 기초 문법 정리 (0) | 2022.03.14 |