string 자료형 문장부호 없애는 방법 string.puctuation

2021. 7. 9. 16:55Python

string 자료형에서 ' , . ! ? 등 문장을 읽기 쉽게 나타낼 때

import string

data ='Let`s go to lunch!'
result = data.translate(str.maketrans('','',string.punctuation))

print(result)

# Lets go to lunch

# 다른방법
punct = string.punctuation
for c in punct:
    data = data.replace(c, "")
    
print(data)
# Lets go to lunch

  출처: https://www.delftstack.com/ko/howto/python/how-to-strip-punctuation-from-a-string-in-python/#%25ED%258C%258C%25EC%259D%25B4%25EC%258D%25AC%25EC%2597%2590%25EC%2584%259C-%25EB%25AC%25B8%25EC%259E%2590%25EC%2597%25B4%25EC%2597%2590%25EC%2584%259C-%25EA%25B5%25AC%25EB%2591%2590%25EC%25A0%2590%25EC%259D%2584-%25EC%25A0%259C%25EA%25B1%25B0%25ED%2595%2598%25EB%25A0%25A4%25EB%25A9%25B4string.punctuation%25EC%259D%2584-%25EC%2582%25AC%25EC%259A%25A9%25ED%2595%2598%25EC%258B%25AD%25EC%258B%259C%25EC%2598%25A4

728x90
반응형