x =10 #변수 할당, 변수는 할당된 값을 "가리킨다"
x = 10 # int 타입 y = 10.1 #float 타입
print(x)
del x
#가변 타입의 예시 (리스트) my_list = [1, 2, 3] my_list[0] = 100 # 가능 #불변 타입의 예시 (문자열) my_string = "Hello" my_string[0] = 'h' # 불가능
문자열(string)이란 문자, 단어 등으로 구성된 문자들의 집합을 말한다. 예를 들면 다음과 같다.
문자열 선언
s1 = 'abc' #'' 으로 선언 s2 = "def" #"" 으로 선언
문자열 결합(Concatenation)
print(s1+s2) #결과: abcdef
문자열 길이(length)
print(len(s1)) #결과: 3
문자열 인덱싱과 슬라이싱
print(s1[0]) s3 = s1 + s2 # abcdef print(s3[2:]) #결과: a, cdef
a | b | c | d | e | f |
---|---|---|---|---|---|
0 | 1 | 2 | 3 | 4 | 5 |
문자열이든 리스트든 시작은 0부터이다
s1 = 'abc' s2 = 'def' s3 = s1 + s2 print(s3.upper()) 결과: ABCDEF
s1 = 'ABC' s2 = 'DEF' s3 = s1 + s2 print(s3.lower()) 결과: abcdef
s1 = 'abc' s2 = 'def' s3 = s1 + s2 print(s3.replace('b','ABCABC')) 결과: aABCABCcdef
s = "a b c d e f g" r = s.split(sep =' ') print(f's.split() : {r}') print(type(r)) #split() 디플트 값은 split(sep = ' ') 이다 #결과: ['a', 'b', 'c', 'd', 'e', 'f', 'g'] #결과: <class 'list'> split은 list를 반환한다.
s = "a b c d e f g" print(s.find('g')) #결과: 12
s = "a b c d e f g" print(','.join(s)) #결과: a, ,b, ,c, ,d, ,e, ,f, ,g
s = " a b c d e f g " print(s.strip()) #결과: a b c d e f g