๐Ÿ—๏ธ Algorithm/๐Ÿ”ท Python

[Python] ํŒŒ์ด์ฌ ํŠน์ • ๋ฌธ์ž ์ฐพ๊ธฐ (find, startswith, endswith)

Dbswnstjd 2022. 3. 21. 09:39

์ฝ”๋”ฉํ…Œ์ŠคํŠธ๋ฅผ ์ค€๋น„ํ•˜๋ฉด์„œ ๋งŽ์€ ๋‚ด์žฅ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด์„œ ์ •๋ฆฌ๋ฅผ ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.

 

๋ฌธ์ž์—ด ์•ˆ์—์„œ ํŠน์ •ํ•œ ๋ฌธ์ž๋ฅผ ์ฐพ๊ฑฐ๋‚˜(find) ํŠน์ • ๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ฌธ์ž์—ด(startswith), ํŠน์ • ๋ฌธ์ž๋กœ ๋๋‚˜๋Š” ๋ฌธ์ž์—ด(endswith) 

 

  • find('string', location)
s = 'abcdefg'
s.find('b') # 1
s.find('a') # 0
s.find('a', 3) # -1

find : ๋ฌธ์ž์—ด ์ค‘์— ํŠน์ • ๋ฌธ์ž๋ฅผ ์ฐพ๊ณ  ์œ„์น˜๋ฅผ ๋ฐ˜ํ™˜ / ์—†์„ ๊ฒฝ์šฐ -1 ๋ฆฌํ„ด

  • startswith('start string', start location)
s = 'abcdefg'
s.startswith('a') # True
s.startswith('b') # False
s.startswith('c', s.find('c')) # True
s.startswith('c', 1) # False

startswith: ๋ฌธ์ž์—ด์ด ํŠน์ • ๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๋Š”์ง€ ํ™•์ธ

return True or False

 

๋‘ ๋ฒˆ์งธ ์ธ์ž์— ๋“ค์–ด๊ฐ€๋Š” ๊ฐ’์— ๋”ฐ๋ผ ์‹œ์ž‘ ์ง€์ ์ด ๋ฐ”๋€œ

  • endswith('end string', start string index, end string index)
    s = 'abcdefg'
    s.endswith('c') # False
    s.endswith('g') # True
    s.endswith('c', 0, 10) # False
    s.endswith('c', 0, 3) # True

endswith : ๋ฌธ์ž์—ด์ด ํŠน์ • ๋ฌธ์ž๋กœ ๋๋‚˜๋Š”์ง€ ํ™•์ธ

return True or False

 

๋‘ ๋ฒˆ์งธ ์ธ์ž๋กœ ๋ฌธ์ž์—ด์˜ ์‹œ์ž‘ ์„ธ ๋ฒˆ์งธ ์ธ์ž๋กœ ๋ฌธ์ž์—ด์˜ ๋ ์ง€์ ์„ ์„ค์ • ๊ฐ€๋Šฅ

'๐Ÿ—๏ธ Algorithm > ๐Ÿ”ท Python' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Python] ํŒŒ์ด์ฌ ์šฐ์„ ์ˆœ์œ„ ํ(Priority Queue)  (0) 2023.03.22