๐String
๋ฌธ์, ๋จ์ด ๋ฑ์ผ๋ก ๊ตฌ์ฑ๋ ๋ฌธ์๋ค์ ๋์ด
- ํฐ๋ฐ์ดํ
"", ์์๋ฐ์ดํ''๋ก ๋๋ฌ์์ฌ ์์
๐๋ฌธ์์ด์ ๊ดํ ์ ๋ณด๋ฅผ ์ ์ ์๋ ํจ์
๐len(str)
๋ฌธ์์ด์ ๊ธธ์ด๋ฅผ returnํ๋ ํจ์
words = "hello world"
len(words) # 11๐count(v)
๋ฌธ์์ด์์ ์ฐพ๊ณ ์ ํ๋ ๋ฌธ์์ ๊ฐ์๋ฅผ returnํ๋ ํจ์
words = "hello world"
words.count('l') # 3๐find(v)
๋ฌธ์์ด์์ ์ฐพ๊ณ ์ ํ๋ ๋ฌธ์์ index๋ฅผ returnํ๋ ํจ์
words = "hello world"
words.find('l') # 2- ์ฐพ๊ณ ์ ํ๋ ๋ฌธ์๊ฐ ์ฌ๋ฌ ๊ฐ์ผ ๊ฒฝ์ฐ, ๊ฐ์ฅ ์ฒ์ ๋ฐ๊ฒฌ๋ ์ธ๋ฑ์ค๋ฅผ return
- ์ผ์นํ๋ ๋ฌธ์๊ฐ ์์ ๊ฒฝ์ฐ -1 return
๐๋ฌธ์์ ๊ดํ ์ ๋ณด๋ฅผ ์ ์ ์๋ ํจ์
- 3๊ฐ์ ํจ์ ๋ชจ๋ ํน์๋ฌธ์, ๊ณต๋ฐฑ์ด ์์ ์ False์ return
๐isdigit()
๋ฌธ์์ด์ด ์ซ์๋ก๋ง ๊ตฌ์ฑ๋์ด ์๋์ง ์๋ ค์ฃผ๋ ํจ์
words = "he11o world"
words.isdigit() # False๐ isalpha()
๋ฌธ์์ด์ด ์ํ๋ฒณ๋ก๋ง ๊ตฌ์ฑ๋์ด ์๋์ง ์๋ ค์ฃผ๋ ํจ์
words = "He11oWorld"
words.isalpha() # False๐ isalnum()
๋ฌธ์์ด์ด ์ํ๋ฒณ ํน์ ์ซ์๋ก๋ง ๊ตฌ์ฑ๋์ด ์๋์ง ์๋ ค์ฃผ๋ ํจ์
words = "HelloWorld"
words.isalnum() # True๐ startswith()
๋ฌธ์์ด์ด ํน์ ๋ฌธ์ ๋๋ ๋ฌธ์์ด๋ก ์์ํ๋์ง ์๋ ค์ฃผ๋ ํจ์
words = "He11oWorld"
words.startswith("He") # True- ๊ณต๋ฐฑ์ ์ทจ๊ธํจ
๐ endswith()
๋ฌธ์์ด์ด ํน์ ๋ฌธ์ ๋๋ ๋ฌธ์์ด๋ก ๋๋๋์ง ์๋ ค์ฃผ๋ ํจ์
words = "he11o world"
words.startswith("d ") # False- ๊ณต๋ฐฑ์ ์ทจ๊ธํจ
๐๋ฌธ์์ด์ ์กฐ์ํ๋ ํจ์
๐split()
๐replace(v, nv)
๋ฌธ์์ด ์์ ํน์ ํ ๊ฐ์ ๋ค๋ฅธ ๊ฐ์ผ๋ก ์นํํ๋ ํจ์
words = "hello world"
words.replace('hello', 'hi') # "hi world"- ๋ฐ๋ ๊ฒฐ๊ณผ๋ง returnํ๋ ๊ฒ (์ค์ ๋ก ๋ณ๊ฒฝ X)
- find()ํจ์์๋ ๋ค๋ฅด๊ฒ ํด๋นํ๋ ๋ชจ๋ ๊ฐ์ ์นํ
๐ re.sub(pattern, replace, str, cnt)
๋ฌธ์์ด์์ ์ ๊ท ํํ์ ํจํด์ ํด๋นํ๋ ๊ฐ์ ๋ค๋ฅธ ๊ฐ์ผ๋ก ์นํํ๋ ์ ๊ท ํํ์ ํจ์
๋ค์ํ ์ ๊ท ํํ์ ํจํด ํํ
| ํจํด | ์ค๋ช |
|---|---|
| ^ | ์ด ํจํด์ผ๋ก ์์ํด์ผ ํจ |
| $ | ์ด ํจํด์ผ๋ก ์ข ๋ฃ๋์ด์ผ ํจ |
| [chars] | ๋ฌธ์๋ค ์ค ํ๋์ฌ์ผ ํจ ๊ฐ๋ฅํ ๋ฌธ์๋ค์ ์งํฉ์ ์ ์ |
| [^chars] | ์๋์ด์ผ ํ ๋ฌธ์๋ค์ ์งํฉ์ ์ ์ |
| [a-d] | ํด๋น ๋ฌธ์ ์ฌ์ด ๋ฒ์์ ์ํ๋ ๋ฌธ์๋ค์ ์งํฉ์ ์ ์ |
| [^a-d] | ํด๋น ๋ฌธ์ ์ฌ์ด ๋ฒ์์ ์ํ์ง ์๋ ๋ฌธ์๋ค์ ์งํฉ์ ์ ์ |
๐strip([chars])
์ธ์์ ๋์ผํ ๋ฌธ์๊ฐ ๋์ค์ง ์์ ๋๊น์ง ์ธ์๋ก ์ ๋ฌ๋ ๋ฌธ์๋ฅผ ์ ๋์์ ์ ๊ฑฐํ๋ ํจ์
words = "hello world"
words.strip('held') # "o wor"- ์ธ์๊ฐ ์์ ์ ์ ๋์ ๊ณต๋ฐฑ ์ ๊ฑฐ
- ๐จ์ ๋ฌ๋๋ ์ธ์๊ฐ ๋ฌธ์์ด์ด ์๋๋ผ ๋ฌธ์ ํด๋์ค
[chars]