๐Ÿ“„ Python

7. ๋ฌธ์ž์—ด

sa1t 2022. 6. 9. 10:45
๋ฐ˜์‘ํ˜•
####################################### ๋ฌธ์ž์—ด #######################################
# ๋ฌธ์ž์—ด์€ ๋ฆฌ์ŠคํŠธ์™€ ๋น„์Šทํ•œ ์‚ฌ์šฉ ๋ฐฉ์‹
ss = "๊น€๊น€๊น€ ์œค์œค์œค ๊ฐ•๊ฐ•๊ฐ• ๋ฏผ๋ฏผ๋ฏผ"
print((ss[2]))
print(ss[0:5])
# ๋ฆฌ์ŠคํŠธ์™€ ๋™์ผํ•˜๊ฒŒ ์ฒจ์ž์— ์ž๋ฆฌ๊ฐ’ ์ž…๋ ฅํ•˜์—ฌ ์ถœ๋ ฅ

ss = "๊ฐ•๊ฐ•๊ฐ•" + ' ์ง€๊ฐ์Ÿ์ด' + " ์˜ค๋Š˜์€ ์•ˆ์ง€๊ฐ"
print(ss)
ss="ํ–‰๋ณตํ•ด๋ผ"*3
print(ss)

print(len(ss))
# ๊ธธ์ด ๊ฐ’ ์ถœ๋ ฅ

## ๋ฌธ์ž์—ด ๊ฐ ๋ฌธ์ž ๋’ค์— ๋‹ค๋ฅธ ๋ฌธ์ž ์ถœ๋ ฅ
# for + ๋ฌธ์ž์—ด

ss="์ •๋ณด๋ณดํ˜ธ ์ง„๋‹จ ๋ถ„์„ ๋ชจ์˜ํ•ดํ‚น"

for i in range(0,len(ss)):
    print(ss[i]+"#", end='')
print('')

## ๋ฌธ์ž์—ด ์‚ฌ์ด ๊ฐ’ ์ถœ๋ ฅ
a=input("์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ")
b=input("์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ")
c=input("์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ")

print(a,b,c,sep='-')

## ๋ฌธ์ž์—ด ์ž…๋ ฅ ๋ฐ›๊ณ  , ๊ฑฐ๊พธ๋กœ ์ถœ๋ ฅ
inss, outss="",""
count,i=0,0

inss=input("๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•˜์„ธ์š” : ")
count=len(inss)

for i in range(0,count):
    outss += inss[count-(i+1)]
print("๋‚ด์šฉ ๊ฑฐ๊พธ๋กœ ์ถœ๋ ฅ = > %s" %outss)

# Quiz ) while๋ฌธ์œผ๋กœ ๋ณ€๊ฒฝ

inss,outss="",""
count,i=0,0

inss=input("๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•˜์„ธ์š” : ")
count=len(inss)

while i<count :
    outss += inss[count-(i+1)]
    i=i+1

print("๋‚ด์šฉ ๊ฑฐ๊พธ๋กœ ์ถœ๋ ฅ = > %s" %outss)


##### ๋ฌธ์ž์—ด ๊ด€๋ จ ํ•จ์ˆ˜
ss="asdf AasDFd ์•„๊ฐ€๋‚˜๋‹ค"
print(ss.upper())
# ์†Œ๋ฌธ์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ
print(ss.lower())
# ๋Œ€๋ฌธ์ž๋ฅผ ์†Œ๋ฌธ์ž๋กœ
print(ss.swapcase())
# ๋Œ€์†Œ๋ฌธ์ž ์„œ๋กœ ๊ตํ™˜
print(ss.title())
# ๋‹จ์–ด ์•ž๊ธ€์ž๋ฅผ ๋Œ€๋ฌธ์ž
### ์œ„ 4๊ฐœ๋Š” ์˜์–ด๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

ss="ํŒŒ์ด์ฌ ๊ณต๋ถ€๋Š” ์ฆ๊ฒ์Šต๋‹ˆ๋‹ค. ๊ณต๋ถ€๊ฐ€ ํ•ญ์ƒ ๋ฏธ๋ž˜๋‹ค. ์žฌ๋ฏธ์—†๋‹ค."
print(ss.count("๊ณต๋ถ€"))
# ํ•ด๋‹น ๋‹จ์–ด๊ฐ€ ๋ฌธ์ž์—ด์— ๋ช‡๊ฐœ ์žˆ๋Š”์ง€
print(ss.find("๊ณต๋ถ€"))
# ์™ผ์ชฝ๋ถ€ํ„ฐ ๊ฒ€์ƒ‰ ํ•ด๋‹น ๋‹จ์–ด์˜ ์œ„์น˜ ( 0๋ถ€ํ„ฐ ์‹œ์ž‘ )
print(ss.rfind("๊ณต๋ถ€"))
# ์˜ค๋ฅธ์ชฝ๋ถ€ํ„ฐ ๊ฒ€์ƒ‰ ํ•ด๋‹น ๋‹จ์–ด์˜ ์œ„์น˜
print(ss.find("์žˆ๋‹ค"))
# ์กด์žฌํ•˜์ง€ ์•Š์„๋•Œ -1 ์ถœ๋ ฅ


print(ss.index("ํ…Œ์ŠคํŠธ")) # ์˜ค๋ฅ˜ ๋ฐœ์ƒ
print(ss.index("๊ณต๋ถ€"))
# find์™€ ๋™์ผํ•˜๊ฒŒ ํ•ด๋‹น ๋‹จ์–ด์˜ ์œ„์น˜ ๊ฐ’ ์ถœ๋ ฅ
# index ์ฐพ๋Š” ๋‹จ์–ด๊ฐ€ ์—†์„ ๋•Œ ์˜ค๋ฅ˜ ๋ฐœ์ƒ
print(ss.rindex("๊ณต๋ถ€"))
# rfind๋ž‘ ๋™์ผ, ์—†์œผ๋ฉด ์—๋Ÿฌ
print(ss.index("๊ณต๋ถ€",6))
# ( "๋‹จ์–ด" , ๊ฒ€์ƒ‰ ์‹œ์ž‘ ์œ„์น˜ ๊ฐ’ )

print(ss.startswith("ํŒŒ์ด์ฌ"))
# ํ•ด๋‹น ๋‹จ์–ด๋กœ ์‹œ์ž‘ํ•˜๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ Bool๋กœ ์ถœ๋ ฅ
print(ss.startswith("ํŒŒ์ด์ฌ",10))
# ์‹œ์ž‘ํ•˜๋Š” ์œ„์น˜ ๊ฐ’ ์กฐ์ ˆ
print(ss.endswith("์—†๋‹ค."))
# ๋๋‚˜๋Š” ๋‹จ์–ด ์—ฌ๋ถ€๋ฅผ Bool๋กœ ์ถœ๋ ฅ

### ๋ฌธ์ž์—ด์ด ๊ด„ํ˜ธ๋กœ ๊ฐ์‹ธ์ ธ์žˆ์ง€ ์•Š์œผ๋ฉด ๊ฐ์‹ธ์ฃผ๊ธฐ
ss=input("๋ฌธ์ž์—ด ์ž…๋ ฅ : ")
print("์ถœ๋ ฅ ๋ฌธ์ž์—ด => ", end='')

if ss.startswith("(")==False:
    print("(",end='')
print(ss,end='')

if ss.endswith(")")==False:
    print(")",end='')
print("")


# Quiz) ์˜์–ด๋กœ ๋œ ์˜ํ™”์ œ๋ชฉ ์ž…๋ ฅ ๋ฐ›์•„ - Tassds Ddfsafs Qafsf ์ด๋Ÿฐ์‹์ด ์•„๋‹ˆ๋ฉด ๋Œ€๋ฌธ์ž๋กœ ์ˆ˜์ •

a=str(input("์˜์–ด๋กœ ๋œ ์˜ํ™” ์ œ๋ชฉ์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š” : "))
print("์ˆ˜์ • ๋œ ์˜ํ™” ์ œ๋ชฉ : " + a.title())


ss= '   ํŒŒ   ์ด   ์ฌ   '
print(ss.strip())
# ๋ฌธ์ž์—ด ์–‘์ชฝ ๋ ๊ณต๋ฐฑ ์‚ญ์ œ
print(ss.rstrip())
# ๋ฌธ์ž์—ด ์˜ค๋ฅธ์ชฝ ๋ ๊ณต๋ฐฑ ์‚ญ์ œ
print(ss.lstrip())
# ๋ฌธ์ž์—ด ์™ผ์ชฝ ๋ ๊ณต๋ฐฑ ์‚ญ์ œ

ss='---ํŒŒ---์ด---์ฌ---'
print(ss.strip("-"))
# ํŠน์ˆ˜๋ฌธ์ž, ๋ฌธ์ž๋“ค๋„ ์‚ญ์ œ ๊ฐ€๋Šฅ
print(ss.rstrip("-"))
# ๋ฌธ์ž์—ด ์˜ค๋ฅธ์ชฝ ๋ ํŠน์ˆ˜๋ฌธ์ž ์‚ญ์ œ
print(ss.lstrip("-"))
# ๋ฌธ์ž์—ด ์™ผ์ชฝ ๋ ํŠน์ˆ˜๋ฌธ์ž ์‚ญ์ œ

# ๊ณต๋ฐฑ ์‚ญ์ œ
inss = "   ํ•œ๊ธ€   python   ์˜์–ด   ๋…์ผ์–ด   "
outss=""

for i in range(0,len(inss)):
    if inss[i] != " " :
        outss += inss[i]
print(outss)

# Quiz) ์ž…๋ ฅ : ํŒŒ์ด์ฌ
#       ์ถœ๋ ฅ : <<<ํŒŒ<<<์ด>>>์ฌ>>>

a=str(input("๋ฌธ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : "))
b=""
for i in range(0,len(a)):
    if len(a)%2==1:
        if i%2==1:
            b += "<<<"+a[i]
        else:
            b += ">>>"+a[i]
    else :
        print("๋‹ค๋ฅธ ๋ฌธ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”")
print(b)



##### ๋ฌธ์ž์—ด ๋‚ด์šฉ ๋ณ€๊ฒฝ
ss="์—ด์‹ฌํžˆ ํŒŒ์ด์ฌ์„ ๊ณต๋ถ€ ์ค‘"
print(ss.replace("ํŒŒ์ด์ฌ","python")) # ํ•จ์ˆ˜ ์‚ฌ์šฉํ–ˆ์„๋•Œ๋งŒ ๋ณ€๊ฒฝ
print(ss) # ํ•จ์ˆ˜ ์‚ฌ์šฉ ์•ˆํ–ˆ์„๋•Œ ๋ณ€๊ฒฝ ์•ˆ๋œ๊ฒƒ ํ™•์ธ

ss=ss.replace("ํŒŒ์ด์ฌ","python") # ๋‚ด์šฉ ์•„์˜ˆ ๋ณ€๊ฒฝ
print(ss)
# ๋งŽ์ด ์“ฐ๋Š” ํ˜•์‹

ss = "Python์„ ์—ด์‹ฌํžˆ ๊ณต๋ถ€ ์ค‘"
print(ss.split())
# ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๋‹จ์–ด๋ฅผ ๋ถ„๋ฆฌ์‹œ์ผœ ๋ฆฌ์ŠคํŠธ๋กœ ์ถœ๋ ฅ

ss1="๊น€์ •ํ˜œ, ์œค์ฑ„์€, ๊ฐ•์ˆœ๊ตฌ"
ss2="010-6733-1864"
print(ss1.split(", "))
print(ss2.split("-"))
# ํŠน์ • ๋ฌธ์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‹จ์–ด๋ฅผ ๋ถ„๋ฆฌ์‹œ์ผœ ๋ฆฌ์ŠคํŠธ๋กœ ์ถœ๋ ฅ

ss1="๊น€๊น€๊น€\n์œค์œค์œค\n๊ฐ•๊ฐ•๊ฐ•\n๋ฏผ๋ฏผ๋ฏผ"
print(ss1)
print(ss1.split("\n"))
print(ss1.splitlines())
# ์ค„ ๋ฐ”๊ฟˆ ๊ธฐ์ค€์œผ๋กœ ๋ถ„๋ฆฌ

ss="\u2665" # ์œ ๋‹ˆ์ฝ”๋“œ ํ•˜ํŠธ
print(ss.join(ss1))
# ๋ฌธ์ž์—ด ๊ฒฐํ•ฉ

## yyyy/mm/dd ๋ฌธ์ž์—ด ์ž…๋ ฅ ๋ฐ›์•„ 10๋…„ ํ›„ ๋‚ ์งœ ์ถœ๋ ฅ
ss=input("๋‚ ์งœ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” (yyyy/mm/dd) : ")

sslist = ss.split("/")
print("์ž…๋ ฅํ•œ ๋‚ ์งœ์˜ 10๋…„ ํ›„ : ", end='')
print(str(int(sslist[0])+10)+"๋…„ ",end='')
print(sslist[1]+'์›” ',end='')
print(sslist[2]+'์ผ',end='')

##### ๋ฆฌ์ŠคํŠธ ๋ฌธ์ž ํ•ญ๋ชฉ ํ˜•์‹ ๋ณ€๊ฒฝ
before= ["2022","06","02"]
after=list(map(int,before))
print(after)
# ๋ฌธ์ž์—ด๋กœ ์ €์žฅํ•œ ๋ฆฌ์ŠคํŠธ ํ•ญ๋ชฉ
# map ํ•จ์ˆ˜ ์‚ฌ์šฉ => int๋กœ ๋ณ€๊ฒฝ

##### ๋ฌธ์ž์—ด ์ •๋ ฌ ๋ฐ ์ฑ„์šฐ๊ธฐ #####
ss='ํŒŒ์ด์ฌ'
print(ss.center(10))
# 10์ž๋ฆฌ ํ™•๋ณด ํ›„, ๊ฐ€์šด๋ฐ ์ •๋ ฌ
print(ss.center(10,"-"))
# ๊ฐ€์šด๋ฐ ์ •๋ ฌ, ๊ณต๋ฐฑ์„ '๋ฌธ์ž'๋กœ ์ฑ„์šฐ๊ธฐ
print(ss.ljust(10))
# ์™ผ์ชฝ ์ •๋ ฌ
print(ss.rjust(10))
# ์˜ค๋ฅธ์ชฝ ์ •๋ ฌ
print(ss.zfill(10))
# ์˜ค๋ฅธ์ชฝ ์ •๋ ฌ, ๊ณต๋ฐฑ 0์œผ๋กœ ์ฑ„์šฐ๊ธฐ

##### ๋ฌธ์ž ๊ตฌ์„ฑ ํŒŒ์•… ( ๊ฒฐ๊ณผ Bool ) #####
print("464546".isdigit())
# ์ˆซ์ž
print("adfsf".isalpha())
# ์˜์–ด
print("fskjfskj123".isalnum())
# ์ˆซ์ž ์˜์–ด ์กฐํ•ฉ
print("asdf".islower())
# ์˜์–ด ์†Œ๋ฌธ์ž
print("ASDF".isupper())
# ์˜์–ด ๋Œ€๋ฌธ์ž
print("    ".isspace())
# ๊ณต๋ฐฑ


# Quiz) ๋‹ค์ด์•„๋ชฌ๋“œ ๋งŒ๋“ค๊ธฐ, ๊ณต๋ฐฑ ์ง์ ‘ ๋„ฃ์–ด ์‚ฌ์šฉ

a = int(input("๋‹ค์ด์•„ ๋ชฌ๋“œ์˜ ๋†’์ด๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : "))
if a%2==0:
    print(" ์ˆซ์ž๋ฅผ ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š” ")
else:
    for i in range(a):
        if i <= (a // 2):
            print(" " * (a // 2 - i), end="")
            print("*" * (2 * i + 1))
        else:
            print(" " * (i - a//2), end="")
            print("*" * (a - (2*i - a) - 1))

# Quiz) ๋‹ค์ด์•„๋ชฌ๋“œ ๋งŒ๋“ค๊ธฐ, ๋†’์ด ์ž…๋ ฅ๋ฐ›์•„ ์ •๋ ฌ, ์ฑ„์šฐ๊ธฐ ๋“ฑ๋“ฑ ํ•จ์ˆ˜ ์‚ฌ์šฉ

di=int(input("๋‹ค์ด์•„๋ชฌ๋“œ์˜ ๋†’์ด๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : "))
a=str("*")
i=0

if di%2==0:
    print(" ์ˆซ์ž๋ฅผ ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š” ")
else:
    for i in range(0,di+1,2):
        print(("*"*(i+1)).center(di))

    for i in range(di-2, 0, -2):
        print(("*" * (i)).center(di))

## ๋Œ€์†Œ๋ฌธ์ž ๋ณ€ํ™˜ ( ์•„์Šคํ‚ค ์ฝ”๋“œ )
inss,outss="",""
ch=""
count,i=0,0

inss=input("๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•˜์„ธ์š” : ")
count=len(inss)

for i in range(0,count) :
    ch=inss[i]
    if ((ord(ch)) >= ord("A") and ord(ch) <= ord("Z")) :
        newch=ch.lower()
    elif ((ord(ch)) >= ord("A") and ord(ch) <= ord("z")):
        newch=ch.upper()
    else:
        newch=ch
    outss += newch
print(outss)
๋ฐ˜์‘ํ˜•