Engineering Note

[Python] startwith, startend 본문

Programming Language/Python

[Python] startwith, startend

Software Engineer Kim 2021. 6. 23. 09:21

접두사 찾기

  • 파이썬에서 문자열의 접두사를 찾으려면 어떻게 해야 할까요?
  • 슬라이싱을 이용하는 방법이 있지만, 이는 좋은 방법이 아닙니다.
  • 문자열의 접두사를 찾기 위해서는 문자열 슬라이싱 대신 startswith() 함수를 사용하는 것이 좋습니다.
  • startswith()를 이용하면 인덱스를 지정할 필요가 없어 깨끗하고 오류 발생 가능성이 낮습니다.

문자열의 접두사와 접미사를 찾는 함수

  • str.startwith("접두사")
    • str문자열에서 "접두사"가 있으면 True, 없으면 False 반환
  • str.startend("접미사")
    • str문자열에서 "접미사"가 있으면 True, 없으면 False 반환
room = "bedroom"

if room.startswith("bath"):  
print("욕실입니다.")  
elif room.startswith("bed"):  
print("침실입니다.")  
elif room.startswith("living"):  
print("거실입니다.")  
Comments