728x90
반응형
Python String Methods
1) capitalize() : 문자열의 첫 글자를 대문자로 변환
text: str = "hello"
print(text.capitalize()) # Hello
2) casefold() : 문자열을 소문자로 변환
text1: str = 'MARIo'
text2: str = 'maRIO'
print(text1.casefold()) # mario
print(text2.casefold()) # mario
print(text1.casefold() == text2.casefold()) # True
3) center() : 문자열을 가운데 정렬
text: str = "hello"
print(text.center(20)) # hello
print(text.center(20, '-')) # -------hello--------
4) count() : 문자열에서 특정 문자열의 개수를 반환
text: str = "hello"
print(text.count('l')) # 2
5) encode() : 문자열을 바이트 객체로 변환
text: str = "hello"
print(text.encode()) # b'hello'
print(text.encode(encoding='utf-8', errors='strict')) # b'hello'
6) endswith() : 문자열이 특정 문자로 끝나는지 확인
text: str = "hello"
print(text.endswith(('a','o'))) # True # a or o
7) expandtabs() : 문자열의 탭을 공백으로 바꿈
text: str = "hello\tim\nin\tthe world"
print(text.expandtabs(20))
# hello im
# in the world
8) find() : 문자열에서 특정 문자열을 찾아서 그 위치를 반환
text: str = "hello, my name is Mario"
position: int = text.find('M')
print(f"position {position}") # position 18
print(f"{text[position:]}") # Mario # position to end
9) format() : 문자열 포맷팅
text: str = "hello, my name is {name}, and I am {age} years old"
print(text.format(name='Mario', age=20))
# hello, my name is Mario, and I am 20 years old
10) format_map() : 문자열의 포맷을 지정하는 함수
text: str = "hello, my name is {name}, and I am {age} years old"
print(text.format_map({'name': 'Mario', 'age': 20}))
# hello, my name is Mario, and I am 20 years old
11) index() : 문자열에서 특정 문자열의 위치를 반환하는 함수
text: str = "hello, my name is Mario"
position: int = text.index('Mario')
print(f"position {position}") # position 18
print(f"{text[position:]}") # Mario # position to end
# find와 다르게 찾는 문자열이 없을 경우 ValueError 발생
try:
text.index('mario')
except Exception as e:
print(f"{e.__class__.__name__} : {e}") # ValueError : substring not found
12) isalnum() : 문자열이 알파벳 또는 숫자로만 이루어져 있는지 확인하는 함수
text1: str = "hello"
text2: str = "hello12!"
print(text1.isalnum()) # True # 문자열이 알파벳 또는 숫자로만 이루어져 있는지 확인
print(text2.isalnum()) # False (!) != string 문자열이 알파벳 또는 숫자로만 이루어져 있는지 확인
13) isalpha() : 문자열이 알파벳으로만 이루어져 있는지 확인
text1: str="hello123"
print(text1.isalpha()) # False # 문자열이 알파벳으로만 이루어져 있는지 확인
14) isascii() : 문자열이 ASCII 문자로만 이루어져 있는지 확인
text1: str = "hello"
text2: str = "hello😇"
print(text1.isascii()) # True
print(text2.isascii()) # False
15) isdecimal() : 문자열이 10진수로만 이루어져 있는지 확인(일반적으로 사용)
text1: str = "❶❷❸"
print(text1.isdecimal()) # False # 문자열이 10진수로만 이루어져 있는지 확인
16) isdigit() : 문자열이 숫자로만 이루어져 있는지 확인
text2: str = "❶❷❸"
print(text2.isdigit()) # True # 문자열이 숫자로만 이루어져 있는지 확인
17) isnumeric() : 문자열이 숫자로만 이루어져 있는지 확인
text1: str = "❶❷❸"
print(text1.isnumeric()) # True # 문자열이 식별자로 사용할 수 있는지 확인
18) isidentifier() : 문자열이 식별자로 사용할 수 있는지 확인하는 함수
text: str = "이 문자열로 변수 선언 가능할까?"
print(text.isidentifier()) # False
text: str = "이_문자열로_변수_선언_가능할까"
print(text.isidentifier()) # True
19) islower() : 문자열이 소문자로만 이루어져 있는지 확인
text: str = "hello"
print(text.islower()) # True
text: str = "Hello"
print(text.islower()) # False
20) isprintable() : 문자열이 출력 가능한지 확인하는 함수
text: str = "hello\n"
print(text.isprintable()) # False # escape 문자열이 포함될 경우 False
text: str = "hello\\n"
print(text.isprintable()) # True
반응형
'Python' 카테고리의 다른 글
[...] 파이썬 어디까지 아니? (0) | 2024.06.12 |
---|---|
[site-packages] AttributeError: module 'lib' has no attribute 'X509_V_FLAG_CB_ISSUER_CHECK' (0) | 2024.06.01 |
[Python] f-string trick (2) (0) | 2024.03.17 |
[Python] 이거 모르면 너는 초보 (0) | 2024.03.05 |
[Python] f-string trick (1) (0) | 2024.03.02 |