본문 바로가기

Python

[Python]String startswith() Method

PyTorch를 사용하면서 특정 레이어를 지정할 때

'startswith' 함수를 사용하는 것을 자주 보았을것입니다.

위 함수는 문자열이 특정 문자(파라미터로 지정된)로 시작하는지 여부(True, False)를 리턴합니다.

 

Syntax

string.startswith(value, start, end)

 

Parameters

- value(필수): 확인하고자 하는 문자 

- start(옵션): 어디서부터 찾을지 위치에 대한 정수값

- end(옵션): 어디까지 찾을지 위치에 대한 정수값

 

Example

string = "welcome to my blog"
result = string.startswith("welcome")
print(result)

>>> True
string = "welcome to my blog"
result = string.startswith("to", 0, 12)
print(result)

>>> False

# 탐색 시작점 변경
string = "welcome to my blog"
result = string.startswith("to", 8, 12)
print(result)

>>> True