memostack
article thumbnail
블로그를 이전하였습니다. 2023년 11월부터 https://bluemiv.tistory.com/에서 블로그를 운영하려고 합니다. 앞으로 해당 블로그의 댓글은 읽지 못할 수 도 있으니 양해바랍니다.
반응형

문자와 문자열

파이썬에서는 문자와 문자열을 따로 구분하지 않는다.

문자와 문자열을 사용할 때는 홑 따옴표(') 또는 쌍 따옴표 (") 둘 다 사용 가능하다.

 

문자

위에서 말했듯이 문자와 문자열을 따로 구분하지 않지만,

  • 굳이 문자를 표현하면 문자 한 글자를 따옴표로 감싼다
alphabet_a = 'a'
print(alphabet_a)  # a

alphabet_b = "b"
print(alphabet_b)  # b

print('c')  # c

print("d")  # d
  • 홑 따옴표(')와 쌍 따옴표(") 둘 다 사용 가능하다

 

문자열

여러 개의 문자를 따옴표로 감싼다.

마찬가지로 홑 따옴표(')와 쌍 따옴표(") 둘 다 사용 가능하다

greet = "Hello, Python!"
print(greet)

language = 'python'
print(language)

 

만약 홑 따옴표(')를 문자열에 넣고 싶으면, 문자열을 쌍 따옴표(")로 감싸고,

반대로 쌍 따옴표(")를 문자열에 넣고 싶으면, 문자열을 홑 따옴표(')로 감싼다.

print("Hello 'Python'!")
print('Hello "Python"!')
Hello 'Python'!
Hello "Python"!

 

다른 프로그래밍 언어와 마찬가지로 이스케이프(escape) 문자를 사용할 때는 백 슬래시(\)를 사용한다.

그래서 개행 문자를 사용할 때는 \n을 사용한다.

print("apple\nbanana\ncat\ndog")
apple
banana
cat
dog

 

그리고, 파이썬에는 쌍 따옴표를 3개(""")를 사용하여 문자열을 만드는 방법도 있는데

개행 문자를 사용하지 않고 엔터를 누르면, 개행되어 문자열이 만들어진다.

lorem = """Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting,
remaining essentially unchanged."""

print(lorem)
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting,
remaining essentially unchanged.

 

 

다른 글

2021/01/14 - [Language/Python] - 파이썬(Python)의 특징 정리

 

파이썬(Python)의 특징 정리

파이썬의 특징 문법이 쉽고 간결하여, 비교적 쉽게 배울 수 있음 상대적으로 쉽다는 뜻으로 프로그래밍을 처음 접하는 사람한테는 어려울 수 도 있음 foo = 1 print(foo) # 1 foo = "Hello Python" print(foo) #.

memostack.tistory.com

2020/11/26 - [Language/Python] - Python 사칙연산을 통한 실수형 데이터 다루기 (+, -, *, **, /, //)

 

Python 사칙연산을 통한 실수형 데이터 다루기 (+, -, *, **, /, //)

더하기 연산 + num1 과 num2 라는 변수를 생성하여, 2개의 변수를 더한 값을 print() 함수로 출력한다 수학에서 사용하는 더하기( + )와 동일하다 num1 = 10 num2 = 6 print(num1 + num2) # 16 빼기 연산 - num1..

memostack.tistory.com

 

반응형
블로그를 이전하였습니다. 2023년 11월부터 https://bluemiv.tistory.com/에서 블로그를 운영하려고 합니다. 앞으로 해당 블로그의 댓글은 읽지 못할 수 도 있으니 양해바랍니다.
profile

memostack

@bluemiv_mm

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!