- ๋ฌธ์์ด ๊ธฐ๋ณธ ํน์ฑ
- ๋์๋ฌธ์ ๋ณํ ๋ฉ์๋
- ๊ณต๋ฐฑ ์ ๊ฑฐ ๋ฉ์๋
- ๋ฌธ์์ด ์นํ ๋ฉ์๋
- ๋ถ๋ฆฌ์ ๊ฒฐํฉ ๋ฉ์๋
- ๊ฒ์ ๋ฉ์๋
- ์์/๋ ํ์ธ ๋ฉ์๋
- ์ ๋์ฝ๋ ํจ์
- ๋ฌธ์์ด ๋ฑ์ฅ ํ์ ๊ณ์ฐ
- ๋ฉ์๋ ์ฒด์ด๋
- ๊ธฐํ ์ ์ฉํ ๋ฉ์๋
- Immutable (๋ถ๋ณ): ๋ฌธ์์ด์ ํ๋ฒ ์์ฑ๋๋ฉด ๋ณ๊ฒฝํ ์ ์์
- ์ฌ๋ผ์ด์ฑ ๋ฐ ์ธ๋ฑ์ฑ ๊ฐ๋ฅ
- ๋์๋ฌธ์ ๊ตฌ๋ถ
# ๋ฌธ์์ด์ ๋ณ๊ฒฝ ๋ถ๊ฐ๋ฅ
mystr = "hello Python"
mystr[0] = 'Y' # TypeError: 'str' object does not support item assignment
# ์ธ๋ฑ์ฑ๊ณผ ์ฌ๋ผ์ด์ฑ
print(mystr[-4]) # ๋ค์์ 4๋ฒ์งธ ๋ฌธ์: 't'
print(mystr[::2]) # 2์นธ์ฉ ๊ฑด๋๋ฐ๋ฉฐ ์ถ๋ ฅ: 'hlopto'
print(mystr[::-1]) # ์ญ์ ์ถ๋ ฅ: 'nohtyP olleh'
# ๋์๋ฌธ์ ๊ตฌ๋ถ
print("P" in mystr) # False (๋๋ฌธ์ P)
print("p" in mystr) # True (์๋ฌธ์ p๊ฐ Python์ ์์)# 1. ์๋ฐ์ดํ (double quotation)
print("Python is fun")
# 2. ํ๋ฐ์ดํ (single quotation)
print('Python is fun')
# 3. ์๋ฐ์ดํ 3๊ฐ (์ฌ๋ฌ ์ค ๋ฌธ์์ด)
print("""Python is fun
and easy to learn""")
# 4. ํ๋ฐ์ดํ 3๊ฐ (์ฌ๋ฌ ์ค ๋ฌธ์์ด)
print('''Python is fun
and easy to learn''')| ๋ฉ์๋ | ์ค๋ช | ๋ฐํ๊ฐ |
|---|---|---|
upper() |
๋ชจ๋ ๋ฌธ์๋ฅผ ๋๋ฌธ์๋ก ๋ณํ | ์ ๋ฌธ์์ด |
lower() |
๋ชจ๋ ๋ฌธ์๋ฅผ ์๋ฌธ์๋ก ๋ณํ | ์ ๋ฌธ์์ด |
capitalize() |
์ฒซ ๊ธ์๋ง ๋๋ฌธ์๋ก ๋ณํ | ์ ๋ฌธ์์ด |
title() |
๊ฐ ๋จ์ด์ ์ฒซ ๊ธ์๋ฅผ ๋๋ฌธ์๋ก ๋ณํ | ์ ๋ฌธ์์ด |
swapcase() |
๋์๋ฌธ์ ๋ฐ์ | ์ ๋ฌธ์์ด |
๋ฌธ์์ด์ ๋๋ฌธ์๋ก ๋ณํํฉ๋๋ค.
str1 = "Apple"
print(str1.upper())
# ์ถ๋ ฅ๊ฒฐ๊ณผ: 'APPLE'
# ์๋ณธ์ ๋ณ๊ฒฝ๋์ง ์์ (immutable)
print(str1)
# ์ถ๋ ฅ๊ฒฐ๊ณผ: 'Apple'๋ฌธ์์ด์ ์๋ฌธ์๋ก ๋ณํํฉ๋๋ค.
str1 = "Apple"
print(str1.lower())
# ์ถ๋ ฅ๊ฒฐ๊ณผ: 'apple'text = "hello WORLD python"
print(text.capitalize()) # ์ฒซ ๊ธ์๋ง ๋๋ฌธ์: 'Hello world python'
print(text.title()) # ๊ฐ ๋จ์ด ์ฒซ ๊ธ์ ๋๋ฌธ์: 'Hello World Python'
print(text.swapcase()) # ๋์๋ฌธ์ ๋ฐ์ : 'HELLO world PYTHON'| ๋ฉ์๋ | ์ค๋ช |
|---|---|
strip() |
์ข์ฐ ๊ณต๋ฐฑ ์ ๊ฑฐ |
lstrip() |
์ผ์ชฝ ๊ณต๋ฐฑ๋ง ์ ๊ฑฐ |
rstrip() |
์ค๋ฅธ์ชฝ ๊ณต๋ฐฑ๋ง ์ ๊ฑฐ |
strip(chars) |
์ง์ ํ ๋ฌธ์๋ค ์ ๊ฑฐ |
๋ฌธ์์ด์ ์ข์ฐ ๊ณต๋ฐฑ์ ์ ๊ฑฐํฉ๋๋ค.
a = " Hello, world "
print(f"์๋ณธ: [{a}]")
# ์ถ๋ ฅ๊ฒฐ๊ณผ: ์๋ณธ: [ Hello, world ]
print(f"strip(): [{a.strip()}]")
# ์ถ๋ ฅ๊ฒฐ๊ณผ: strip(): [Hello, world]
print(f"lstrip(): [{a.lstrip()}]")
# ์ถ๋ ฅ๊ฒฐ๊ณผ: lstrip(): [Hello, world ]
print(f"rstrip(): [{a.rstrip()}]")
# ์ถ๋ ฅ๊ฒฐ๊ณผ: rstrip(): [ Hello, world]text = "###Python###"
print(text.strip('#'))
# ์ถ๋ ฅ๊ฒฐ๊ณผ: 'Python'
text2 = "xxxyyyPythonyyxxx"
print(text2.strip('xy'))
# ์ถ๋ ฅ๊ฒฐ๊ณผ: 'Python'ํน์ ๋ฌธ์์ด์ ๋ค๋ฅธ ๋ฌธ์์ด๋ก ์นํํฉ๋๋ค.
a = " Hello, world "
# ๊ธฐ๋ณธ ์ฌ์ฉ
print(a.upper().strip().replace('H', 'J'))
# ์ถ๋ ฅ๊ฒฐ๊ณผ: 'JELLO, WORLD'
# count ๋งค๊ฐ๋ณ์: ์นํ ํ์ ์ ํ
text = "apple apple apple"
print(text.replace('apple', 'orange', 2))
# ์ถ๋ ฅ๊ฒฐ๊ณผ: 'orange orange apple'# ๊ฐ๊ฒฉ ๋ฌธ์์ด์์ ์ซ์๋ง ์ถ์ถํ๊ธฐ
price = "21,300์"
# ๋ฐฉ๋ฒ 1: replace ์ฐ์ ์ฌ์ฉ
result = int(price.replace("์", "").replace(",", "").strip())
print(result)
# ์ถ๋ ฅ๊ฒฐ๊ณผ: 21300
# ๋ฐฉ๋ฒ 2: ์ฌ๋ผ์ด์ฑ๊ณผ replace ์กฐํฉ
result = int(price.strip().replace(",", "")[:-1])
print(result)
# ์ถ๋ ฅ๊ฒฐ๊ณผ: 21300๋ฌธ์์ด์ ๊ตฌ๋ถ์ ๊ธฐ์ค์ผ๋ก ๋ถ๋ฆฌํ์ฌ ๋ฆฌ์คํธ๋ก ๋ฐํํฉ๋๋ค.
| ๋งค๊ฐ๋ณ์ | ์ค๋ช | ๊ธฐ๋ณธ๊ฐ |
|---|---|---|
separator |
๋ถ๋ฆฌ ๊ธฐ์ค ๋ฌธ์์ด | ๊ณต๋ฐฑ (์ฐ์ ๊ณต๋ฐฑ๋ ์๋ ์ฒ๋ฆฌ) |
maxsplit |
์ต๋ ๋ถ๋ฆฌ ํ์ | -1 (๋ฌด์ ํ) |
# ๊ณต๋ฐฑ ๊ธฐ์ค์ผ๋ก ๋ถ๋ฆฌ (๊ธฐ๋ณธ)
myStr = "Hello Python 2024"
print(myStr.split())
# ์ถ๋ ฅ๊ฒฐ๊ณผ: ['Hello', 'Python', '2024']
# ํน์ ๊ตฌ๋ถ์๋ก ๋ถ๋ฆฌ
date = "2025-01-16"
print(date.split("-"))
# ์ถ๋ ฅ๊ฒฐ๊ณผ: ['2025', '01', '16']
# ์ต๋ ๋ถ๋ฆฌ ํ์ ์ง์
text = "a,b,c,d,e"
print(text.split(",", 2))
# ์ถ๋ ฅ๊ฒฐ๊ณผ: ['a', 'b', 'c,d,e']
# ์ผํ์ ๊ณต๋ฐฑ์ผ๋ก ๋ ๋ฐ์ดํฐ ๋ถ๋ฆฌ
data = "์ฌ๊ณผ,๋ฐ๋๋,ํ์ธ์ ํ,ํฌ๋,๋ณต์ญ์"
print(data.split(","))
# ์ถ๋ ฅ๊ฒฐ๊ณผ: ['์ฌ๊ณผ', '๋ฐ๋๋', 'ํ์ธ์ ํ', 'ํฌ๋', '๋ณต์ญ์']Delimiter (๊ตฌ๋ถ์): ํน์ ๋ฌธ์์ด์ ๊ธฐ์ค์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฆฌํ๋ ๋ฌธ์
๋ฆฌ์คํธ๋ฅผ ํ๋์ ๋ฌธ์์ด๋ก ๊ฒฐํฉํฉ๋๋ค.
# ๋ฆฌ์คํธ๋ฅผ ๋ฌธ์์ด๋ก ๊ฒฐํฉ
data = ["A", "B", "C", "D", "E"]
print(",".join(data))
# ์ถ๋ ฅ๊ฒฐ๊ณผ: 'A,B,C,D,E'
# ์๊ฐ ํ์์ผ๋ก ๊ฒฐํฉ
time_parts = ["23", "45", "12"]
print(":".join(time_parts))
# ์ถ๋ ฅ๊ฒฐ๊ณผ: '23:45:12'
# ๋ฌธ์์ด์ ๊ฐ ๋ฌธ์ ์ฌ์ด์ ๊ตฌ๋ถ์ ์ฝ์
mystr = "animals"
print('-'.join(mystr))
# ์ถ๋ ฅ๊ฒฐ๊ณผ: 'a-n-i-m-a-l-s'
# ๋ฌธ์์ด์ ๋ฆฌ์คํธ๋ก ๋ณํ ํ ๊ฒฐํฉ
print('-'.join(list(mystr)))
# ์ถ๋ ฅ๊ฒฐ๊ณผ: 'a-n-i-m-a-l-s'# ๊ณต๋ฐฑ ์ ๊ทํ (์ฌ๋ฌ ๊ณต๋ฐฑ์ ํ๋๋ก)
text = "Hello World Python"
normalized = " ".join(text.split())
print(normalized)
# ์ถ๋ ฅ๊ฒฐ๊ณผ: 'Hello World Python'| ๋ฉ์๋ | ์ค๋ช | ๋ชป ์ฐพ์ ๊ฒฝ์ฐ |
|---|---|---|
index() |
๋ฌธ์์ด ์์น ๋ฐํ | ValueError ๋ฐ์ |
find() |
๋ฌธ์์ด ์์น ๋ฐํ | -1 ๋ฐํ |
str3 = "hello python"
# index() - ์์น ๋ฐํ
print(str3.index("lo"))
# ์ถ๋ ฅ๊ฒฐ๊ณผ: 3
# find() - ์์น ๋ฐํ
print(str3.find("python"))
# ์ถ๋ ฅ๊ฒฐ๊ณผ: 6
# ์๋ ๋ฌธ์์ด ๊ฒ์
# print(str3.index("xx")) # ValueError: substring not found
print(str3.find("xx"))
# ์ถ๋ ฅ๊ฒฐ๊ณผ: -1
# ์์ ์์น ์ง์
print(str3.find("o", 5)) # ์ธ๋ฑ์ค 5 ์ดํ์์ 'o' ์ฐพ๊ธฐ
# ์ถ๋ ฅ๊ฒฐ๊ณผ: 10ํน์ ๋ฌธ์์ด์ ๋ฑ์ฅ ํ์๋ฅผ ๋ฐํํฉ๋๋ค.
str4 = "aabababbabbaababb"
print(str4.count("a"))
# ์ถ๋ ฅ๊ฒฐ๊ณผ: 8
print(str4.count("ab"))
# ์ถ๋ ฅ๊ฒฐ๊ณผ: 5
# ๋ฒ์ ์ง์
print(str4.count("a", 0, 10)) # ์ธ๋ฑ์ค 0~9 ๋ฒ์์์๋ง ๊ฒ์
# ์ถ๋ ฅ๊ฒฐ๊ณผ: 5์ค๋ฅธ์ชฝ(๋ค)์์๋ถํฐ ๊ฒ์ํฉ๋๋ค.
text = "hello world hello"
print(text.find("hello")) # ์ผ์ชฝ๋ถํฐ: 0
print(text.rfind("hello")) # ์ค๋ฅธ์ชฝ๋ถํฐ: 12๋ฌธ์์ด์ด ํน์ ๋ฌธ์์ด๋ก ์์ํ๋์ง ํ์ธํฉ๋๋ค.
๋ฌธ์์ด์ด ํน์ ๋ฌธ์์ด๋ก ๋๋๋์ง ํ์ธํฉ๋๋ค.
url = "http://example.com/index.html"
print(url.startswith("http"))
# ์ถ๋ ฅ๊ฒฐ๊ณผ: True
print(url.startswith("https"))
# ์ถ๋ ฅ๊ฒฐ๊ณผ: False
print(url.endswith(".html"))
# ์ถ๋ ฅ๊ฒฐ๊ณผ: True
print(url.endswith(".css"))
# ์ถ๋ ฅ๊ฒฐ๊ณผ: False
# ํํ๋ก ์ฌ๋ฌ ํจํด ํ์ธ
filename = "document.pdf"
print(filename.endswith(('.pdf', '.doc', '.txt')))
# ์ถ๋ ฅ๊ฒฐ๊ณผ: True# ํ์ผ ํ์ฅ์ ํ์ธ
def is_image_file(filename):
return filename.lower().endswith(('.jpg', '.jpeg', '.png', '.gif'))
print(is_image_file("photo.JPG")) # True
print(is_image_file("document.pdf")) # False๋ฌธ์๋ฅผ **Unicode ์ฝ๋๊ฐ(์ ์)**์ผ๋ก ๋ฐํํฉ๋๋ค.
Unicode ์ฝ๋๊ฐ์ ๋ฌธ์๋ก ๋ฐํํฉ๋๋ค.
# ord(): ๋ฌธ์ -> ์ฝ๋๊ฐ
print(ord("a"))
# ์ถ๋ ฅ๊ฒฐ๊ณผ: 97
print(ord("A"))
# ์ถ๋ ฅ๊ฒฐ๊ณผ: 65
print(ord("๊ฐ"))
# ์ถ๋ ฅ๊ฒฐ๊ณผ: 44032
# chr(): ์ฝ๋๊ฐ -> ๋ฌธ์
print(chr(97))
# ์ถ๋ ฅ๊ฒฐ๊ณผ: 'a'
print(chr(65))
# ์ถ๋ ฅ๊ฒฐ๊ณผ: 'A'
print(chr(44032))
# ์ถ๋ ฅ๊ฒฐ๊ณผ: '๊ฐ'
# ํ๊ธ ๊ฐ์ ๊ณ์ฐ
hangul_count = ord("ํฃ") - ord("๊ฐ") + 1
print(f"ํ๊ธ ๊ฐ์: {hangul_count}")
# ์ถ๋ ฅ๊ฒฐ๊ณผ: ํ๊ธ ๊ฐ์: 11172# ์ํ๋ฒณ ๋๋ฌธ์ ์ถ๋ ฅ
for i in range(26):
print(chr(ord('A') + i), end=' ')
# ์ถ๋ ฅ๊ฒฐ๊ณผ: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
# ๋ฌธ์๊ฐ ๋๋ฌธ์์ธ์ง ํ์ธ
def is_uppercase(char):
return ord('A') <= ord(char) <= ord('Z')
print(is_uppercase('A')) # True
print(is_uppercase('a')) # Falseword = "Alice in wonderland"
data = word.lower()
result = {}
for ch in data:
if ch.isalpha(): # ์ํ๋ฒณ์ธ์ง ํ์ธ
result[ch] = result.get(ch, 0) + 1
print(result)
# ์ถ๋ ฅ๊ฒฐ๊ณผ: {'a': 2, 'l': 2, 'i': 2, 'c': 1, 'e': 2, 'n': 3, 'w': 1, 'o': 1, 'd': 2, 'r': 1}word = "Alice in wonderland"
w = word.lower()
result = {ch: w.count(ch) for ch in w if ch.isalpha()}
print(result)
# ์ถ๋ ฅ๊ฒฐ๊ณผ: {'a': 2, 'l': 2, 'i': 2, 'c': 1, 'e': 2, 'n': 3, 'w': 1, 'o': 1, 'd': 2, 'r': 1}from collections import Counter
word = "Alice in wonderland"
counter = Counter(ch for ch in word.lower() if ch.isalpha())
print(dict(counter))
# ์ถ๋ ฅ๊ฒฐ๊ณผ: {'a': 2, 'l': 2, 'i': 2, 'c': 1, 'e': 2, 'n': 3, 'w': 1, 'o': 1, 'd': 2, 'r': 1}
# ๊ฐ์ฅ ๋ง์ด ๋ฑ์ฅํ ๋ฌธ์
print(counter.most_common(3))
# ์ถ๋ ฅ๊ฒฐ๊ณผ: [('n', 3), ('a', 2), ('l', 2)]๋๋ถ๋ถ์ ๋ฌธ์์ด ๋ฉ์๋๋ ์๋ก์ด ๋ฌธ์์ด์ ๋ฐํํ๋ฏ๋ก ์ฒด์ด๋์ด ๊ฐ๋ฅํฉ๋๋ค.
# ๋ฉ์๋ ์ฒด์ด๋ ์์
a = " Hello, World "
result = a.upper().strip().replace('H', 'J')
print(result)
# ์ถ๋ ฅ๊ฒฐ๊ณผ: 'JELLO, WORLD'
# ๋ณต์กํ ์ฒด์ด๋
text = " Python Programming "
result = text.strip().lower().replace(' ', '_').title()
print(result)
# ์ถ๋ ฅ๊ฒฐ๊ณผ: 'Python_Programming'
# ๋ฐ์ดํฐ ์ ์ ํ์ดํ๋ผ์ธ
raw_data = " John,Doe,30,New York "
cleaned = raw_data.strip().split(',')
print(cleaned)
# ์ถ๋ ฅ๊ฒฐ๊ณผ: ['John', 'Doe', '30', 'New York']| ๋ฉ์๋ | ์ค๋ช |
|---|---|
isalpha() |
๋ชจ๋ ๋ฌธ์๊ฐ ์ํ๋ฒณ์ธ์ง ํ์ธ |
isdigit() |
๋ชจ๋ ๋ฌธ์๊ฐ ์ซ์์ธ์ง ํ์ธ |
isalnum() |
๋ชจ๋ ๋ฌธ์๊ฐ ์ํ๋ฒณ ๋๋ ์ซ์์ธ์ง ํ์ธ |
isspace() |
๋ชจ๋ ๋ฌธ์๊ฐ ๊ณต๋ฐฑ์ธ์ง ํ์ธ |
isupper() |
๋ชจ๋ ๋ฌธ์๊ฐ ๋๋ฌธ์์ธ์ง ํ์ธ |
islower() |
๋ชจ๋ ๋ฌธ์๊ฐ ์๋ฌธ์์ธ์ง ํ์ธ |
print("Hello".isalpha()) # True
print("12345".isdigit()) # True
print("Hello123".isalnum()) # True
print(" ".isspace()) # True
print("HELLO".isupper()) # True
print("hello".islower()) # True| ๋ฉ์๋ | ์ค๋ช |
|---|---|
center(width) |
๊ฐ์ด๋ฐ ์ ๋ ฌ |
ljust(width) |
์ผ์ชฝ ์ ๋ ฌ |
rjust(width) |
์ค๋ฅธ์ชฝ ์ ๋ ฌ |
zfill(width) |
์ผ์ชฝ์ 0์ผ๋ก ์ฑ์ |
text = "Python"
print(text.center(20, '-'))
# ์ถ๋ ฅ๊ฒฐ๊ณผ: '-------Python-------'
print(text.ljust(20, '-'))
# ์ถ๋ ฅ๊ฒฐ๊ณผ: 'Python--------------'
print(text.rjust(20, '-'))
# ์ถ๋ ฅ๊ฒฐ๊ณผ: '--------------Python'
print("42".zfill(5))
# ์ถ๋ ฅ๊ฒฐ๊ณผ: '00042'์ค๋ฐ๊ฟ ๋ฌธ์๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ถ๋ฆฌํฉ๋๋ค.
text = """Line 1
Line 2
Line 3"""
lines = text.splitlines()
print(lines)
# ์ถ๋ ฅ๊ฒฐ๊ณผ: ['Line 1', 'Line 2', 'Line 3']| ์นดํ ๊ณ ๋ฆฌ | ๋ฉ์๋ | ์ค๋ช |
|---|---|---|
| ๋์๋ฌธ์ | upper(), lower() |
๋/์๋ฌธ์ ๋ณํ |
capitalize(), title() |
์ฒซ ๊ธ์ ๋๋ฌธ์ํ | |
swapcase() |
๋์๋ฌธ์ ๋ฐ์ | |
| ๊ณต๋ฐฑ | strip(), lstrip(), rstrip() |
๊ณต๋ฐฑ/๋ฌธ์ ์ ๊ฑฐ |
| ์นํ | replace(old, new) |
๋ฌธ์์ด ์นํ |
| ๋ถ๋ฆฌ/๊ฒฐํฉ | split(sep) |
๋ถ๋ฆฌ -> ๋ฆฌ์คํธ |
join(iterable) |
๋ฆฌ์คํธ -> ๊ฒฐํฉ | |
| ๊ฒ์ | find(), rfind() |
์์น ๋ฐํ (-1) |
index(), rindex() |
์์น ๋ฐํ (์๋ฌ) | |
count() |
๋ฑ์ฅ ํ์ | |
| ํ์ธ | startswith(), endswith() |
์์/๋ ํ์ธ |
isalpha(), isdigit(), etc. |
๋ฌธ์ ํ์ ํ์ธ | |
| ์ ๋์ฝ๋ | ord(), chr() |
๋ฌธ์ <-> ์ฝ๋๊ฐ |
| ์ ๋ ฌ | center(), ljust(), rjust() |
๋ฌธ์์ด ์ ๋ ฌ |