Repository navigation
Expand file tree
/
Copy pathStringMethods.py
More file actions
118 lines (93 loc) · 3.34 KB
/
Copy pathStringMethods.py
File metadata and controls
118 lines (93 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# =============================================
# 문자열 함수(메서드) 예제
# =============================================
# ==================== 대소문자 변환 ====================
print("=== 대소문자 변환 ===")
text = "Hello Python"
print(text.upper()) # HELLO PYTHON
print(text.lower()) # hello python
# ==================== 공백 제거 ====================
print("\n=== 공백 제거 ===")
text = " Hello, world "
print(f"[{text.strip()}]") # [Hello, world]
print(f"[{text.lstrip()}]") # [Hello, world ]
print(f"[{text.rstrip()}]") # [ Hello, world]
# ==================== 치환 ====================
print("\n=== 치환 ===")
text = "Hello, world"
print(text.replace("world", "Python")) # Hello, Python
# 메서드 체이닝
text = " Hello, world "
print(text.upper().strip().replace('H', 'J')) # JELLO, WORLD
# 실용적 활용: 가격 문자열 처리
price = "21,300원"
result = int(price.replace("원", "").replace(",", "").strip())
print(result) # 21300
# ==================== split(), join() ====================
print("\n=== split(), join() ===")
# split()
data = "사과,바나나,파인애플,포도"
fruits = data.split(",")
print(fruits) # ['사과', '바나나', '파인애플', '포도']
text = "Hello Python 2024"
words = text.split()
print(words) # ['Hello', 'Python', '2024']
# join()
items = ["A", "B", "C", "D"]
result = ",".join(items)
print(result) # A,B,C,D
time_parts = ["23", "45", "12"]
print(":".join(time_parts)) # 23:45:12
# ==================== 검색 ====================
print("\n=== 검색 ===")
text = "hello python"
# index()
print(text.index("lo")) # 3
# find()
print(text.find("xx")) # -1 (없으면 -1)
print(text.find("python")) # 6
# count()
text = "aabababbabbaababb"
print(text.count("a")) # 8
# ==================== startswith(), endswith() ====================
print("\n=== startswith(), endswith() ===")
url = "http://example.com/index.html"
print(url.startswith("http")) # True
print(url.startswith("https")) # False
print(url.endswith(".html")) # True
print(url.endswith(".php")) # False
# ==================== ord(), chr() ====================
print("\n=== ord(), chr() ===")
print(ord("a")) # 97
print(ord("A")) # 65
print(chr(97)) # 'a'
print(chr(65)) # 'A'
# 한글 개수
print(ord("가")) # 44032
print(ord("힣")) # 55203
print(ord("힣") - ord("가") + 1) # 11172
# ==================== String 특성 ====================
print("\n=== String 특성 ===")
text = "hello Python"
# 인덱싱, 슬라이싱 가능
print(text[-4]) # 't'
print(text[::2]) # 'hlopto'
print(text[::-1]) # 'nohtyP olleh'
# in 연산자 (대소문자 구분)
print("P" in text) # True
print("p" in text) # False
# immutable - 변경 불가
# text[0] = 'H' # TypeError
# ==================== 문자 빈도 계산 ====================
print("\n=== 문자 빈도 계산 ===")
word = "Alice in wonderland"
data = word.lower()
# 방법 1: for 문
result = {}
for ch in data:
if ch.isalpha():
result[ch] = result.get(ch, 0) + 1
print(result)
# 방법 2: Dict Comprehension
result = {ch: data.count(ch) for ch in data if ch.isalpha()}
print(result)