✅문자열 관련 내장 함수 (Built-in Function)
함수 | 설명 | 예제 |
ord(char) | 문자의 유니코드(ASCII) 값을 반환 | ord('A') → 65 |
chr(num) | 유니코드 값을 문자로 변환 | chr(97) → 'a' |
len(string) | 문자열 길이 반환 | len("hello") → 5 |
str() | 다른 자료형을 문자열로 변환 | str(123) → "123" |
repr(obj) | 객체를 문자열로 변환 (개발자용) | repr(3.14) → '3.14' |
format(value, spec) | 포맷 형식 적용 | format(3.1415, ".2f") → '3.14' |
# 입력: 문자 'A' → 출력: 유니코드 값 65
print(ord('A')) # 65
# 입력: 숫자 97 → 출력: 문자 'a'
print(chr(97)) # 'a'
text = "Python"
# 문자열 길이 반환
print(len(text)) # 6
# 정수를 문자열로 변환
print(str(123)) # '123'
# 실수를 문자열로 변환
print(repr(3.1415)) # '3.1415' (개발자용 문자열 변환)
✅문자열 메서드 (String Method)
메서드 | 설명 | 예제 |
string.upper() | 모든 문자 대문자로 변환 | "hello".upper() → "HELLO" |
string.lower() | 모든 문자 소문자로 변환 | "HELLO".lower() → "hello" |
string.capitalize() | 첫 글자만 대문자로 변환 | "hello".capitalize() → "Hello" |
string.title() | 각 단어의 첫 글자만 대문자로 변환 | "hello world".title() → "Hello World" |
string.strip() | 앞뒤 공백 제거 | " hello ".strip() → "hello" |
string.lstrip() | 왼쪽 공백 제거 | " hello ".lstrip() → "hello " |
string.rstrip() | 오른쪽 공백 제거 | " hello ".rstrip() → " hello" |
string.replace(old, new) | 특정 문자열 변경 | "apple".replace("p", "b") → "abble" |
string.split(sep) | 특정 구분자로 문자열 분할 → 리스트 반환 | "a,b,c".split(",") → ['a', 'b', 'c'] |
string.join(iterable) | 리스트의 문자열을 하나의 문자열로 결합 | ",".join(["a", "b", "c"]) → "a,b,c" |
string.count(sub) | 특정 문자열 개수 반환 | "banana".count("a") → 3 |
string.find(sub) | 특정 문자열 처음 등장 위치 반환 (없으면 -1) | "hello".find("e") → 1 |
string.index(sub) | 특정 문자열 처음 등장 위치 반환 (없으면 오류) | "hello".index("e") → 1 |
string.startswith(prefix) | 특정 문자열로 시작하는지 확인 (True/False) | "hello".startswith("he") → True |
string.endswith(suffix) | 특정 문자열로 끝나는지 확인 (True/False) | "hello".endswith("lo") → True |
string.isalpha() | 모든 문자가 알파벳인지 확인 | "hello".isalpha() → True |
string.isdigit() | 모든 문자가 숫자인지 확인 | "123".isdigit() → True |
string.isalnum() | 모든 문자가 알파벳 또는 숫자인지 확인 | "abc123".isalnum() → True |
text = "hello world"
# 'o'가 처음 나오는 위치 찾기
print(text.find('o')) # 4
# 'x'는 존재하지 않으므로 -1 반환
print(text.find('x')) # -1
# 존재하지 않는 문자를 index()로 찾으면 오류 발생
# print(text.index('x')) # ValueError 발생
text = "apple"
# 'p'를 'b'로 변경
print(text.replace("p", "b")) # 'abble'
csv_data = "apple,banana,grape"
# 콤마(,) 기준으로 문자열 분할 → 리스트 반환
fruits = csv_data.split(',')
print(fruits) # ['apple', 'banana', 'grape']
# 리스트를 다시 하나의 문자열로 결합
print(", ".join(fruits)) # 'apple, banana, grape'
filename = "report.pdf"
# "report"로 시작하는지 확인
print(filename.startswith("report")) # True
# ".pdf"로 끝나는지 확인
print(filename.endswith(".pdf")) # True
text1 = "Python"
text2 = "1234"
text3 = "Python123"
# 알파벳만 포함하는지 확인
print(text1.isalpha()) # True
print(text2.isalpha()) # False
# 숫자만 포함하는지 확인
print(text2.isdigit()) # True
print(text3.isdigit()) # False
# 알파벳 + 숫자인지 확인
print(text3.isalnum()) # True
'Python Basic Syntax (파이썬 기초 문법) > Method (메서드)' 카테고리의 다른 글
Set Function & Method (세트 내장함수 & 메서드) (1) | 2025.02.14 |
---|---|
Dictionray Function & Method (딕셔너리 내장함수 & 메서드) (0) | 2025.02.14 |
List Function & Method (리스트 내장함수 & 메서드) (0) | 2025.02.14 |
Numeric Type Function & Method (숫자형 내장함수 & 메서드) (0) | 2025.02.14 |