KR_String - somaz94/python-study GitHub Wiki
๋ฌธ์์ด(String)์ ์ฐ์๋ ๋ฌธ์๋ค์ ๋์ด์ ๋งํ๋ค. ํ์ด์ฌ์์๋ ์์๋ฐ์ดํ(')๋ ํฐ๋ฐ์ดํ(")๋ก ๋๋ฌ์ธ์ธ ๋ฌธ์๋ค์ ์งํฉ์ ์๋ฏธํ๋ค.
"Life is too short, You need Python"
"a"
"123" # ์ซ์๋ ๋ฐ์ดํ๋ก ๋๋ฌ์ธ์ด๋ฉด ๋ฌธ์์ด
# 1. ํฐ๋ฐ์ดํ
str1 = "Hello World"
# 2. ์์๋ฐ์ดํ
str2 = 'Python is fun'
# 3. ํฐ๋ฐ์ดํ 3๊ฐ
str3 = """Life is too short,
You need python"""
# 4. ์์๋ฐ์ดํ 3๊ฐ
str4 = '''Life is too short,
You need python'''
# 1. ์์๋ฐ์ดํ ํฌํจํ๊ธฐ
food = "Python's favorite food is perl"
# 2. ํฐ๋ฐ์ดํ ํฌํจํ๊ธฐ
say = '"Python is very easy." he says.'
# 3. ์ญ์ฌ๋์๋ฅผ ์ฌ์ฉํ์ฌ ํฌํจํ๊ธฐ
food = 'Python\'s favorite food is perl'
say = "\"Python is very easy.\" he says."
โ Tip:
- ๋ฌธ์์ด ์์ ์์๋ฐ์ดํ๊ฐ ์๋ค๋ฉด ํฐ๋ฐ์ดํ๋ก ๋๋ฌ์ธ๊ธฐ
- ๋ฌธ์์ด ์์ ํฐ๋ฐ์ดํ๊ฐ ์๋ค๋ฉด ์์๋ฐ์ดํ๋ก ๋๋ฌ์ธ๊ธฐ
- ์ญ์ฌ๋์(
\
)๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฐ์ดํ๋ฅผ ๋ฌธ์ ๊ทธ๋๋ก ํํ ๊ฐ๋ฅ
# 1. ์ด์ค์ผ์ดํ ์ฝ๋ ์ฌ์ฉ
multiline = "Life is too short\nYou need python"
# 2. ๋ฐ์ดํ 3๊ฐ ์ฌ์ฉ
multiline = """
Life is too short
You need python
"""
์ญ์ฌ๋์(\
)๋ฅผ ์ผ๋ฐ ๋ฌธ์๋ก ์ฒ๋ฆฌํ๊ณ ์ถ์ ๋ ์ฌ์ฉํฉ๋๋ค.
# ์ผ๋ฐ ๋ฌธ์์ด
print("C:\\Users\\user\\Documents") # C:\Users\user\Documents
# ์์ ๋ฌธ์์ด (r ์ ๋์ด ์ฌ์ฉ)
print(r"C:\Users\user\Documents") # C:\Users\user\Documents
# ์ ๊ท์์์ ์ ์ฉํ๊ฒ ์ฌ์ฉ
import re
pattern = r"\bword\b" # ๋จ์ด ๊ฒฝ๊ณ์ ์๋ "word" ์ฐพ๊ธฐ
โ Tip:
- ์์ ๋ฌธ์์ด์
r
์ ๋์ด๋ฅผ ์ฌ์ฉ - ํ์ผ ๊ฒฝ๋ก๋ ์ ๊ท์์์ ์ ์ฉ
- ์ด์ค์ผ์ดํ ์ํ์ค๊ฐ ์ฒ๋ฆฌ๋์ง ์์
์ฝ๋ | ์ค๋ช |
---|---|
\n |
์ค๋ฐ๊ฟ |
\t |
ํญ |
\\ |
๋ฌธ์ \
|
\' |
์์๋ฐ์ดํ |
\" |
ํฐ๋ฐ์ดํ |
\r |
์บ๋ฆฌ์ง ๋ฆฌํด |
\f |
ํผ ํผ๋ |
\a |
๋ฒจ ์๋ฆฌ |
\b |
๋ฐฑ์คํ์ด์ค |
\000 |
๋๋ฌธ์ |
\xhh |
16์ง์ hh์ ํด๋นํ๋ ๋ฌธ์ |
\uhhhh |
16์ง์ hhhh์ ํด๋นํ๋ ์ ๋์ฝ๋ ๋ฌธ์ |
โ Tip:
- ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉ๋๋ ๊ฒ์
\n
,\t
,\\
,\'
,\"
- ๋ฌธ์์ด ๋ด์์ ํน์ํ ๊ธฐ๋ฅ์ ํ๋ ๋ฌธ์๋ค์ ํํํ ๋ ์ฌ์ฉ
-
\x
,\u
๋ ํน์ ์ฝ๋ ํฌ์ธํธ์ ๋ฌธ์๋ฅผ ํํํ ๋ ์ฌ์ฉ
head = "Python"
tail = " is fun!"
head + tail # 'Python is fun!'
a = "python"
a * 2 # 'pythonpython'
# ์์ฉ
print("=" * 50) # 50๊ฐ์ ๋ฑํธ๋ฅผ ์ถ๋ ฅ
print("My Program")
a = "Life is too short"
len(a) # 17 - ๋ฌธ์์ด์ ๊ธธ์ด๋ฅผ ๋ฐํ
# in ์ฐ์ฐ์ ์ฌ์ฉ
print("Python" in "Python is fun") # True
print("Java" in "Python is fun") # False
a = "Life is too short, You need Python"
a[3] # 'e' (์ผ์ชฝ์์ 4๋ฒ์งธ ๋ฌธ์)
a[-1] # 'n' (์ค๋ฅธ์ชฝ์์ ์ฒซ ๋ฒ์งธ ๋ฌธ์)
โ Tip:
- ์ธ๋ฑ์ค๋ 0๋ถํฐ ์์
- ์์ ์ธ๋ฑ์ค๋ ๋ค์์๋ถํฐ ์ ๊ทผ (-1๋ถํฐ ์์)
a = "Life is too short, You need Python"
a[0:4] # 'Life' - 0๋ฒ์งธ๋ถํฐ 4๋ฒ์งธ ๊ธ์ ์ง์ ๊น์ง
a[19:] # 'You need Python' - 19๋ฒ์งธ๋ถํฐ ๋๊น์ง
a[:17] # 'Life is too short' - ์ฒ์๋ถํฐ 17๋ฒ์งธ ๊ธ์ ์ง์ ๊น์ง
a[:] # ์ ์ฒด ๋ฌธ์์ด
a[19:-7] # 'You need' - 19๋ฒ์งธ๋ถํฐ ๋์์ 7๋ฒ์งธ ๊ธ์ ์ง์ ๊น์ง
# ์คํ
(step) ์ฌ์ฉ
a[::2] # 'Lf st hr,Y edPto' - 2์นธ์ฉ ๊ฑด๋๋ฐ๋ฉฐ ์ ํ
a[::-1] # 'nohtyP deen uoY ,trohs oot si efiL' - ๋ฌธ์์ด ๋ค์ง๊ธฐ
โ Tip:
-
a[์์:๋]
ํ์์ผ๋ก ์ฌ์ฉ - ๋ ๋ฒํธ๋ ํฌํจ๋์ง ์์
- ์์ ๋ฒํธ ์๋ต ์ ์ฒ์๋ถํฐ
- ๋ ๋ฒํธ ์๋ต ์ ๋๊น์ง
-
a[์์:๋:์คํ ]
ํ์์ผ๋ก ์คํ (๊ฐ๊ฒฉ) ์ง์ ๊ฐ๋ฅ
์ง์ ์ | ์ค๋ช | ์์ |
---|---|---|
%d | ์ ์(decimal) | print("%d" % 10) โ "10" |
%f | ์ค์(float) | print("%.2f" % 3.14) โ "3.14" |
%s | ๋ฌธ์์ด(string) | print("%s" % "hello") โ "hello" |
%r | repr() ๊ฒฐ๊ณผ | print("%r" % "hello") โ "'hello'" |
%c | ๋ฌธ์ ํ๋ | print("%c" % 'A') โ "A" |
%% | % ๋ฌธ์ ์์ฒด | print("%%") โ "%" |
# ์ซ์ ๋์
"I eat %d apples." % 3 # 'I eat 3 apples.'
# ๋ฌธ์์ด ๋์
"I eat %s apples." % "five" # 'I eat five apples.'
# ์ฌ๋ฌ ๊ฐ ๋์
number = 10
day = "three"
"I ate %d apples. so I was sick for %s days." % (number, day) # 'I ate 10 apples. so I was sick for three days.'
# ์์์ ํํ
"Pi is approximately %.2f" % 3.14159 # 'Pi is approximately 3.14'
# ์ ๋ ฌ๊ณผ ํญ ์ง์
"%10s" % "hi" # ' hi' - ์ฐ์ธก์ ๋ ฌ, 10์นธ ํญ
"%-10s" % "hi" # 'hi ' - ์ข์ธก์ ๋ ฌ, 10์นธ ํญ
# ๊ธฐ๋ณธ ์ฌ์ฉ
"I eat {0} apples".format(3) # 'I eat 3 apples'
# ์ด๋ฆ์ผ๋ก ๋ฃ๊ธฐ
"I ate {number} apples. so I was sick for {day} days.".format(number=10, day=3) # 'I ate 10 apples. so I was sick for three days.'
# ์ธ๋ฑ์ค์ ์ด๋ฆ ํผ์ฉ
"I ate {0} apples. so I was sick for {day} days.".format(10, day=3)
# ์ ๋ ฌ
"{0:<10}".format("hi") # 'hi ' - ์ผ์ชฝ ์ ๋ ฌ
"{0:>10}".format("hi") # ' hi' - ์ค๋ฅธ์ชฝ ์ ๋ ฌ
"{0:^10}".format("hi") # ' hi ' - ๊ฐ์ด๋ฐ ์ ๋ ฌ
# ๊ณต๋ฐฑ ์ฑ์ฐ๊ธฐ
"{0:=^10}".format("hi") # '====hi====' - ๊ฐ์ด๋ฐ ์ ๋ ฌ, = ๋ฌธ์๋ก ์ฑ์ฐ๊ธฐ
# ์์์ ํํ
"{0:.2f}".format(3.14159) # '3.14'
# ์ฒ ๋จ์ ์ฝค๋ง
"{0:,}".format(1000000) # '1,000,000'
# ๊ธฐํ ํ์ ์ง์
"{0:b}".format(42) # '101010' - 2์ง์
"{0:o}".format(42) # '52' - 8์ง์
"{0:x}".format(42) # '2a' - 16์ง์
"{0:e}".format(42.0) # '4.200000e+01' - ์ง์ ํ๊ธฐ๋ฒ
name = 'ํ๊ธธ๋'
age = 30
f'๋์ ์ด๋ฆ์ {name}์
๋๋ค. ๋์ด๋ {age}์
๋๋ค.' # '๋์ ์ด๋ฆ์ ํ๊ธธ๋์
๋๋ค. ๋์ด๋ 30์
๋๋ค.'
# ํํ์ ์ฌ์ฉ
age = 30
f'๋๋ ๋ด๋
์ด๋ฉด {age+1}์ด์ด ๋๋ค.' # '๋๋ ๋ด๋
์ด๋ฉด 31์ด์ด ๋๋ค.'
# ์ ๋ ฌ
f'{"hi":<10}' # 'hi ' - ์ผ์ชฝ ์ ๋ ฌ
f'{"hi":>10}' # ' hi' - ์ค๋ฅธ์ชฝ ์ ๋ ฌ
f'{"hi":^10}' # ' hi ' - ๊ฐ์ด๋ฐ ์ ๋ ฌ
# Python 3.8+ ๋๋ฒ๊น
์ง์
f'{name=}' # "name='ํ๊ธธ๋'" - ๋ณ์๋ช
๊ณผ ๊ฐ ํจ๊ป ์ถ๋ ฅ
# ์์์ ํ์
f'{3.14159:.2f}' # '3.14'
# ๋ ์ง ํ์
import datetime
now = datetime.datetime.now()
f'{now:%Y-%m-%d %H:%M:%S}' # '2023-07-01 12:34:56' - ๋ ์ง ํ์ ์ง์
# ๋ณตํฉ ํ์
f'{"python":!^20}' # '!!!!!!!python!!!!!!!' - ์ฑ์ฐ๊ธฐ, ์ ๋ ฌ, ํญ ์ง์
# ์ฒ ๋จ์ ๊ตฌ๋ถ
f'{1000000:,}' # '1,000,000'
โ Tip:
- Python 3.6 ์ด์์์๋ f-๋ฌธ์์ด ๋ฐฉ์์ ๊ถ์ฅ
- f-๋ฌธ์์ด์ ๋ณ์๋ฅผ ์ง์ ์ฐธ์กฐํ ์ ์์ด ๊ฐ๋ ์ฑ์ด ์ข์
- ํํ์๋ ์ฌ์ฉ ๊ฐ๋ฅ
- Python 3.8๋ถํฐ๋ ๋๋ฒ๊น ์ง์ (๋ณ์๋ช ๊ณผ ๊ฐ ํจ๊ป ์ถ๋ ฅ)
a = "hobby"
a.count('b') # 2 - ๋ฌธ์์ด ๋ด 'b'์ ๊ฐ์ ๋ฐํ
a = "Python is the best choice"
a.find('b') # 14 - 'b'์ ์์น ๋ฐํ, ์์ผ๋ฉด -1 ๋ฐํ
a.index('b') # 14 - 'b'์ ์์น ๋ฐํ, ์์ผ๋ฉด ์ค๋ฅ ๋ฐ์
# ๊ฒ์ ์์ ์์น ์ง์
a.find('t', 3) # 11 - ์ธ๋ฑ์ค 3๋ถํฐ 't'๋ฅผ ์ฐพ์
# ์ค๋ฅธ์ชฝ์์๋ถํฐ ์ฐพ๊ธฐ
a.rfind('t') # 19 - ์ค๋ฅธ์ชฝ์์๋ถํฐ 't' ๊ฒ์
a.rindex('t') # 19 - ์ค๋ฅธ์ชฝ์์๋ถํฐ 't' ๊ฒ์
",".join('abcd') # 'a,b,c,d'
",".join(['a', 'b', 'c', 'd']) # 'a,b,c,d'
# ์ฌ๋ฌ ๋ฌธ์์ด ๊ฒฐํฉ (์ฑ๋ฅ์ ์ด์ )
words = ["Hello", "World", "Python"]
" ".join(words) # 'Hello World Python'
a = "hi"
a.upper() # 'HI' - ๋๋ฌธ์๋ก ๋ณํ
a.lower() # 'hi' - ์๋ฌธ์๋ก ๋ณํ
a.capitalize() # 'Hi' - ์ฒซ ๊ธ์๋ง ๋๋ฌธ์๋ก
a.title() # ๊ฐ ๋จ์ด์ ์ฒซ ๊ธ์๋ฅผ ๋๋ฌธ์๋ก
# ๋์๋ฌธ์ ๋ฐ์
"Hello World".swapcase() # 'hELLO wORLD'
# ๋์๋ฌธ์ ํ์ธ
"ABC".isupper() # True - ๋ชจ๋ ๋๋ฌธ์์ธ์ง ํ์ธ
"abc".islower() # True - ๋ชจ๋ ์๋ฌธ์์ธ์ง ํ์ธ
a = " hi "
a.lstrip() # 'hi ' - ์ผ์ชฝ ๊ณต๋ฐฑ ์ ๊ฑฐ
a.rstrip() # ' hi' - ์ค๋ฅธ์ชฝ ๊ณต๋ฐฑ ์ ๊ฑฐ
a.strip() # 'hi' - ์์ชฝ ๊ณต๋ฐฑ ์ ๊ฑฐ
# ํน์ ๋ฌธ์ ์ ๊ฑฐ
"www.example.com".strip('w.com') # 'example' - ์์ชฝ์์ w, ., c, o, m ๋ฌธ์๋ค ์ ๊ฑฐ
a = "Life is too short"
a.replace("Life", "Your leg") # 'Your leg is too short'
# ์นํ ํ์ ์ ํ
"hello hello hello".replace("hello", "hi", 2) # 'hi hi hello'
a = "Life is too short"
a.split() # ['Life', 'is', 'too', 'short']
b = "a:b:c:d"
b.split(':') # ['a', 'b', 'c', 'd']
# ๋ถํ ๊ฐ์ ์ ํ
"a:b:c:d:e".split(':', 2) # ['a', 'b', 'c:d:e']
# ์ค ๋จ์๋ก ๋ถํ
multiline = "Line1\nLine2\nLine3"
multiline.splitlines() # ['Line1', 'Line2', 'Line3']
# 3๋ถํ (์๋ถ๋ถ, ๊ตฌ๋ถ์, ๋ท๋ถ๋ถ)
"name=value".partition('=') # ('name', '=', 'value')
# ์ํ๋ฒณ ๋๋ ์ซ์ ํ๋จ
"abc123".isalnum() # True - ์ํ๋ฒณ ๋๋ ์ซ์๋ก๋ง ๊ตฌ์ฑ
"abc".isalpha() # True - ์ํ๋ฒณ์ผ๋ก๋ง ๊ตฌ์ฑ
"123".isdigit() # True - ์ซ์๋ก๋ง ๊ตฌ์ฑ
"3.14".isnumeric() # False - ์์์ ์ ์ซ์๋ก ์ธ์ ์๋จ
# ๊ณต๋ฐฑ ํ๋จ
" \t\n".isspace() # True - ๊ณต๋ฐฑ ๋ฌธ์๋ก๋ง ๊ตฌ์ฑ
# ์๋ณ์ ํ๋จ
"valid_name".isidentifier() # True - ์ ํจํ Python ์๋ณ์(๋ณ์๋ช
)
# ์์/๋ ํ๋จ
"Hello".startswith("He") # True - "He"๋ก ์์ํ๋์ง
"Hello".endswith("lo") # True - "lo"๋ก ๋๋๋์ง
# ๋ชจ๋ ํ์ ๊ฐ๋ฅํ ๋ฌธ์์ธ์ง
"Hello!".isprintable() # True - ๋ชจ๋ ์ถ๋ ฅ ๊ฐ๋ฅํ ๋ฌธ์์ธ์ง
# ์ผ์ชฝ ์ ๋ ฌ
"hello".ljust(10) # 'hello ' - ์ผ์ชฝ ์ ๋ ฌ, 10์๋ฆฌ
# ์ค๋ฅธ์ชฝ ์ ๋ ฌ
"hello".rjust(10) # ' hello' - ์ค๋ฅธ์ชฝ ์ ๋ ฌ, 10์๋ฆฌ
# ๊ฐ์ด๋ฐ ์ ๋ ฌ
"hello".center(10) # ' hello ' - ๊ฐ์ด๋ฐ ์ ๋ ฌ, 10์๋ฆฌ
# 0์ผ๋ก ์ฑ์ฐ๊ธฐ (์ซ์ ์ถ๋ ฅ์ฉ)
"42".zfill(5) # '00042' - 0์ผ๋ก ์ฑ์ 5์๋ฆฌ๋ก
# maketrans๋ก ๋ณํ ํ
์ด๋ธ ์์ฑ ํ translate๋ก ๋ณํ
translation_table = str.maketrans('abc', '123')
"abcdef".translate(translation_table) # '123def'
# ์ญ์ ๋ฌธ์ ์ง์
rm_table = str.maketrans('', '', 'aeiou')
"Hello World".translate(rm_table) # 'Hll Wrld' - ๋ชจ์ ์ ๊ฑฐ
โ ์ฃผ์์ฌํญ:
- ๋ฌธ์์ด์ immutable(๋ณ๊ฒฝ ๋ถ๊ฐ๋ฅํ) ์๋ฃํ
- ๋ฌธ์์ด ํจ์๋ค์ ์๋ณธ์ ๋ณ๊ฒฝํ์ง ์๊ณ ์๋ก์ด ๋ฌธ์์ด์ ๋ฐํ
- ์๋ณธ ๋ณ๊ฒฝ์ ์ํด์๋ ๋ฐํ๊ฐ์ ๋ค์ ๋ณ์์ ํ ๋นํด์ผ ํจ
# ์๋ชป๋ ์ฌ์ฉ
a = "hi"
a.upper() # a๋ ์ฌ์ ํ "hi"
# ์ฌ๋ฐ๋ฅธ ์ฌ์ฉ
a = "hi"
a = a.upper() # a๋ ์ด์ "HI"
ํ์ด์ฌ 3์์ ๋ฌธ์์ด์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ ๋์ฝ๋๋ฅผ ์ฌ์ฉํฉ๋๋ค.
# ๋ฌธ์์ด ์ธ์ฝ๋ฉ (str -> bytes)
text = "์๋
ํ์ธ์"
encoded = text.encode('utf-8') # b'\xec\x95\x88\xeb\x85\x95\xed\x95\x98\xec\x84\xb8\xec\x9a\x94'
# ๋ฌธ์์ด ๋์ฝ๋ฉ (bytes -> str)
decoded = encoded.decode('utf-8') # '์๋
ํ์ธ์'
# ๋ค์ํ ์ธ์ฝ๋ฉ ์ฌ์ฉ
text.encode('euc-kr') # EUC-KR ์ธ์ฝ๋ฉ
text.encode('cp949') # CP949 ์ธ์ฝ๋ฉ (์๋์ฐ์์ ์ฃผ๋ก ์ฌ์ฉ)
# ์๋ฌ ์ฒ๋ฆฌ ์ต์
text = "์๋
ํ์ธ์"
try:
# 'ascii'๋ ํ๊ธ์ ํํํ ์ ์์
text.encode('ascii')
except UnicodeEncodeError:
print("ASCII๋ก ์ธ์ฝ๋ฉํ ์ ์์ต๋๋ค")
# ์๋ฌ ์ฒ๋ฆฌ ๋ฐฉ์ ์ง์
text.encode('ascii', errors='ignore') # b'' (๋ณํ ๋ถ๊ฐ ๋ฌธ์ ๋ฌด์)
text.encode('ascii', errors='replace') # b'?????' (๋ณํ ๋ถ๊ฐ ๋ฌธ์ ? ๋ก ๋์ฒด)
text.encode('ascii', errors='xmlcharrefreplace') # b'안녕하세요' (XML ์ํฐํฐ๋ก ๋์ฒด)
โ ๋ฌธ์ ์ธ์ฝ๋ฉ ๊ด๋ จ Tip:
- UTF-8์ ํ๋์ ์ธ ์น๊ณผ ์์คํ ์์ ๋๋ฆฌ ์ฌ์ฉ๋๋ ํ์ค
- ํ๊ธ Windows์์๋ CP949(MS949) ์ธ์ฝ๋ฉ๋ ํํ ์ฌ์ฉ๋จ
- errors ๋งค๊ฐ๋ณ์๋ก ์ธ์ฝ๋ฉ/๋์ฝ๋ฉ ์ค๋ฅ ์ฒ๋ฆฌ ๋ฐฉ์์ ์ง์ ๊ฐ๋ฅ
- ํ์ผ์ ์ด ๋๋ encoding ๋งค๊ฐ๋ณ์๋ก ์ธ์ฝ๋ฉ ์ง์ ๊ฐ๋ฅ
Python 3์์๋ ๋ฐ์ดํธ(bytes)์ ๋ฌธ์์ด(str)์ด ๋ช ํํ ๊ตฌ๋ถ๋ฉ๋๋ค.
# ๋ฐ์ดํธ ๋ฆฌํฐ๋ด ์์ฑ
b = b'hello' # bytes ๊ฐ์ฒด
s = 'hello' # str ๊ฐ์ฒด
# ๋ฐ์ดํธ์ ๋ฌธ์์ด ๋ณํ
bytes_obj = bytes('์๋
', 'utf-8')
str_obj = bytes_obj.decode('utf-8')
# 16์ง์ ํํ
bytes_obj.hex() # 'ec9588eb8595'
# 16์ง์ ๋ฌธ์์ด์ ๋ฐ์ดํธ๋ก ๋ณํ
bytes.fromhex('ec9588eb8595') # b'\xec\x95\x88\xeb\x85\x95'
# ๋ฐ์ดํธ ๋ฐฐ์ด(์์ ๊ฐ๋ฅํ ๋ฐ์ดํธ)
ba = bytearray(b'hello')
ba[0] = 72 # ASCII ์ฝ๋ 'H'
ba # bytearray(b'Hello')
โ ๋ฐ์ดํธ ๋ฌธ์์ด Tip:
- ๋ฐ์ดํธ(bytes)๋ 0-255 ์ฌ์ด์ ์ ์ ์ํ์ค
- str์ ์ ๋์ฝ๋ ๋ฌธ์์ด์ด๋ฉฐ, bytes๋ ๋ฐ์ด๋๋ฆฌ ๋ฐ์ดํฐ
- ๋คํธ์ํฌ ํต์ , ํ์ผ I/O ๋ฑ์์ ๋ฐ์ดํธ ์ฒ๋ฆฌ ํ์
- ๋ฐ์ดํธ์ ๋ฌธ์์ด ๊ฐ ๋ณํ์ encode/decode ์ฌ์ฉ
์ ๊ทํํ์์ ๋ณต์กํ ๋ฌธ์์ด ํจํด์ ๊ฒ์ํ๊ฑฐ๋ ์กฐ์ํ ๋ ์ฌ์ฉํฉ๋๋ค.
import re
# ํจํด ๋งค์นญ
text = "The rain in Spain"
pattern = r"ai"
re.findall(pattern, text) # ['ai', 'ai']
# ์ฒซ ๋ฒ์งธ ๋งค์น ์ฐพ๊ธฐ
match = re.search(r"ai", text)
if match:
print(match.group()) # 'ai'
print(match.start()) # 5 - ๋งค์น ์์ ์์น
print(match.end()) # 7 - ๋งค์น ๋ ์์น
# ํจํด ๋ฐ๊พธ๊ธฐ
re.sub(r"ai", "XX", text) # 'The rXXn in SpXXn'
# ํจํด์ผ๋ก ๋ถํ
re.split(r"\s", "Hello World Python") # ['Hello', 'World', 'Python']
# ์ ๊ท์ ์ปดํ์ผ(์ฑ๋ฅ ํฅ์)
pattern = re.compile(r"\d+") # ํ๋ ์ด์์ ์ซ์
pattern.findall("There are 123 apples and 456 oranges") # ['123', '456']
# ๋ค์ํ ์ ๊ท์ ํจํด
re.search(r"^\w+", "Hello, World!") # ๋จ์ด ๋ฌธ์๋ก ์์
re.search(r"\d{2,4}", "Phone: 1234-5678") # 2~4์๋ฆฌ ์ซ์
re.search(r"\b[A-Z][a-z]*\b", "Hello World") # ๋๋ฌธ์๋ก ์์ํ๋ ๋จ์ด
โ ์ ๊ท์ Tip:
- r ์ ๋์ฌ(์์ ๋ฌธ์์ด)๋ฅผ ์ ๊ท์ ํจํด์ ์ฌ์ฉํ๋ฉด ์ด์ค์ผ์ดํ ๋ฌธ์ ๋ฐฉ์ง
-
re.compile()
๋ก ์ ๊ท์ ๊ฐ์ฒด๋ฅผ ๋ฏธ๋ฆฌ ์ปดํ์ผํ๋ฉด ์ฑ๋ฅ ํฅ์ - ์์ฃผ ์ฐ๋ ํจํด:
-
\d
: ์ซ์,\w
: ๋จ์ด ๋ฌธ์,\s
: ๊ณต๋ฐฑ ๋ฌธ์ -
*
: 0ํ ์ด์ ๋ฐ๋ณต,+
: 1ํ ์ด์ ๋ฐ๋ณต,?
: 0 ๋๋ 1ํ ๋ฐ์ -
[]
: ๋ฌธ์ ํด๋์ค,^
: ๋ฌธ์์ด ์์,$
: ๋ฌธ์์ด ๋ -
\b
: ๋จ์ด ๊ฒฝ๊ณ,.
: ์์์ ํ ๋ฌธ์
-
-
count()
: ํน์ ๋ฌธ์์ ๊ฐ์ ์ธ๊ธฐ -
find()/index()
: ์์น ์ฐพ๊ธฐ -
join()
: ๋ฌธ์์ด ์ฝ์ -
upper()/lower()
: ๋์๋ฌธ์ ๋ณํ -
strip()
: ๊ณต๋ฐฑ ์ ๊ฑฐ -
replace()
: ๋ฌธ์์ด ์นํ -
split()
: ๋ฌธ์์ด ๋ถ๋ฆฌ -
startswith()/endswith()
: ์์/๋ ๋ฌธ์์ด ํ์ธ -
isalpha()/isdigit()/isalnum()
: ๋ฌธ์์ด ํ์ ํ์ธ -
encode()/decode()
: ์ธ์ฝ๋ฉ/๋์ฝ๋ฉ