๐ Python/๊ธฐ์ด
[Python] strip(), rstrip(), lstrip() ์ฌ์ฉ ๋ฐฉ๋ฒ ๋ฐ ์์
ใ
ใ
ใ
ใ
2022. 10. 31. 00:00
728x90
ํ์ด์ฌ์ strip()ํจ์๋ฅผ ์ด์ฉํ๋ฉด ๋ฌธ์์ ๊ณต๋ฐฑ์ ์์จ ์ ์๋ค.
- strip([chars]) :์ธ์๋ก ์ ๋ฌ๋ ๋ฌธ์๋ฅผ String์ ์ผ์ชฝ๊ณผ ์ค๋ฅธ์ชฝ์์ ์ ๊ฑฐ
- lstrip([chars]) : ์ธ์๋ก ์ ๋ฌ๋ ๋ฌธ์๋ฅผ String์ ์ผ์ชฝ์์ ์ ๊ฑฐ
- rstrip([chars]) : ์ธ์๋ก ์ ๋ฌ๋ ๋ฌธ์๋ฅผ String์ ์ค๋ฅธ์ชฝ์์ ์ ๊ฑฐ
1. ๊ณต๋ฐฑ ์ ๊ฑฐ
: strip() ํจ์ ์์ ์๋ฌด ์ธ์๋ ์ ๋ฌํ์ง ์์ผ๋ฉด ๊ณต๋ฐฑ์ ์ ๊ฑฐํจ
text = ' Water boils at 100 degrees '
print('[' + text.rstrip() + ']')
print('[' + text.lstrip() + ']')
print('[' + text.strip() + ']')
# result
[ Water boils at 100 degrees]
[Water boils at 100 degrees ]
[Water boils at 100 degrees]
2. ๋์ผํ ๋ฌธ์ ์ ๊ฑฐ
: ์ธ์๋ก ๋ฌธ์ ํ๋๋ฅผ ์ ๋ฌํ๋ฉด ๋์ผํ์ง ์์ ๋ฌธ์๊ฐ ๋์ฌ๋ ๊น์ง ์ ๊ฑฐํจ
text = '0000000Water boils at 100 degrees 000'
print(text.lstrip('0'))
print(text.rstrip('0'))
print(text.strip('0'))
# result
Water boils at 100 degrees 000
0000000Water boils at 100 degrees
Water boils at 100 degrees
3. ์ฌ๋ฌ ๋ฌธ์ ์ ๊ฑฐ
: ์ธ์๋ก ์ฌ๋ฌ ๋ฌธ์๋ฅผ ์ ๊ฑฐํ ๊ฒฝ์ฐ์๋ ๋์ผํ์ง ์์ ๋ฌธ์๊ฐ ๋์ฌ ๋ ๊น์ง ์ ๊ฑฐํจ
text = ",,,,,123.....water....pp"
print(text.lstrip(',123.p'))
print(text.rstrip(',123.p'))
print(text.strip(',123.p'))
# result
water....pp
,,,,,123.....water
water
728x90