Development/Python
[Python] delete characters in string : strip, lstrip, rstrip
오도원공육사
2020. 4. 24. 23:42
반응형
1. strip
2. lstrip
3. rstrip
1. strip([charset])
문자열의 양끝에서 제거한다. charset을 지정하지 않으면 공백문자를 제거한다. charset을 지정하면 string에 양 끝에서 모든 조합을 제거한다.
test_string = "\t python \t"
print('[{}]'.format(test_string))
test_string = test_string.strip()
print('[{}]'.format(test_string))
print()
양옆의 공백을 제거했다.
test_string = "\t>>>>>><<<<<<>>>>hello, python!<<<<<<<<><><>>>< \t"
print('[{}]'.format(test_string))
test_string = test_string.strip('<>\t ')
print('[{}]'.format(test_string))
print()
지정한 charset을 보면 '<', '>', '\t', ' ' 4개를 지정했다. 즉, 해당 4개의 문자에 해당되면 전부 제거한다.
2. lstrip([charset])
strip()함수를 왼쪽에만 적용한다고 생각하면 된다.
# lstrip([charset])
test_string = "\t>>>>>><<<<<<>>>>hello, python!<<<<<<<<><><>>>< \t"
print('[{}]'.format(test_string))
test_string = test_string.lstrip('<>\t ')
print('[{}]'.format(test_string))
print()
3. rstrip([charset])
strip()함수를 오른쪽에만 적용한다.
# rstrip([charset])
test_string = "\t>>>>>><<<<<<>>>>hello, python!<<<<<<<<><><>>>< \t"
print('[{}]'.format(test_string))
test_string = test_string.rstrip('<>\t ')
print('[{}]'.format(test_string))
print()
반응형