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
λ°μν