Study/Python

05. Python 문자열

2019. 7. 18. 20:59

1. 문자열 접근

hello = 'Hello, world!'
hello[0]
#'H'출력
hello[-1]
#'!'출력 (뒤에서 첫번째)
hello[len(hello)-1]
#'!'출력 

문자열을 출력하는 것은 가능하지만 hello[0]='a' 같이 할당하는 것은 불가능!

hello = 'hello, world!'
hello[0:12]
#'hello,world'출력
hello[0:13]
#'hello,world! '출력
hello[1:1]
#''출력
hello[1:2]
#'e'출력 
hello[3:-1]
#'lo, world' 출력
hello[:5]
#'hello'출력

2. 문자열 조작

대체하기 replace(바꿀문자열, 새 문자열)

문자열 분리 split(기분문자열)

구분자 문자열와 문자열 리스트(튜플)의 요소를 연결하여 문자열로 만듦 join(리스트)

 

 

반응형