article thumbnail image
Published 2022. 6. 15. 16:22
728x90

파이썬 기초를  아주 많이 쉽게 간추려서 올려 드리고있습니다. 완전 기초 입문자를 위한 자료입니다.

문자열 자료형

파이썬에서 문자열 객체는 따옴표를 이용하여 생성합니다. 

 

홑따옴표(' … ')  쌍따옴표(" … ")  홑따옴표 세 개(''' … ''')  쌍따옴표 세 개(""" … """)

>>> s ='Python is great!'
>>> s ="Python is great!"
>>> s = '''Python is great!'''
>>> s = """Python is great!"""
>>> print(s)
Python is great!

또한 역슬래쉬(\)를 이용하여 긴 문자열 생성을 할 수 있습니다. 

>>> sentence = 'Python is the \
most popular programming \
language in these days.'
>>> print(sentence)
Python is the most popular programming language in these
days.

문자열 안에 홑따옴표 혹은 쌍따옴표를 쓰고싶을때의 방법도 알려드리겠습니다. 

say
'hello' to mom.
4
>>> a = 'say 'hello' to mom
'
SyntaxError: invalid syntax
>>> b = "say 'hello' to mom
"
>>> c = '''say 'hello' to mom
'''
>>> d = """say 'hello' to mom
"""
>>> print(d)
say 'hello' to mom
>>> s = 'say "hello" to mom
'
>>> s = '''say "hello" to mom
'''
>>> s = """say "hello" to mom
"""
>>> print(s)
say "hello" to mom

s = """say "hello" to '
mom
'"""
>>> print(s)
say "hello" to 'mom'

긴 문장을 쓰고싶을때 !

# letter to Alice
print('''Dear Alice,
How are you?
I am busy to study programming this vacation.
Say hello to your parents.
Sincerely,
Bob''')

 

문자열 객체의 특징

 - immutable하다. 

 - 순서가 있는 자료형으로 인덱싱을 이용할수 있다. 

인덱스에 있는 값들을 불러올수 있는 자료형입니다. 

 

문자열 연결하기 (+) 

문자열 반복하기는 (*) 를 사용합니다

예를들어서  a =hello, a*3 를 쓰면 문자열을 3회 반복하여 출력이 됩니다. 'hellohellohello' 

문자열 길이/포함 관계

문자열 길이 - len() 내장함수입니다. 

subject='programming'

len(subject)

11 (subject의 문자열길이를 나타냅니다. )

문자열 포함 - in, not in 연산자

>>> 'r' in subject
True
>>> 'gram' in subject
True
>>> 'abcd' not in subject
True
더보기

문자열 객체에 사용할 수 있는 메소드 확인방법 . 문자열.메소드() 형태로 사용한다. 

>>> dir(str)

['__add__', '__class__', ……'__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

문자열 메소드 이용하기

 

728x90

'Language > python' 카테고리의 다른 글

numpy -인덱싱 / 슬라이싱  (0) 2022.06.21
numpy - ndarray 데이터 형태 바꿔보기  (0) 2022.06.21
numpy- 기초  (0) 2022.06.21
파이썬 기초-3(입출력문)  (0) 2022.06.15
파이썬 기초 -1  (0) 2022.06.14
복사했습니다!