https://docs.python.org/3/tutorial/introduction.html

문자

대화형 모드에서는 마지막에 출력된 값이 ”_“에 저장된다.

문자 이스케이프는 ”\“(백슬래시)를 사용하여 한다.

문자열이 정의한것과 다르게 출력이 될수 있다.
이 print()함수는 따옴표를 생략하고 이스케이프된 문자와 특수 문자를 출력하여 읽기 쉽게 출력된다.

s = 'First line.\nSecond line.'  # \n means newline
s  # without print(), special characters are included in the string
 
print(s)  # with print(), special characters are interpreted, so \n produces new line

백슬러시 (”\”) 를 이스케이프 문자로 안쓰려면 문자열 따옴표나 쌍따옴표 앞에 r을 붙인다.
단, r을 붙여도 백슬러시가 끝으로 끝날수는 없다. (끝에는 이스케이프 문자로 해석됨)

you can use raw strings by adding an r before the first quote:

print(r'C:\some\name')  # note the r before the quote

문자열 기능

# 3 times 'un', followed by 'ium'
3 * 'un' + 'ium'
 
'Py' 'thon'
# Python 자동으로 붙음 (단, 변수나 수식이 들어간 iteral은 안되고 문자 문자만)
 
#변수는 +를 쓰면 된다.
prefix + 'thon'
 
prefix = 'Py'
prefix 'thon'  # can't concatenate a variable and a string literal
  File "<stdin>", line 1
    prefix 'thon'
           ^^^^^^
SyntaxError: invalid syntax
('un' * 3) 'ium'
  File "<stdin>", line 1
    ('un' * 3) 'ium'
               ^^^^^
SyntaxError: invalid syntax

문자열은 리스트 처럼 쓸수 있지만 값은 불변(immutable)이다.

리스트

python에서 리스트를 변수에 할당하면 해당 변수는 기존 리스트 를 참조

rgb = ["Red", "Green", "Blue"]
rgba = rgb
id(rgb) == id(rgba)  # they reference the same object
 
rgba.append("Alph")
rgb

리스트의 slice를 사용한 할당은 얕은 복사본(swallow copy)을 만든다.

correct_rgba = rgba[:]
correct_rgba[-1] = "Alpha"
correct_rgba
 
rgba

← python 3.14으로