미식가의 개발 일기

[파이썬] 문자열 조작 본문

Python

[파이썬] 문자열 조작

대체불가 핫걸 2024. 7. 11. 19:07

포맷 스트링(f-string)

문자열 내에서 변수나 표현식의 값을 포맷
idx = 1
fruit = "Apple"

print('{}: {}'.format(idx + 1, fruit)) 
print(f'{idx + 1}: {fruit}') -> 2: Apple

a = 1.234
print(f'{a:.2f}'} -> 1.23​

조인(join)

iterable 객체(리스트, 튜플 등)의 요소들을 하나의 문자열로 연결할 때 사용
words = ["Hello", "World", "!"]
combined_string = " ".join(words) -> Hello World !


words = ["Hello", "World", "!"]
combined_string = ".".join(words) -> Hello.World.!

 

구분자 지정(sep)

print('A1', 'B1', sep=',') -> A1, B1

 

줄바꿈 지정(end)

print('aa', end=' ')
print('bb')
-> aa bb

 

접두사, 접미사 체크(startswith, endswitch)

startswith(접두사): 지정하는 특정 문자로 시작하는지
str = 'kang minji'
result = str.startswitch('kang')
print(result) -> True
endswith(접미사): 지정하는 특정 문자로 끝나는지
str = 'hojun ddong'
result = str.endswitch('ddong')
print(result) -> True
반응형