Guidelines

find()와 rfind()둜 νŠΉμ • 문자의 μœ„μΉ˜ μ°ΎκΈ°

"banana"λΌλŠ” λ¬Έμžμ—΄μ—μ„œ "a"λΌλŠ” λ¬Έμžκ°€ λͺ‡ λ²ˆμ§Έμ— μžˆλŠ”μ§€ 찾으렀면 μ–΄λ–»κ²Œ ν• κΉŒμš”?

find()와 rfind() ν•¨μˆ˜λŠ” λ¬Έμžμ—΄ λ‚΄μ—μ„œ νŠΉμ • λ¬Έμžλ‚˜ λ¬Έμžμ—΄μ˜ μœ„μΉ˜λ₯Ό μ°ΎμŠ΅λ‹ˆλ‹€.


find() ν•¨μˆ˜ μ‚¬μš©λ²•

find() ν•¨μˆ˜λŠ” λ¬Έμžμ—΄μ˜ μ‹œμž‘(μ™Όμͺ½)λΆ€ν„° νŠΉμ • 문자 및 λ¬Έμžμ—΄μ„ μ°Ύμ•„ κ·Έ μœ„μΉ˜μ˜ 인덱슀(Index) 번호λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.

μΈλ±μŠ€λž€? : ν”„λ‘œκ·Έλž˜λ°μ—μ„œ μΈλ±μŠ€λŠ” μˆœμ„œλ₯Ό λ‚˜νƒ€λ‚΄λŠ” 번호λ₯Ό λœ»ν•©λ‹ˆλ‹€. μ—¬λŸ¬ 문자둜 κ΅¬μ„±λœ λ¬Έμžμ—΄μ—μ„œ 각 λ¬ΈμžλŠ” 인덱슀λ₯Ό 가지고 있으며, μΈλ±μŠ€λŠ” 0λΆ€ν„° μ‹œμž‘ν•©λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄, λ¬Έμžμ—΄ "hello"μ—μ„œ "h"의 μΈλ±μŠ€λŠ” 0, "e"의 μΈλ±μŠ€λŠ” 1μž…λ‹ˆλ‹€.

λ¬Έμžμ—΄ λ‚΄ 곡백도 μΈλ±μŠ€μ— ν¬ν•¨λ˜λ©°, λ¬Έμžμ—΄ λ‚΄ μ°ΎλŠ” λ¬Έμžμ—΄μ΄ μ—¬λŸ¬ 번 λ‚˜νƒ€λ‚  경우 처음 λ‚˜νƒ€λ‚˜λŠ” μœ„μΉ˜μ˜ 인덱슀λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.

μ°ΎλŠ” λ¬Έμžλ‚˜ λ¬Έμžμ—΄μ΄ μ—†μœΌλ©΄ -1을 λ°˜ν™˜ν•©λ‹ˆλ‹€.

find() ν•¨μˆ˜ μ‚¬μš© μ˜ˆμ‹œ
text = "파이썬 μˆ˜μ—…, μˆ˜μ—…μ€ μž¬λ―Έμžˆλ‹€" # text λ¬Έμžμ—΄ λ³€μˆ˜μ˜ μ‹œμž‘λΆ€ν„° "μˆ˜μ—…" λ¬Έμžμ—΄μ˜ μ‹œμž‘ μœ„μΉ˜ λ°˜ν™˜ position = text.find("μˆ˜μ—…") # μ™Όμͺ½μ—μ„œ 첫 번째 "μˆ˜μ—…" λ¬Έμžμ—΄μ˜ μ‹œμž‘ μœ„μΉ˜ λ°˜ν™˜ print(position)

text둜 μ œμ‹œλœ "파이썬 μˆ˜μ—…, μˆ˜μ—…μ€ μž¬λ―Έμžˆλ‹€" λ¬Έμžμ—΄μ—μ„œ μΈλ±μŠ€λ³„ 문자λ₯Ό λ‚˜νƒ€λ‚΄λ©΄ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

λ¬Έμžμ—΄ 인덱슀
0: '파' 1: '이' 2: '썬' 3: ' ' 4: '수' 5: 'μ—…' 6: ',' 7: ' ' 8: '수' 9: 'μ—…' 10: '은' ...

μœ„ μ½”λ“œμ—μ„œ find() ν•¨μˆ˜λŠ” λ¬Έμžμ—΄ textμ—μ„œ "μˆ˜μ—…" λ¬Έμžμ—΄μ„ μ™Όμͺ½λΆ€ν„° μ°ΎμŠ΅λ‹ˆλ‹€.

"μˆ˜μ—…" λ¬Έμžμ—΄μ€ 4번째 μΈλ±μŠ€λΆ€ν„° μ‹œμž‘ν•˜λ―€λ‘œ, find() ν•¨μˆ˜λŠ” "μˆ˜μ—…" λ¬Έμžμ—΄μ˜ μ‹œμž‘ μœ„μΉ˜μΈ 4λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.


rfind() ν•¨μˆ˜ μ‚¬μš©λ²•

rfind() ν•¨μˆ˜λŠ” λ¬Έμžμ—΄μ˜ 였λ₯Έμͺ½(끝)λΆ€ν„° νŠΉμ • 문자 및 λ¬Έμžμ—΄μ„ μ°Ύμ•„ κ·Έ μœ„μΉ˜μ˜ 인덱슀λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.

μ°ΎλŠ” λ¬Έμžλ‚˜ λ¬Έμžμ—΄μ΄ μ—†μœΌλ©΄ -1을 λ°˜ν™˜ν•©λ‹ˆλ‹€.

rfind() ν•¨μˆ˜ μ‚¬μš© μ˜ˆμ‹œ
text = "파이썬 μˆ˜μ—…, μˆ˜μ—…μ€ μž¬λ―Έμžˆλ‹€" # text λ¬Έμžμ—΄ λ³€μˆ˜μ˜ μ‹œμž‘λΆ€ν„° "μˆ˜μ—…" λ¬Έμžμ—΄μ˜ μ‹œμž‘ μœ„μΉ˜ λ°˜ν™˜ position = text.rfind("μˆ˜μ—…") # 였λ₯Έμͺ½μ—μ„œ 첫 번째 "μˆ˜μ—…" λ¬Έμžμ—΄μ˜ μ‹œμž‘ μœ„μΉ˜ λ°˜ν™˜ print(position)

rfind() ν•¨μˆ˜λŠ” λ¬Έμžμ—΄ textμ—μ„œ "μˆ˜μ—…" λ¬Έμžμ—΄μ„ 였λ₯Έμͺ½λΆ€ν„° μ°ΎμŠ΅λ‹ˆλ‹€.

"μˆ˜μ—…" λ¬Έμžμ—΄μ€ 8번째 μΈλ±μŠ€λΆ€ν„° μ‹œμž‘ν•˜λ―€λ‘œ, rfind() ν•¨μˆ˜λŠ” 였λ₯Έμͺ½λΆ€ν„° νƒμƒ‰ν–ˆμ„ λ•Œ "μˆ˜μ—…" λ¬Έμžμ—΄μ˜ μ‹œμž‘ μœ„μΉ˜μΈ 8을 λ°˜ν™˜ν•©λ‹ˆλ‹€.

Mission
0 / 1

rfind() ν•¨μˆ˜λŠ” λ¬Έμžμ—΄μ˜ μ‹œμž‘(μ™Όμͺ½)λΆ€ν„° νŠΉμ • λ¬Έμžμ—΄μ„ μ°Ύμ•„ 인덱슀λ₯Ό λ°˜ν™˜ν•œλ‹€.

O
X

Guidelines

AI Tutor

Publish

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Execution Result