파이썬(Python) 따옴표 3개의 부호로 줄을 바꾸는 긴 문자열 만들기
문자들이 나열된 상태를
Strings 문자열이라고 합니다.
파이썬에서 따옴표("...", '...')로 감싼
모든 데이터는 문자 데이터 입니다.
문자열의 테이터 타입은
str(string) 입니다.
파이썬에서 옆으로 한줄 코딩
줄을 바꾸는 긴 문자열을
만들고 싶을때는
작은따옴표(''')나 큰따옴표(""")
3개로 감싸 줍니다.
poem = '''So "it is" quite different, then, if in a mountain town
the mountains are close, rather than far. Here
they are far, their distance away established,
consistent year to year, like a parent’s
or sibling’s. They have their own music.
So I confess I do not know what it’s like,
listening to mountains up close, like a lover,
the silence of them known, not guessed at.'''
print(poem)
>> So "it is" quite different, then, if in a mountain town
the mountains are close, rather than far. Here
they are far, their distance away established,
consistent year to year, like a parent’s
or sibling’s. They have their own music.
So I confess I do not know what it’s like,
listening to mountains up close, like a lover,
the silence of them known, not guessed at.
여기서 잠깐!
긴 문자열은 큰따옴표나 작은따옴표
3개로 감싸면 된다고 했는데~
문자 데이터 속에 큰따옴표(")가 있다면
이때는 큰따옴표로 감싸면 에러가 납니다.
문자 데이터 속에 큰따옴표(") 를
작음따옴표(')로 바꿔주거나
양쪽을 작은따옴표로 감싸줘야 합니다.
예) """이렇게 하면 "에러가 난다는거죠" !!"""
"""이렇게 바꾸면 '에러가 안나죠' !!"""
반대로 문자 데이터 속에
작은 따옴표가 있다면
양 쪽은큰따옴표로 감싸줘야
에러나 안나요.
예) '''이렇게 하면 '에러가 나겠죠?' !!'''
"""이렇게 하면 '에러가 안나요' !!"""
물론 역슬래시 이스케이프 문자
\" 로 구분해주면 따옴표를
아무거나사용해도 되구요.
예) """이렇게 하면 \"에러가 안나죠?\" !!"""
'''이렇게 하면 \'에러가 안나요\' !!'''
힐링아무의 코딩일기 힐코딩!
'Python > Python 기초' 카테고리의 다른 글
파이썬(Python) 문자열을 쪼개주는 split() 함수 (0) | 2022.04.25 |
---|---|
파이썬(Python) 문자열을 대문자와 소문자로 바꿔주는 upper(), lower() 함수 (0) | 2022.04.24 |
파이썬(Python) 실습 - print() 함수, input() (0) | 2022.04.23 |
파이썬(Python) 문자를 숫자로 변경 int() 함수 (0) | 2022.04.23 |
파이썬(Python) 사용자에게 직접 입력을 받는 input() 함수 (0) | 2022.04.21 |
댓글