λ‘œμΌ“πŸΎ
article thumbnail

 

μ΄λ²ˆκΈ€μ—μ„œλŠ” νŒŒμ΄μ¬μ—μ„œ 자주 μ‚¬μš©ν•˜λŠ” λ¬Έμžμ—΄ ν•¨μˆ˜λ“€μ„ μ •λ¦¬ν•΄λ³΄κ² μŠ΅λ‹ˆλ‹€.

 

 


# λŒ€λ¬Έμžμ™€ μ†Œλ¬Έμžλ‘œ λ³€ν™˜ν•˜κΈ°

 

ν•¨μˆ˜ μ„€λͺ… μ‚¬μš©λ²•
upper() μ†Œλ¬Έμžλ₯Ό λŒ€λ¬Έμžλ‘œ λ³€ν™˜ν•©λ‹ˆλ‹€ string.upper()
lower() λŒ€λ¬Έμžλ₯Ό μ†Œλ¬Έμžλ‘œ λ³€ν™˜ν•©λ‹ˆλ‹€ string.lower()
swapcase() λŒ€λ¬ΈμžλŠ” μ†Œλ¬Έμžλ‘œ, μ†Œλ¬ΈμžλŠ” λŒ€λ¬Έμžλ‘œ λ³€ν™˜ν•©λ‹ˆλ‹€ string.swapcase()
title() 각 λ‹¨μ–΄μ˜ μ•žκΈ€μžλ§Œ λŒ€λ¬Έμžλ‘œ λ³€ν™˜ν•©λ‹ˆλ‹€. λ§Œμ•½ 각 단어 쀑간에 λŒ€λ¬Έμžκ°€ μžˆλ‹€λ©΄ μ†Œλ¬Έμžλ‘œ λ³€ν™˜λ©λ‹ˆλ‹€. string.title()

 

inputStr = "ByungJun And Jiwon"

upperStr = inputStr.upper()
print(upperStr)#BYUNGJUN AND JIWON

lowerStr = inputStr.lower()
print(lowerStr)#byungjun and jiwon

swapcaseStr = inputStr.swapcase()
print(swapcaseStr)#bYUNGjUN aND jIWON

titleStr = inputStr.title()
print(titleStr)#Byungjun And Jiwon


 


# λ¬Έμžμ—΄ μ°ΎκΈ°

 

ν•¨μˆ˜ μ„€λͺ… μ‚¬μš©λ²•
count() λ¬Έμžμ—΄μ—μ„œ "찾을 λ¬Έμžμ—΄"이 λͺ‡ 개 μžˆλŠ”μ§€ 개수λ₯Ό μ…‰λ‹ˆλ‹€ string.count("찾을 λ¬Έμžμ—΄")
find() λ¬Έμžμ—΄μ—μ„œ "찾을 λ¬Έμžμ—΄"이 μ™Όμͺ½(0)λΆ€ν„° μ‹œμž‘ν•΄μ„œ λͺ‡λ²ˆμ§Έμ— μœ„μΉ˜ν•˜λŠ”μ§€ μ•Œλ €μ€λ‹ˆλ‹€ string.find("찾을 λ¬Έμžμ—΄")
rfind()  find()와 λ‹€λ₯΄κ²Œ λ°˜λŒ€νŽΈμ—μ„œλΆ€ν„° μ°ΎμŠ΅λ‹ˆλ‹€ string.rfind("찾을 λ¬Έμžμ—΄")
index() find()와 κΈ°λŠ₯이 λ™μΌν•©λ‹ˆλ‹€. λ‹€λ§Œ ν•΄λ‹Ή λ¬Έμžμ—΄μ΄ μ—†μœΌλ©΄ -1λ₯Ό λ°˜ν™˜ν•˜λŠ” find와 달리 였λ₯˜κ°€ λ°œμƒν•©λ‹ˆλ‹€ string.index("찾을 λ¬Έμžμ—΄")
rindex() index()와 λ‹€λ₯΄κ²Œ λ°˜λŒ€νŽΈμ—μ„œλΆ€ν„° μ°ΎμŠ΅λ‹ˆλ‹€ string.rindex("찾을 λ¬Έμžμ—΄")
startwith() "찾을 λ¬Έμžμ—΄"λ‘œλΆ€ν„° λ¬Έμžμ—΄μ΄ μ‹œμž‘λ˜λ©΄ True μ•„λ‹ˆλ©΄ Falseλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€. string.startswith("찾을 λ¬Έμžμ—΄")
endswith() "찾을 λ¬Έμžμ—΄"이 λ¬Έμžμ—΄μ˜ λ§ˆμ§€λ§‰μ΄λ©΄ True μ•„λ‹ˆλ©΄ Falseλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€ string.endswith("찾을 λ¬Έμžμ—΄")

 

inputStr = "μ•„κΈ° νŒλ‹€μ™€ μ–΄λ₯Έ νŒλ‹€λŠ” λ‘˜ λ‹€ νŒλ‹€μž…λ‹ˆλ‹€"

print(inputStr.count("νŒλ‹€"))
#3
print(inputStr.find("νŒλ‹€"), inputStr.rfind("νŒλ‹€"), inputStr.find("νŒλ‹€", 6), inputStr.find("뢈곰"))
#3 18 10 -1
print(inputStr.index("νŒλ‹€"), inputStr.rindex("νŒλ‹€"), inputStr.index("νŒλ‹€", 6))
#3 18 10
print(inputStr.startswith("μ•„κΈ°"), inputStr.startswith("μ‚¬μž"), inputStr.startswith("νŒλ‹€", 3), inputStr.endswith("νŒλ‹€μž…λ‹ˆλ‹€"))
#True False True True

 


# λ¬Έμžμ—΄ 곡백 μ‚­μ œ

 

ν•¨μˆ˜ μ„€λͺ… μ‚¬μš©λ²•
strip() μ–‘ μͺ½μ˜ 곡백을 μ‚­μ œν•΄μ€λ‹ˆλ‹€. μ£Όμ˜ν•  점은 λ¬Έμžμ—΄ 쀑간에 μžˆλŠ” 곡백은 μ‚­μ œν•΄μ£Όμ§€ μ•ŠμŠ΅λ‹ˆλ‹€. string.strip()
rstrip() 였λ₯Έμͺ½ 곡백을 μ‚­μ œν•΄μ€λ‹ˆλ‹€. μ£Όμ˜ν•  점은 λ¬Έμžμ—΄ 쀑간에 μžˆλŠ” 곡백은 μ‚­μ œν•΄μ£Όμ§€ μ•ŠμŠ΅λ‹ˆλ‹€. string.rstrip()
lstrip() μ™Όμͺ½ 곡백을 μ‚­μ œν•΄μ€λ‹ˆλ‹€. μ£Όμ˜ν•  점은 λ¬Έμžμ—΄ 쀑간에 μžˆλŠ” 곡백은 μ‚­μ œν•΄μ£Όμ§€ μ•ŠμŠ΅λ‹ˆλ‹€. string.lstrip()
replace() "κΈ°μ‘΄ λ¬Έμžμ—΄"을 "μƒˆ λ¬Έμžμ—΄"둜 λ³€κ²½ν•΄μ€λ‹ˆλ‹€. string.replace("κΈ°μ‘΄ λ¬Έμžμ—΄","μƒˆ λ¬Έμžμ—΄")

 

inputStr = "     μ•„κΈ° νŒλ‹€μ™€ μ–΄λ₯Έ νŒλ‹€λŠ” λ‘˜ λ‹€ νŒλ‹€μž…λ‹ˆλ‹€     "

replaceStr = inputStr.replace("νŒλ‹€", "μ‚¬μž")
print(replaceStr)
#     μ•„κΈ° μ‚¬μžμ™€ μ–΄λ₯Έ μ‚¬μžλŠ” λ‘˜ λ‹€ μ‚¬μžμž…λ‹ˆλ‹€

stripStr = inputStr.strip()
print(stripStr)
#μ•„κΈ° νŒλ‹€μ™€ μ–΄λ₯Έ νŒλ‹€λŠ” λ‘˜ λ‹€ νŒλ‹€μž…λ‹ˆλ‹€

rstripStr = inputStr.rstrip()
print(rstripStr)
#     μ•„κΈ° νŒλ‹€μ™€ μ–΄λ₯Έ νŒλ‹€λŠ” λ‘˜ λ‹€ νŒλ‹€μž…λ‹ˆλ‹€

lstripStr = inputStr.lstrip()
print(lstripStr)
#μ•„κΈ° νŒλ‹€μ™€ μ–΄λ₯Έ νŒλ‹€λŠ” λ‘˜ λ‹€ νŒλ‹€μž…λ‹ˆλ‹€

 


# λ¬Έμžμ—΄ 뢄리

 

ν•¨μˆ˜ μ„€λͺ… μ‚¬μš©λ²•
split() λ¬Έμžμ—΄μ„ κ³΅λ°±μ΄λ‚˜ λ‹€λ₯Έ 문자둜 뢄리해 리슀트둜 λ°˜ν™˜ν•©λ‹ˆλ‹€ string.split()
string.split("뢄리할 문자")
join() μ˜ˆμ‹œλ₯Ό 직접 λ³΄λŠ”κ²Œ 더 이해가 μ‰¬μšΈκ²ƒ κ°™μŠ΅λ‹ˆλ‹€ string.join("joinν•  λ¬Έμžμ—΄")

 

inputStr = "μ•„κΈ° νŒλ‹€μ™€ μ–΄λ₯Έ νŒλ‹€λŠ” λ‘˜ λ‹€ νŒλ‹€μž…λ‹ˆλ‹€"

splitStr = inputStr.split()
print(splitStr)
#['μ•„κΈ°', 'νŒλ‹€μ™€', 'μ–΄λ₯Έ', 'νŒλ‹€λŠ”', 'λ‘˜', 'λ‹€', 'νŒλ‹€μž…λ‹ˆλ‹€']

splitPandaStr = inputStr.split("νŒλ‹€")
print(splitPandaStr)
#['μ•„κΈ° ', '와 μ–΄λ₯Έ ', 'λŠ” λ‘˜ λ‹€ ', 'μž…λ‹ˆλ‹€']

joinChar = '%'
joinStr = joinChar.join(inputStr)
print(joinStr)
#μ•„%κΈ°% %판%λ‹€%와% %μ–΄%λ₯Έ% %판%λ‹€%λŠ”% %λ‘˜% %λ‹€% %판%λ‹€%μž…%λ‹ˆ%λ‹€

joinList = inputStr.split()
joinStr2 = "".join(joinList)
print(joinStr2)
#μ•„κΈ° νŒλ‹€μ™€ μ–΄λ₯Έ νŒλ‹€λŠ” λ‘˜ λ‹€ νŒλ‹€μž…λ‹ˆλ‹€

 

join에 λŒ€ν•œ μ„€λͺ…을 λ§λΆ™μ΄μžλ©΄

 

joinStr을 보면 각 λ¬Έμžλ§ˆλ‹€ % κ°€ 뢙은 κ±Έ λ³΄μ‹€μˆ˜ μžˆμŠ΅λ‹ˆλ‹€. 곡백도 문자둜 μ·¨κΈ‰λ˜κΈ° λ•Œλ¬Έμ— "κΈ°% % 판%" μ΄λŸ°μ‹μœΌλ‘œ 좜λ ₯λ©λ‹ˆλ‹€.

 

joinStr2의 경우 λ¬Έμžμ—΄μ„ split()을 μ΄μš©ν•΄ 리슀트둜 λ§Œλ“  λ‹€μŒ "".join()을 μ΄μš©ν•˜μ—¬ λ‹€μ‹œ λ¬Έμžμ—΄λ‘œ λ³€ν™˜μ‹œμΌœμ€λ‹ˆλ‹€.

 


# ν•¨μˆ˜λͺ…에 λŒ€μž…ν•˜κΈ° : map() ν•¨μˆ˜

 

ν•¨μˆ˜ μ„€λͺ… μ‚¬μš©λ²•
map() map(ν•¨μˆ˜λͺ…, 리슀트λͺ…) ν•¨μˆ˜λŠ” λ¦¬μŠ€νŠΈκ°’ ν•˜λ‚˜ν•˜λ‚˜λ₯Ό ν•¨μˆ˜λͺ…에 λŒ€μž…ν•œλ‹€ map("ν•¨μˆ˜λͺ…","리슀트λͺ…")

 

def myFunc(num):
    return num + 10

inputList = [10,20,30]

outputList = list(map(myFunc, inputList))
print(outputList)
#[20, 30, 40]

inputList2 = ["10","20","30"]
outputList2 = list(map(int, inputList2))
print(outputList2)
#[10, 20, 30]

 

outputList2λ₯Ό μ„€λͺ…ν•˜μžλ©΄,

 

map의 첫번째 int(λ¬Έμžμ—΄μ„ 숫자둜 λ³€ν™˜ν•΄μ£ΌλŠ” ν•¨μˆ˜)λŠ” λ‹€μŒ int("10"), int("20"), int("30")κ³Ό κ°™μŠ΅λ‹ˆλ‹€.

 


# λ¬Έμžμ—΄ ꡬ성 νŒŒμ•…ν•˜κΈ° 

 

ν•¨μˆ˜ μ„€λͺ… μ‚¬μš©λ²•
isdigit() 숫자둜만 κ΅¬μ„±λ˜μ–΄ μžˆλŠ”μ§€ ν™•μΈν•΄μ€λ‹ˆλ‹€ string.isdigit()
isalpha() 문자둜만 κ΅¬μ„±λ˜μ–΄ μžˆλŠ”μ§€ ν™•μΈν•΄μ€λ‹ˆλ‹€ string.isalpha()
isalnum() 숫자,문자둜 κ΅¬μ„±λ˜μ–΄ μžˆλŠ”μ§€ ν™•μΈν•΄μ€λ‹ˆλ‹€ string.isalnum()
islower() μ†Œλ¬Έμžλ‘œλ§Œ κ΅¬μ„±λ˜μ–΄ μžˆλŠ”μ§€ ν™•μΈν•΄μ€λ‹ˆλ‹€ string.islower()
isupper() λŒ€λ¬Έμžλ‘œλ§Œ κ΅¬μ„±λ˜μ–΄ μžˆλŠ”μ§€ ν™•μΈν•΄μ€λ‹ˆλ‹€ string.isupper()
isspace() 곡백으둜만 κ΅¬μ„±λ˜μ–΄ μžˆλŠ”μ§€ ν™•μΈν•΄μ€λ‹ˆλ‹€ string.isspace()

 

onlyNum = "1231231231"
AlNum = "123Test"
onlyAl ="Test"

print(onlyNum.isdigit())#True
print(onlyNum.isalpha())#False
print(onlyNum.isalnum())#True

print(AlNum.isdigit())#False
print(AlNum.isalpha())#False
print(AlNum.isalnum())#True

print(onlyAl.isdigit())#False
print(onlyAl.isalpha())#True
print(onlyAl.isalnum())#True

onlyLower = "test"
onlyUpper = "TEST"
LowerAndUpper = "testTEST"
onlySpace = "      "

print("---------------")

print(onlyLower.islower())#True
print(onlyLower.isupper())#False
print(onlyLower.isspace())#False

print(onlyUpper.islower())#False
print(onlyUpper.isupper())#True
print(onlyUpper.isspace())#False

print(LowerAndUpper.islower())#False
print(LowerAndUpper.isupper())#False
print(LowerAndUpper.isspace())#False

print(onlySpace.islower())#False
print(onlySpace.isupper())#False
print(onlySpace.isspace())#True

 

이상 νŒŒμ΄μ¬μ—μ„œ 자주 μ‚¬μš©ν•˜λŠ” λ¬Έμžμ—΄ κ΄€λ ¨ ν•¨μˆ˜ μ •λ¦¬μ˜€μŠ΅λ‹ˆλ‹€.

 

μ½μ–΄μ£Όμ…”μ„œ κ°μ‚¬ν•©λ‹ˆλ‹€.μ§ˆλ¬Έμ€ μ–Έμ œλ‚˜ ν™˜μ˜ν•©λ‹ˆλ‹€.
"λ‚œ λ°˜λ“œμ‹œ λ°±μ—”λ“œ 왕이 될거야"

 

profile on loading

Loading...