찾기와 바꾸기

Python 정규 표현식

Maria Eugenia Inzaugarat

Data scientist

부분 문자열 찾기

  • 대상 문자열에서 지정한 부분 문자열을 검색합니다.

my_string = "Where's Waldo?"
my_string.find("Waldo")
8
my_string.find("Wenda")
-1
Python 정규 표현식

부분 문자열 찾기

  • 대상 문자열에서 지정한 부분 문자열을 검색합니다.

my_string = "Where's Waldo?"
my_string.find("Waldo", 0, 6)
-1
Python 정규 표현식

인덱스 함수

  • .find()와 유사하게, 대상 문자열에서 지정한 부분 문자열을 검색합니다.

my_string = "Where's Waldo?"
my_string.index("Waldo")
8
my_string.index("Wenda")
File "<stdin>", line 1, in <module> 
ValueError: substring not found
Python 정규 표현식

인덱스 함수

  • .find()와 유사하게, 대상 문자열에서 지정한 부분 문자열을 검색합니다.

my_string = "Where's Waldo?"
try:
    my_string.index("Wenda")
except ValueError:
      print("Not found")
"Not found"
Python 정규 표현식

발생 횟수 세기

  • 지정한 부분 문자열의 발생 횟수를 반환합니다.

my_string = "How many fruits do you have in your fruit basket?"
my_string.count("fruit")
2
my_string.count("fruit", 0, 16)
1
Python 정규 표현식

부분 문자열 바꾸기

  • 부분 문자열이 나타나는 부분을 새로운 부분 문자열로 바꿉니다.

my_string = "The red house is between the blue house and the old house"
print(my_string.replace("house", "car"))
The red car is between the blue car and the old car
print(my_string.replace("house", "car", 2))
The red car is between the blue car and the old house
Python 정규 표현식

마무리

  • 문자열 조작:

    • 슬라이싱과 연결
    • 대소문자 조정
    • 분할과 결합
    • 시작과 끝 문자 제거
    • 부분 문자열 찾기
    • 발생 횟수 세기
    • 부분 문자열 바꾸기
Python 정규 표현식

연습해 봅시다!

Python 정규 표현식

Preparing Video For Download...