728x90

 


Python String Methods

1) capitalize() : ๋ฌธ์ž์—ด์˜ ์ฒซ ๊ธ€์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜

text: str = "hello"
print(text.capitalize()) # Hello

2) casefold() : ๋ฌธ์ž์—ด์„ ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜

text1: str = 'MARIo'
text2: str = 'maRIO'

print(text1.casefold()) # mario
print(text2.casefold()) # mario
print(text1.casefold() == text2.casefold()) # True

3) center() : ๋ฌธ์ž์—ด์„ ๊ฐ€์šด๋ฐ ์ •๋ ฌ

text: str = "hello"
print(text.center(20)) #        hello        
print(text.center(20, '-')) # -------hello--------

4) count() : ๋ฌธ์ž์—ด์—์„œ ํŠน์ • ๋ฌธ์ž์—ด์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜

text: str = "hello"
print(text.count('l')) # 2

5) encode() : ๋ฌธ์ž์—ด์„ ๋ฐ”์ดํŠธ ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜

text: str = "hello"
print(text.encode()) # b'hello'
print(text.encode(encoding='utf-8', errors='strict')) # b'hello'

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

text: str = "hello"
print(text.endswith(('a','o'))) # True # a or o

7) expandtabs() : ๋ฌธ์ž์—ด์˜ ํƒญ์„ ๊ณต๋ฐฑ์œผ๋กœ ๋ฐ”๊ฟˆ

text: str = "hello\tim\nin\tthe world"
print(text.expandtabs(20))

# hello               im
# in                  the world

8) find() : ๋ฌธ์ž์—ด์—์„œ ํŠน์ • ๋ฌธ์ž์—ด์„ ์ฐพ์•„์„œ ๊ทธ ์œ„์น˜๋ฅผ ๋ฐ˜ํ™˜

text: str = "hello, my name is Mario"
position: int = text.find('M')
print(f"position {position}") # position 18
print(f"{text[position:]}") # Mario # position to end

9) format() : ๋ฌธ์ž์—ด ํฌ๋งทํŒ…

text: str = "hello, my name is {name}, and I am {age} years old" 
print(text.format(name='Mario', age=20))
# hello, my name is Mario, and I am 20 years old

10) format_map() : ๋ฌธ์ž์—ด์˜ ํฌ๋งท์„ ์ง€์ •ํ•˜๋Š” ํ•จ์ˆ˜

text: str = "hello, my name is {name}, and I am {age} years old"
print(text.format_map({'name': 'Mario', 'age': 20}))
# hello, my name is Mario, and I am 20 years old

11) index() : ๋ฌธ์ž์—ด์—์„œ ํŠน์ • ๋ฌธ์ž์—ด์˜ ์œ„์น˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜

text: str = "hello, my name is Mario"
position: int = text.index('Mario')
print(f"position {position}") # position 18
print(f"{text[position:]}") # Mario # position to end

# find์™€ ๋‹ค๋ฅด๊ฒŒ ์ฐพ๋Š” ๋ฌธ์ž์—ด์ด ์—†์„ ๊ฒฝ์šฐ ValueError ๋ฐœ์ƒ
try: 
    text.index('mario')
except Exception as e:
    print(f"{e.__class__.__name__} : {e}") # ValueError : substring not found

12) isalnum() : ๋ฌธ์ž์—ด์ด ์•ŒํŒŒ๋ฒณ ๋˜๋Š” ์ˆซ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜

text1: str = "hello"
text2: str = "hello12!"
print(text1.isalnum()) # True # ๋ฌธ์ž์—ด์ด ์•ŒํŒŒ๋ฒณ ๋˜๋Š” ์ˆซ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋Š”์ง€ ํ™•์ธ
print(text2.isalnum()) # False (!) != string ๋ฌธ์ž์—ด์ด ์•ŒํŒŒ๋ฒณ ๋˜๋Š” ์ˆซ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋Š”์ง€ ํ™•์ธ

13) isalpha() : ๋ฌธ์ž์—ด์ด ์•ŒํŒŒ๋ฒณ์œผ๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋Š”์ง€ ํ™•์ธ

text1: str="hello123"
print(text1.isalpha()) # False # ๋ฌธ์ž์—ด์ด ์•ŒํŒŒ๋ฒณ์œผ๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋Š”์ง€ ํ™•์ธ

14) isascii() : ๋ฌธ์ž์—ด์ด ASCII ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋Š”์ง€ ํ™•์ธ

text1: str = "hello"
text2: str = "hello๐Ÿ˜‡"

print(text1.isascii()) # True
print(text2.isascii()) # False

15) isdecimal() : ๋ฌธ์ž์—ด์ด 10์ง„์ˆ˜๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋Š”์ง€ ํ™•์ธ(์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉ)

text1: str = "โถโทโธ"
print(text1.isdecimal()) # False # ๋ฌธ์ž์—ด์ด 10์ง„์ˆ˜๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋Š”์ง€ ํ™•์ธ

16) isdigit() : ๋ฌธ์ž์—ด์ด ์ˆซ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋Š”์ง€ ํ™•์ธ

text2: str = "โถโทโธ"
print(text2.isdigit()) # True # ๋ฌธ์ž์—ด์ด ์ˆซ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋Š”์ง€ ํ™•์ธ

17) isnumeric() : ๋ฌธ์ž์—ด์ด ์ˆซ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋Š”์ง€ ํ™•์ธ

text1: str = "โถโทโธ"
print(text1.isnumeric()) # True # ๋ฌธ์ž์—ด์ด ์‹๋ณ„์ž๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธ

18) isidentifier() : ๋ฌธ์ž์—ด์ด ์‹๋ณ„์ž๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜

text: str = "์ด ๋ฌธ์ž์—ด๋กœ ๋ณ€์ˆ˜ ์„ ์–ธ ๊ฐ€๋Šฅํ• ๊นŒ?"
print(text.isidentifier()) # False
text: str = "์ด_๋ฌธ์ž์—ด๋กœ_๋ณ€์ˆ˜_์„ ์–ธ_๊ฐ€๋Šฅํ• ๊นŒ"
print(text.isidentifier()) # True

19) islower() : ๋ฌธ์ž์—ด์ด ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋Š”์ง€ ํ™•์ธ

text: str = "hello"
print(text.islower()) # True

text: str = "Hello"
print(text.islower()) # False

20) isprintable() : ๋ฌธ์ž์—ด์ด ์ถœ๋ ฅ ๊ฐ€๋Šฅํ•œ์ง€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜

text: str = "hello\n"
print(text.isprintable()) # False # escape ๋ฌธ์ž์—ด์ด ํฌํ•จ๋  ๊ฒฝ์šฐ False

text: str = "hello\\n"
print(text.isprintable()) # True
๋ฐ˜์‘ํ˜•
๋‹คํ–ˆ๋‹ค