lstrip()
메소드는 문자열의 왼쪽(앞쪽)에서 지정한 문자열을 제거하는 메소드
string = " Hello, World! "
result = string.lstrip() # 앞쪽 공백 제거
print(result) # 출력: "Hello, World! "
string = "###Hello, World!###"
result = string.lstrip("#") # 앞쪽 '#' 제거
print(result) # 출력: "Hello, World!###"
rstrip()
메소드는 문자열의 오른쪽(뒤쪽)에서 지정한 문자열을 제거하는 메소드
string = " Hello, World! "
result = string.rstrip() # 뒤쪽 공백 제거
print(result) # 출력: " Hello, World!"
string = "###Hello, World!###"
result = string.rstrip("#") # 뒤쪽 '#' 제거
print(result) # 출력: "###Hello, World!"
strip()
메소드는 문자열의 양쪽(앞뒤)에서 지정한 문자열을 제거하는 메소드
string = " Hello, World! "
result = string.strip() # 앞뒤 공백 제거
print(result) # 출력: "Hello, World!"
string = "###Hello, World!###"
result = string.strip("#") # 앞뒤 '#' 제거
print(result) # 출력: "Hello, World!"
find()
메소드는 문자열에서 특정 문자열을 찾아 해당 문자열의 첫 번째 인덱스를 반환한다. 만약 문자열에 찾는 문자열이 없을 경우 -1 반환
string = "Hello, World!"
index = string.find("World") # "World"의 첫 번째 인덱스를 찾음
print(index) # 출력: 7
index = string.find("Python") # "Python"은 문자열에 존재하지 않음
print(index) # 출력: -1
startswith()
메소드는 문자열이 특정 문자열로 시작하는지 여부를 체크하여 True 또는 False를 반환한다. 또한 체크할 때, 시작 인덱스를 지정할 수 있다.
string = "Hello, World!"
result = string.startswith("Hello") # "Hello"로 시작하는지 체크
print(result) # 출력: True
result = string.startswith("World") # "World"로 시작하는지 체크
print(result) # 출력: False
result = string.startswith("World", 7) # 인덱스 7부터 "World"로 시작하는지 체크
print(result) # 출력: True
endswith()
메소드는 문자열이 특정 문자열로 끝나는지 여부를 체크하여 True 또는 False를 반환한다. 또한 체크할 때, 시작 인덱스와 끝 인덱스를 지정할 수 있다.
string = "Hello, World!"
result = string.endswith("World!") # "World!"로 끝나는지 체크
print(result) # 출력: True
result = string.endswith("World!", 7, 12) # 인덱스 7부터 12까지 "World!"로 끝나는지 체크
print(result) # 출력: True
result = string.endswith("World!", 7, 11) # 인덱스 7부터 11까지 "World!"로 끝나는지 체크
print(result) # 출력: False