긴 문자열을 할당하는 방법
조회수 13009회
발생하는 문제 및 실행환경
엄청 긴 쿼리를 보기 좋게 여러 줄로 나눠서 할당할 때
javascript에서처럼 +
를 쓰거나 백슬래시(\
)를 쓰는 게
파이썬스러운 방법인지 모르겠습니다.
별로 보기 좋지도 않고 따옴표를 계속 쓰는 것도 번거로워요
long_string='some text not important. just garbage to'+
'illustrate my example';
#또는
long_string='some text not important. just garbage to' \
'illustrate my example';
문자열을 여러 줄에 걸쳐 쓰는 가장 파이썬스러운 방법이 뭔가요?
1 답변
-
1. (큰)따옴표 3개를 쓰는 방법
이 방법은 따옴표 안에 있는 모든 코드(개행문자까지 전부)를 다 문자로 취급하니 유의해서 써주세요
s = """\ this is a very long string if I had the energy to type more and more ...""" print s
출력 :
this is a very long string if I had the energy to type more and more ...
2. 괄호로 묶어주는 방법
추가로 공백 문자나 개행 문자가 섞이진 않지만 한 줄 한 줄 따옴표로 묶어줘야 해서 귀찮습니다.
s = ("this is a very" "long string too" "for sure ..." ) print(s)
출력 :
this is a verylong string toofor sure ...
댓글 입력