3주자 파이썬 크롤링숙제 개발일지

김현진·2022년 2월 7일

개발일지

목록 보기
1/3
  • replace(old, new, [count]) -> replace("찾을값", "바꿀값", [바꿀횟수])

  • 공백(white space) 제거

strip()에 인자를 전달하지 않음. 인자를 전달하지 않으면 문자열에서 공백을 제거.

text = ' Water boils at 100 degrees '
print('[' + text.rstrip() + ']')
print('[' + text.lstrip() + ']')
print('[' + text.strip() + ']')

결과
[ Water boils at 100 degrees][Water boils at 100 degrees ]
[Water boils at 100 degrees]

  • 동일한 문자 제거

인자로 문자 1개를 전달하면 그 문자와 동일한 것을 모두 제거. 동일하지 않은 문자가 나올 때까지 제거.
text = '0000000Water boils at 100 degrees 000'
print(text.lstrip('0'))
print(text.rstrip('0'))
print(text.strip('0'))

결과
Water boils at 100 degrees 000
0000000Water boils at 100 degrees
Water boils at 100 degrees

  • 여러 문자 제거

인자로 여러 문자를 전달하면 그 문자들과 동일한 것들을 모두 제거. 동일하지 않은 문자가 나올 때까지 제거.
text = ",,,,,123.....water....pp"
print(text.lstrip(',123.p'))
print(text.rstrip(',123.p'))
print(text.strip(',123.p'))

결과
water....pp
,,,,,123.....water
water

0개의 댓글