Python

[Python] λ¬Έμžμ—΄ μžλ£Œν˜•

μ½”λ”©ν•˜λŠ” λΆ•μ–΄ 2021. 2. 5. 02:20
λ°˜μ‘ν˜•

λ”°μ˜΄ν‘œλ‘œ λ‘˜λŸ¬μ‹Έμ—¬ 있으면 λͺ¨λ‘ λ¬Έμžμ—΄μ΄λΌκ³  보면 λœλ‹€.

νŒŒμ΄μ¬μ—μ„œ λ¬Έμžμ—΄μ„ λ§Œλ“œλŠ” 방법은 총 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
λ°˜μ‘ν˜•