๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Python

[Python] ๋ฌธ์ž์—ด ์ž๋ฃŒํ˜•

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 2. 5.
๋ฐ˜์‘ํ˜•

๋”ฐ์˜ดํ‘œ๋กœ ๋‘˜๋Ÿฌ์‹ธ์—ฌ ์žˆ์œผ๋ฉด ๋ชจ๋‘ ๋ฌธ์ž์—ด์ด๋ผ๊ณ  ๋ณด๋ฉด ๋œ๋‹ค.

ํŒŒ์ด์ฌ์—์„œ ๋ฌธ์ž์—ด์„ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์€ ์ด 4๊ฐ€์ง€๋‹ค.

 

1. ํฐ ๋”ฐ์˜ดํ‘œ(")๋กœ ์–‘์ชฝ ๋‘˜๋Ÿฌ์‹ธ๊ธฐ
2. ์ž‘์€๋”ฐ์˜ดํ‘œ(')๋กœ ์–‘์ชฝ ๋‘˜๋Ÿฌ์‹ธ๊ธฐ
3. ํฐ๋”ฐ์˜ดํ‘œ 3๊ฐœ๋ฅผ ์—ฐ์†(""")์œผ๋กœ ์จ์„œ ์–‘์ชฝ ๋‘˜๋Ÿฌ์‹ธ๊ธฐ
4. ์ž‘์€๋”ฐ์˜ดํ‘œ 3๊ฐœ๋ฅผ ์—ฐ์†(''')์œผ๋กœ ์จ์„œ ์–‘์ชฝ ๋‘˜๋Ÿฌ์‹ธ๊ธฐ

 

 

-๋ฌธ์ž์—ด์— ์ž‘์€๋”ฐ์˜ดํ‘œ(') ํฌํ•จ์‹œํ‚ค๊ธฐ

Python's favorite food is perl

์ด๋Ÿด๋•Œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฌธ์ž์—ด์„ ํฐ๋”ฐ์˜ดํ‘œ(")๋กœ ๋‘˜๋Ÿฌ์‹ธ์•ผ ํ•œ๋‹ค.

>>> food = "Python's favorite food is perl"
>>> food
"Python's favorite food is perl"

 

 

-๋ฌธ์ž์—ด์— ํฐ๋”ฐ์˜ดํ‘œ(") ํฌํ•จ์‹œํ‚ค๊ธฐ

"Python is very easy." he says.

์ด๋Ÿด ๋•Œ๋Š” ๋ฌธ์ž์—ด์„ ์ž‘์€ ๋”ฐ์˜ดํ‘œ(')๋กœ ๋‘˜๋Ÿฌ์‹ธ๋ฉด ๋œ๋‹ค.

>>> say = '"Python is very easy." he says.'

 

 

-๋ฐฑ์Šฌ๋ž˜์‹œ(\)๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ž‘์€๋”ฐ์˜ดํ‘œ(')์™€ ํฐ๋”ฐ์˜ดํ‘œ(")๋ฅผ ๋ฌธ์ž์—ด์— ํฌํ•จ์‹œํ‚ค๊ธฐ

>>> food = 'Python\'s favorite food is perl'
>>> say = "\"Python is very easy.\" he says."

๋ฐฑ์Šฌ๋ž˜์‹œ(\)๋ฅผ ์ž‘์€๋”ฐ์˜ดํ‘œ(')๋‚˜ ํฐ๋”ฐ์˜ดํ‘œ(") ์•ž์— ์‚ฝ์ž…ํ•˜๋ฉด ๋ฐฑ์Šฌ๋ž˜์‹œ(\) ๋’ค์˜ ์ž‘์€ ๋”ฐ์˜ดํ‘œ(')๋‚˜ ํฐ๋”ฐ์˜ดํ‘œ(")๋Š” ๋ฌธ์ž์—ด์„ ๋‘˜๋Ÿฌ์‹ธ๋Š” ๊ธฐํ˜ธ์˜ ์˜๋ฏธ๊ฐ€ ์•„๋‹ˆ๋ผ ๋ฌธ์ž ('), (") ๊ทธ ์ž์ฒด๋ฅผ ๋œปํ•˜๊ฒŒ ๋œ๋‹ค.

 

 

 

โ–ถ ์—ฌ๋Ÿฌ ์ค„์ธ ๋ฌธ์ž์—ด์„ ๋ณ€์ˆ˜์— ๋Œ€์ž…ํ•˜๊ณ  ์‹ถ์„ ๋•Œ

Life is too short
You need python

 

 

-์ค„์„ ๋ฐ”๊พธ๋Š” ์ด์Šค์ผ€์ดํ”„ ์ฝ”๋“œ '\n' ์‚ฝ์ž…ํ•˜๊ธฐ

>>> multiline = "Life is too short\nYou need python"

ํ•˜์ง€๋งŒ ์ฝ๊ธฐ์— ๋ถˆํŽธํ•˜๊ณ  ์ค„์ด ๊ธธ์–ด์ง€๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค.

 

 

-์—ฐ์†๋œ ์ž‘์€๋”ฐ์˜ดํ‘œ 3๊ฐœ(''') ๋˜๋Š” ํฐ๋”ฐ์˜ดํ‘œ 3๊ฐœ(""") ์‚ฌ์šฉํ•˜๊ธฐ

>>> multiline='''
... Life is too short
... You need python
... '''
>>> multiline="""
... Life is too short
... You need python
... ""

 

 

-์ถœ๋ ฅ ๊ฒฐ๊ณผ

>>> print(multiline)
Life is too short
You need python

 

 

 

โ–ถ ๋ฌธ์ž์—ด ์—ฐ์‚ฐํ•˜๊ธฐ

-๋ฌธ์ž์—ด ๋”ํ•ด์„œ ์—ฐ๊ฒฐํ•˜๊ธฐ(Concatenation)

>>> head = "Python"
>>> tail = " is fun!"
>>> head + tail
'Python is fun!'

 

 

-๋ฌธ์ž์—ด ๊ณฑํ•˜๊ธฐ

>>> a = "python"
>>> a * 2
'pythonpython'

์œ„ ์†Œ์Šค ์ฝ”๋“œ์—์„œ a * 2 ๋ฌธ์žฅ์€ a๋ฅผ ๋‘ ๋ฒˆ ๋ฐ˜๋ณตํ•˜๋ผ๋Š” ๋œป์ด๋‹ค.

 

 

-๋ฌธ์ž์—ด ๊ณฑํ•˜๊ธฐ ์‘์šฉ

# multistring.py

print("=" * 50)
print("My Program")
print("=" * 50)

 

 

-๋ฌธ์ž์—ด ๊ธธ์ด ๊ตฌํ•˜๊ธฐ

>>> a = "Life is too short"
>>> len(a)
17
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€