- python docs: https://docs.python.org/3/library/stdtypes.html#str.strip

문자열의 앞뒤에서 어떤 문자(들)를 지워야할 때 strip 메소드가 유용합니다. lstrip, rstrip은 왼쪽, 오른쪽부터 지우는거니 원리는 strip이랑 같습니다.

str.strip([chars])

Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped:

문자열의 앞, 뒤부터 chars에 해당되는 문자들이 지워진 문자열 복사본을 반환합니다. 매개변수 chars는 지울 문자들입니다. 매개변수를 넣지 않거나 None을 넣으면 공백(whitespace)을 지웁니다.
chars 매개변수는 지울 prefix, suffix가 아니라, 문자열 앞뒤에서 지울 문자들의 집합입니다.
>>> '   spacious   '.strip()
'spacious'
>>> 'www.example.com'.strip('cmowz.')
'example'
>>> comment_string = '#....... Section 3.2.1 Issue #32 .......'
>>> comment_string.strip('.#! ')
'Section 3.2.1 Issue #32'

strip으로 문자열에서 공백을 지우거나 cmowz.중 하나인 문자열을 앞뒤로 계속 지워본 예시입니다.

반응형