[Python-basic][Data type] 문자열 (string)
문자열 (string)
파이썬에서, 문자열은 문자들의 sequence이다.
파이썬에서 문자열은 immutable이다.
문자열 만들기
’’ 또는 “ “ 을 이용하여 문자열을 만들 수 있다.
>>> '안녕하세요'
'안녕하세요'
>>> "만나서 반가워요"
"만나서 반가워요"
” “ 안에 ‘‘을 넣을 수도, ‘’ 안에 “ “을 넣을 수도 있다.
some_string = '그가 이렇게 말했다. "안녕하세요"'
print(some_string)
그가 이렇게 말했다. "안녕하세요"
’’’ ‘’’, “”” “"”도 문자열로 만들 수 있다. 주로 여러 줄의 문자열을 만들 때 쓰인다.
some_string = '''이것도 가능'''
print(some_string)
이것도 가능
other_string = """동해물과 백두산이
마르고 닳도록
하느님이 보우하사
우리나라 만세"""
print(other_string)
동해물과 백두산이
마르고 닳도록
하느님이 보우하사
우리나라 만세
문자열에 쓰일 수 있는 기능들
다른 자료형을 문자열 자료형으로 변환하고자 한다면 str() 함수를 이용하면 된다.
-
이스케이프 \ (/와 비슷한데 반대로 뒤집힌 문자)
기호 의미 \n 개행 \t 탭 \’ 또는 \” ’ 또는 “ 자체를 문자열 안에 써서 그대로 출력하고자 할 때 쓰임 \ 백슬래시 ‘\’ 자체를 출력하고자 할 때
문자열과 문자열을 서로 이을 때 +기호를 사용한다. 이는 문자열이 할당된 변수에서도 적용 가능하다.
print("안녕", "하세요")
안녕 하세요
위 예제와 같이 print()함수 내에서 문자열들이 인수로 대입된 경우 자동으로 사이에 빈 칸을 삽입한다
‘*’ 연산자를 이용하여 문자열을 반복시켜 출력시킬 수 있다.
a = 'oh' * 4
b = 'wow' * 2
print(a)
print(b)
ohohohoh
wowwow
문자열에서 문자 추출하기
문자열에서 하나의 문자를 추출하려면 [] 안에 offset을 입력한다. offset은 0부터 시작한다.
letters = 'abcdefghijklmnopqrstuvwxyz'
print(letters[0])
print(letters[3])
print(letters[-1])
print(letters[-4])
a
d
z
w
-1은 문자열의 맨 마지막을 지정한다.
문자열의 길이보다 더 큰 수를 입력하면 오류가 발생한다.
문자열은 immutable이기에, 문자열에 다른 문자를 삽입하거나 변경할 수 없다.
name = "james'
name[0] = "J"
print(name)
예외가 발생했습니다. TypeError
'str' object does not support item assignment File "C:\python2\python_basic\study.py", line 2, in name[0] = 'J'
대신 replace()와 같이 문자열 함수들을 적절히 활용해야 한다.
name = 'james'
name = name.replace('j', "J")
print(name)
James
슬라이스(slice) [start : end : step]
- [:] 시작부터 끝까지 모든 문자열을 추출한다.
- [start : ] 특정 start offset부터 끝까지를 지정한다.
- [ : end] 시작부터 특정 end offset - 1까지를 지정한다.
- [start : end] 특정 start offset부터 특정 end offset - 1까지를 지정한다.
- [start : end : step] start부터 end - 1까지 step만큼의 간격으로 추출한다
letters = 'abcdefghijklmnopqrstuvwxyz'
print(letters[:])
print(letters[2:])
print(letters[:3])
print(letters[4:7])
print(letters[::3])
abcdefghijklmnopqrstuvwxyz
cdefghijklmnopqrstuvwxyz
abc
efg
adgjmpsvy
print(letters[::-1])
zyxwvutsrqponmlkjihgfedcba
슬라이싱은 그 offset이 문자열의 길이보다 커도 오류를 발생시키진 않는다.
letters = 'abcdefghijklmnopqrstuvwxyz'
print(letters[50:])
print(letters[:50])
print(letters[100:102])
abcdefghijklmnopqrstuvwxyz
문자열 관련 함수들
- len() 문자열 길이 계산
- split() 인수로 들어가는 separator를 기준으로 문자열을 나눠 list로 표현
some_string = '물을 붓고, 불을 킨
후, 물이 끓으면, 면과 스프를 넣으세요'
print(some_string.split(','))
['물을 붓고', ' 불을 킨 후', ' 물이 끓으면', '면과 스프를 넣으세요']
- string.join(list) : list 내 인수들을 string 문자로 결합. split()의 반대 기능
some_list = ['hello', 'my dogs', 'his cats']
print(','.join(some_list))
hello,my dogs,his cats
- 그 외 문자열 관련 함수들
some_sentence = '''직사광선 및 습기를 피해 서늘한 곳에 진열, 유통 중 변질품은
구입상점 및 본사에서 항상 교환 및 소비자기본법에 의한 피해 보상'''
print("'직사광선'이란 단어로 시작하나요? ", some_sentence.startswith('직사광선'))
print("'미국산'이란 단어로 끝나나요? ", some_sentence.endswith('미국산'))
find_word = '피해'
print("처음 나타나는 '{}' 단어의 위치는 몇? {}".format(find_word, some_sentence.find(find_word)))
print("마지막에 나타나는 '{}' 단어의 위치는 몇? {}".format(find_word, some_sentence.rfind(find_word)))
print("문장 중 '{}' 단어의 개수는? {}".format(find_word, some_sentence.count(find_word)))
print("문장의 모두가 영어 알파벳 또는 숫자로만 이루어져 있나요?", some_sentence.isalnum())
'직사광선'이란 단어로 시작하나요? True
'미국산'이란 단어로 끝나나요? False
처음 나타나는 '피해' 단어의 위치는 몇? 11
마지막에 나타나는 '피해' 단어의 위치는 몇? 66
문장 중 '피해' 단어의 개수는? 2
문장의 모두가 영어 알파벳 또는 숫자로만 이루어져 있나요? False
some_sentence = "it will be honored to be with you..."
print('strip:', some_sentence.strip('.')) # 문장의 양 쪽 끝에 있는 해당 문자열을 삭제
print('capitalize:', some_sentence.capitalize()) # 첫 단어의 첫 글자 대문자화
print('title:', some_sentence.title()) # 모든 단어의 첫 글자 대문자화
print('upper:', some_sentence.upper()) # 모든 문자들을 대문자화
print('lower:', some_sentence.lower()) # 모든 문자들을 소문자화
new_sentence = some_sentence.title()
print('swapcase:', new_sentence.swapcase()) # 대문자는 소문자로, 소문자는 대문자로
size = 60
print("'{}'".format(some_sentence.center(size)))
# 문자열을 60칸 만큼의 빈 공간에 넣었을 때 해당 문자열을 가운데 정렬
print("'{}'".format(some_sentence.ljust(size))) # 왼쪽 정렬
print("'{}'".format(some_sentence.rjust(size))) # 오른쪽 정렬
strip: it will be honored to be with you
capitalize: It will be honored to be with you...
title: It Will Be Honored To Be With You...
upper: IT WILL BE HONORED TO BE WITH YOU...
lower: it will be honored to be with you...
swapcase: iT wILL bE hONORED tO bE wITH yOU...
' it will be honored to be with you... '
'it will be honored to be with you... '
' it will be honored to be with you...'
some_sentence = "you are watching youtube and you just realized you are wasting your youth."
print(some_sentence)
print(some_sentence.replace('you', 'we')) # (원래 부분 문자열, 바꿀 문자열, 바꿀 갯수(선택))
print(some_sentence.replace('you', 'we', 3)) # 문자열 내의 'you'라는 단어들 중 처음 3개만 바꿈
you are watching youtube and you just realized you are wasting your youth.
we are watching wetube and we just realized we are wasting wer weth.
we are watching wetube and we just realized you are wasting your youth.
참고로 replace의 세 번째 인수를 생략하면 문자열 내 모든 ‘원래 부분 문자열’들을 ‘바꿀 문자열’들로 바꾼다. 결과창의 두 번째 줄을 보면 ‘you’라는 단어가 모두 ‘we’라는 단어로 바뀐 것을 확인할 수 있다.
Reference
[1] Bill Lubannovic, “Introducing Python”, (O’REILLY, 2015)
This content is licensed under
CC BY-NC 4.0
댓글남기기