728x90

replace()

  • replace()는 모든 공백을 제거 한다
  • replace(" ", "") 형식으로 사용 하면 공백 제거
a = " H e l l o W o r l d "
print(a)
# " H e l l o W o r l d "

print(a.replace(" ",""))
# "HelloWorld"

 

lstrip(), rstrip(), strip()

  • Python에서 공백 제거를 할 수 있는 함수
  • lstrip() - 왼쪽에 있는 공백 제거
  • rstrip() - 오른쪽에 있는 공백 제거
  • strip() - 왼쪽 오른쪽에 있는 공백 제거
  • 세가지 모두 문자열 사이사이의 공백은 지우지 못한다.
a = "     H e l l o W o r l d    "
print(a)
print(a.lstrip())
print(a.rstrip())
print(a.strip())
# "     H e l l o W o r l d    "
# "H e l l o W o r l d    "
# "     H e l l o W o r l d"
# "H e l l o W o r l d"

 

728x90

'Python' 카테고리의 다른 글

[Python] sort(), sorted() 차이  (0) 2022.04.13
[Python] continue, pass, break 비교  (0) 2022.04.12
[Python] for, range  (0) 2022.04.10
[Python] map 함수  (0) 2022.04.09
[Python] 비트 연산자(Bitwise Operators)  (0) 2022.04.08

+ Recent posts