*๋ชจ๋ ์ถ์ฒ๋ ๋์ "๋ฐ์ดํฐ ๊ณผํ์ ์ํ ํ์ด์ฌ ํ๋ก๊ทธ๋๋ฐ"์ ๋๋ค*
6์ฐจ์_๋ฌธ์์ด_๊ฐ์์.pdf
1.79MB
6์ฐจ์_๋ฌธ์์ด_๊ณผ์ .docx
4.95MB
#1. ๋ฌธ์์ด์ ์ดํด
๋ฌธ์์ด(string)์ ๊ฐ๋
- ์ค์ํ๊ฒ ๋ค๋ค์ง๋ ์๋ฃํ ์ค ํ๋
- ๋ฌธ์์ด ํน์ง
- ์ํ์ค ์๋ฃํ
- ๋ฐ์ดํฐ๋ฅผ ์์ฐจ์ ์ผ๋ก ๋ฉ๋ชจ๋ฆฌ์ ์ ์ฅ
- ์ํ์ค ์๋ฃํ
๋ฌธ์์ด์ ์ธ๋ฑ์ฑ๊ณผ ์ฌ๋ผ์ด์ฑ
- ์ธ๋ฑ์ฑ
- ๋ ๋ค๋ฅธ ์ํ์ค ์๋ฃํ์ธ ๋ฆฌ์คํธ์ ๋น์ทํ ํน์ง ๊ฐ๊ณ ์์
- ์๋์ ์ฃผ์์ธ ์ธ๋ฑ์ค ํ์ฉ
- ์ธ๋ฑ์ฑ ์ฌ์ฉ ๋ฐฉ๋ฒ
- ํ์
- ๋ฌธ์์ด ์ ์ฅ๋ ๋ณ์๋ช [์ธ๋ฑ์ค ์ฃผ์]
a = 'apple' print(a[0], a[3]) #a l print(a[-1], a[-5]). #e a
- ํ์
- ์ฌ๋ผ์ด์ฑ
- ์ฃผ์๊ฐ ํ์ฉํ์ฌ ๋ฌธ์์ด์ ๋ถ๋ถ ์ถ์ถ
- ๋ฆฌ์คํธ์ ์ฌ๋ผ์ด์ฑ๊ณผ ๋์ผ
a = 'Do it yourself!' print(a[3:6], 'and', a[-9:]) #it and yourself! print(a[:] #Do it yourself! print(a[-30:30]) #Do it yourself! print(a[::2]) #D tyusl! print(a[::-1]) #!flesuoy ti oD
- ์
๋ ฅ
- ๋ณ์๋ช [์์๋ฒํธ : ๋๋ฒํธ]
- ์ถ๋ ฅ
- ์์๋ฒํธ ๋ฌธ์์ด ~ ๋๋ฒํธ -1 ๋ฌธ์์ด
- ์ฒ์๋ถํฐ ๋๊น์ง ์ถ๋ ฅํ๊ธฐ
- ๋ณ์๋ช [:]
- ๊ฑด๋๋ธ ์นธ์ ์ง์ ํ๊ธฐ
- ๋ณ์๋ช [์์๋ฒํธ : ๋๋ฒํธ : ์นธ์]
- -1 → ์ญ์์ผ๋ก
- 2 → 2์นธ์ฉ ๊ฑด๋๋ฐ๊ธฐ
๋ฌธ์์ด์ ์ฐ์ฐ
- ๋ณ์์ฒ๋ผ ์ฐ์ฐ ๊ฐ๋ฅ, ๋ฆฌ์คํธ ์ฐ์ฐ๊ณผ ๋์ผ
- ex. ๋ฌธ์์ด๊ณผ ์ ์ํ์ ๋ํ๊ธฐ ์ฐ์ฐ ๋ถ๊ฐ.. , ๊ณฑํ๊ธฐ ์ฐ์ฐ์ ๊ฐ๋ฅ
- ๋ํ๊ธฐ ์ฐ์ฐ→ ๋ชจ๋ ๋ณ์๊ฐ ๋ฌธ์์ด์ผ ๊ฒฝ์ฐ ํ
์คํธ ๋ถ์ด๊ธฐ
- ์์ฃผ ๋ฐ์ํ๋ ์ค์
num1 = 2 print("๊ฒฐ๊ณผ๋" + num1) #์ค๋ฅ ๋ฐ์
- #๋ฌธ์์ด ๊ฐ๋ง์ ์ผ๋ก a = "Apple" b = "Pie" print(a + " " +b) #Apple Pie
- ๊ณฑํ๊ธฐ ์ฐ์ฐ
print(a*2 +" " + b*2)
#AppleApple PiePie
- ํน์ ๋ฌธ์์ด ํฌํจ ์ฌ๋ถ ํ์ธ
if 'p' in a:
print(a)
else:
print(b)
#Apple
๋ฌธ์์ด ํจ์
- ๋ฌธ์์ด ๋ค๋ฃฐ ๋ ์ฌ์ฉํ๊ธฐ ์ ์ฉ
- ํจ์ ์ฌ์ฉ๋ฐฉ๋ฒ
- ๋ฌธ์์ด๋ณ์.ํจ์๋ช
- ์ฃผ์ ๋ฌธ์์ด ํจ์
- upper() ; ๋๋ฌธ์ ๋ณํ
- drama = "The Glory" drama.upper() #THE GLORY
- lower() ; ์๋ฌธ์ ๋ณํ
- drama.lower() #the glory
- title() ; ๊ฐ ๋จ์ด ์๊ธ์๋ง ๋๋ฌธ์๋ก ๋ณ๊ฒฝ
- drama.title() #The Glory
- capitalize(); ์ฒซ ๋ฒ์งธ ๊ธ์๋ง ๋๋ฌธ์๋ก ๋ณ๊ฒฝ
- drama.capitalize() #The glory
- count() ; ํน์ ๋ฌธ์ ํฌํจ๋ ๊ฐ์
- drama = 'The Glory' drama.count('l') #1 drama.upper().count('l') #0
- isdigit() ; ๋ฌธ์์ด์ด ์ซ์์ธ์ง ํ์ธ
- drama.isdigit() #False
- startswith() ; ํด๋น ๋ฌธ์์ด๋ก ์์ํ๋์ง ํ์ธ
- True or False ๊ฐ์ผ๋ก ๋ฐํ
drama.startswith('e') #False
๋ฌธ์์ด์ ๋ถ๋ฆฌ: split() ํจ์
- ํน์ ๊ฐ ๊ธฐ์ค์ผ๋ก ๋ฌธ์์ด ๋ถ๋ฆฌ → ๋ฆฌ์คํธ๋ก ๋ณํ
- ๊ณต๋ฐฑ ๊ธฐ์ค์ผ๋ก ๋ถ๋ฆฌ
- ๋งค๊ฐ๋ณ์ ์ง์ X
- ๊ณต๋ฐฑ์ ๊ธฐ์ค์ผ๋ก ๋ฌธ์์ด ๋ถ๋ฆฌ
- ๋งค๊ฐ๋ณ์ ์ง์ X
- items = 'zero one two'.split() print(items) #['zero','one','two']
- ๋งค๊ฐ๋ณ์ ์ง์ O
- test1 = 'window,mac,linux' test1.split(',') #['window','mac','linux'] #๊ฐ ๊ฐ์ a,b,c ๋ณ์๋ก ์ธํจํน a,b,c = test1.split(',') print(a,b,c) #window mac linux #'.'์ ๊ธฐ์ค์ผ๋ก ๋๋๊ณ ์ธํจํน test2 = 'red.blue.green' d,e,f = test2.split('.') print(d,e,f) #red blue green
๋ฌธ์์ด์ ๊ฒฐํฉ: join() ํจ์
- ๋ฌธ์์ด๋ก ๊ตฌ์ฑ๋ ๋ฆฌ์คํธ ํฉ์ณ → ํ๋์ ๋ฌธ์์ด๋ก ๋ง๋ค ๋
- ์ฌ์ฉ ํํ
- ๊ตฌ๋ถ์.join(๋ฆฌ์คํธ๋ช )
- ์ฌ์ฉ ์์(1) - ๊ตฌ๋ถ์ ์ฌ์ฉ X
- color = ['red','blue','green'] result = ''.join(color) result #'redbluegreen'
- ๋ฆฌ์คํธ์ ๊ฐ ๊ฐ์ด ํ๋์ ๋ฌธ์์ด ๊ฐ์ผ๋ก ๋ฐํ๋จ
- ์ฌ์ฉ ์์(2) - ๊ตฌ๋ถ์ ์ฌ์ฉ O
- result = ' '.join(color) result #'red blue green' result = ','.join(color) result #'red,blue,green' result = '-'.join(color) result #'red-blue-green'
#2. ๋ฌธ์์ด ์์ ์ง์
- ๋ฌธ์์ด ํ์ ์ง์ → ์ถ๋ ฅ
- print() ์ฌ์ฉํ์ฌ ํ์ ํต์ผ → ๋ฌธ์์ด ์ถ๋ ฅ ์ ์ ์ฉ
์์ ์ง์ ์ ๊ฐ๋
- print() ํจ์
- ๋ณ์ ํน์ ๊ฐ ๊ตฌ๋ถ
- ์ฝค๋ง(,) ๋์ด์ฐ๊ธฐ ์ฌ์ฉ
- ๋ณ์ ํน์ ๊ฐ ๊ตฌ๋ถ
- ํ์ง๋ง ํน์ ํ์์ ๋ง์ถฐ ๊ฒฐ๊ณผ ์ถ๋ ฅํด์ผ ํ ๊ฒฝ์ฐ
- ex. ์ธ ์๋ฆฌ ์ซ์ ๋จ์๋ก ๋์ด์ฐ๊ธฐ, %์ถ๋ ฅ…
% ์์๊ณผ format()ํจ์
- ๊ธฐ๋ณธ ์ถ๋ ฅ ํ์ ์ธ์ %์์ & format()ํจ์ ๊ตฌ๋ฌธ ์ฌ์ฉํ์ฌ ํ์ ์ง์
print(1,2,3)
#1 2 3
print('a'+''+'b'+''+'c')
#a b c
print('%d %d %d' % (1,2,3))
#1 2 3
print('{} {} {}'.format('a','b','c'))
#a b c
- '%d %d %d' % (1,2,3)
- ์ซ์ 1,2,3์ด ๊ฐ๊ฐ ์ฒซ %d๋ถํฐ ์ฐจ๋ก๋๋ก ๋์๋์ด ํ ๋น๋จ
- '{} {} {}'.format('a','b','c')
- ์๋ฌด๊ฒ๋ ์๋ {} ๊ณต๊ฐ์ ‘a’, ‘b’, ‘c’๊ฐ ๊ฐ๊ฐ ๋์๋์ด ์ถ๋ ฅ๋จ
- ์์ ์ง์ ์ถ๋ ฅํ๊ธฐ์ ์ฅ์
- ๋ฐ์ดํฐ์ ์ถ๋ ฅ ํ์ ๋ถ๋ฅ ๊ฐ๋ฅ
- ์คํ์ด์ค ๋ฐ ‘+’๊ธฐํธ ํ์ฉํ๋ ๊ฒ๋ณด๋ค ์๊ฐ์ ์ดํด ์ฌ์
- ๋ฐ์ดํฐ๋ฅผ ํ์์ ๋ฐ๋ผ ๋ค๋ฅด๊ฒ ํํ ๊ฐ๋ฅ
- ๋ฌธ์์ด → %s
- print(’%s %s’ % (’one’, ‘two’))
- ์ ์ํ → %d
- ๋ฌธ์์ด → %s
- ๋ฐ์ดํฐ์ ์ถ๋ ฅ ํ์ ๋ถ๋ฅ ๊ฐ๋ฅ
- % ์์
- ์ถ๋ ฅ ์์
- ‘%์๋ฃํ % (๊ฐ)’
print('I eat %d apples.' % 3) #I eat 3 apples. print('I eat %s apples.' %'five') #I eat five apples.
- ์ถ๋ ฅ ์์
๋ณ์ ์๋ฃํ์ ๋ฐ๋ฅธ ์์
- ์๋ฃํ ๋ง๋ค ์์๋ฌธ์๊ฐ ๋ค๋ฆ
- 1 ๊ฐ ์ด์์ ๊ฐ ํ ๋น ๊ฐ๋ฅ, ์๊ดํธ ์ฌ์ฉ
- ์ฌ์ฉ ์์ - ๊ฐ ์ง์ ๋ฃ์ ๋
print('Fruit : %s, Weight(g) : %f' % ('Apple', 0.53))
#Fruit : Apple, Weight(g) : 0.53
- ์ฌ์ฉ ์์ - ๋ณ์๋ก ๊ฐ ๋ฃ์ ๋
number = 3
day = 'three'
print('I ate %d apples. I was sick for %s days." %(number, day))
#I ate 3 apples. I was sick for three days.
- format() ํจ์
- %์์๊ณผ ํ์ฉ๋ฒ ๊ฑฐ์ ๋์ผ
- ์ฐจ์ด์
- ๋ฌธ์์ด ํํ์ ์ธ์ ์ฌ์ฉ
- ์ฌ์ฉ ๋ฐฉ๋ฒ
- ‘{์๋ฃํ}’.format(์ธ์)
- ๊ธฐ๋ณธ ํํ ํํ
- print("I'm {0} years old.".format(20)) #I'm 20 years old.
- ๋ณ์ ์ด๋ฆ ํ์ฉ ๋ฐ ๋ณ์ ์๋ฃํ ์ง์
- age = 22; name = 'kyuree' print("I'm {0} years old.".format(age)) #I'm 22 years old. print("My name is {0} and {1} years old.".format(name, age)) #My name is kyuree and 22 years old. print('Fruit : {0}, Weight(g) : {1:.2f}'.format('Apple', 0.53))
ํจ๋ฉ
- ์ฌ์ ๊ณต๊ฐ ์ง์ → ๊ธ์ ๋ฐฐ์ด ๋ง์ถค or ์์์ ์๋ฆฟ์ ๋ง์ถค
- % ์์ & formatํจ์ ๋ชจ๋ ํจ๋ฉ ๊ธฐ๋ฅ ์ ๊ณต
- % ์์ ํจ๋ฉ
- ์ฌ์ฉ ๋ฐฉ๋ฒ(1) - ๊ธ์ ๋ฐฐ์ด
#์ฐ์ธก ์ ๋ ฌ print("%10d" % 12) # 12 #์ข์ธก ์ ๋ ฌ print("%-10d" % 12) #12
- "%10d" % 12
- 10์๋ฆฌ ๊ณต๊ฐ ํ๋ณด
- ์ฐ์ธก ์ ๋ ฌ๋ก 12 ์ถ๋ ฅ
- 9๋ฒ์งธ ์นธ๋ถํฐ ์ถ๋ ฅ์์
- "%-10d" % 12
- ์ข์ธก ์ ๋ ฌ๋ก 12์ถ๋ ฅ
- ‘-’ ๋ถํธ ๋ถ์ฌ์ค
- ์ฌ์ฉ ๋ฐฉ๋ฒ(2) - ์์์ ์๋ฆฟ์
#์ฐ์ธก ์ ๋ ฌ print("%10.3f" % 5.9234) # 5.923 print("%10.2f" % 5.9234) # 5.92 #์ข์ธก ์ ๋ ฌ print("-10.2f" % 5.9234) #5.92
- "%10.3f" % 5.9234
- 10์๋ฆฌ ๊ณต๊ฐ ํ๋ณด
- ์์์ ์ ์งธ ์๋ฆฌ๊น์ง ์ถ๋ ฅ
- 10์๋ฆฌ ์์ ์์์ ๋ ํฌํจ๋จ
- format() ํจ์ ํจ๋ฉ
- % ์์๊ณผ ๋น์ท
- ์ฌ์ฉ ๋ฐฉ๋ฒ(1) - ๊ธ์ ๋ฐฐ์ด
#์ฐ์ธก ์ ๋ ฌ print("{0:>10s}".format("apple")) # apple #์ข์ธก ์ ๋ ฌ print("{0:<10s}".format("apple")) #apple
- "{0:>10s}".format("apple")
- 10์๋ฆฌ ๊ณต๊ฐ ํ๋ณด
- ์ฐ์ธก ์ ๋ ฌ ์ถ๋ ฅ
- ์ข์ธก ์ ๋ ฌ?
- ‘<’ ๋ถํธ ์ฌ์ฉ
- ์ฌ์ฉ ๋ฐฉ๋ฒ(2) - ์์์ ์๋ฆฟ์
#์ฐ์ธก ์ ๋ ฌ "{1:>10.5f}".format("apple" , 5.743) #' 5.74300' #์ข์ธก ์ ๋ ฌ "{1:<10.5f}".format("apple" , 5.743) #'5.74300 '
- "{1:>10.5f}".format("apple" , 5.743)
- 10์๋ฆฌ ๊ณต๊ฐ ํ๋ณด
- ์์์ ๋ค์ฏ ๋ฒ์งธ ์๋ฆฌ๊น์ง ์ค์ ์ถ๋ ฅ
- ์์์ ๋ ํฌํจ
- ์ฐ์ธก ์ ๋ ฌ
#3. ๋ฌธ์์ด ์ฌ์ฉ ์์
1. ๋ฌธ์์ด ๊ฐ์ ์ธ๊ธฐ ; len()ํจ์
3. ํ๋ฒ, ์์ํ๊ณผ, ์ด๋ฆ(์ฑ ์ ์ธ) ์ ๋ ฅ๋ฐ์ ๊ณ ์ ์์ด๋ ๋ง๋ค๊ธฐ
- ๋ฌธ์
- ๋์ ํํ์๋ค์ ์ด์ด๋ ๋ฆฌ์คํธ ๋ง๋ค๊ธฐ ์ํด ํ๋ฒ, ์์ํ๊ณผ, ์ด๋ฆ์ ํ๋์ ๋ฌธ์์ด๋ก ์ ๋ ฅ๋ฐ๊ณ ํฉ์น๋ค. ๊ทธ๋ค์์ ๊ฐ ์ ๋ณด๊ฐ ‘-’๋ก ์ฐ๊ฒฐ๋ ๊ณ ์ ์์ด๋๋ฅผ ์ถ๋ ฅํ๋ค.
- ์ ๋ ฅ
- ์ฒซ์งธ ์ค์ ์ด๋ฆ, ์์ํ๊ณผ, ํ๋ฒ์ด ๋ด๊ธด ๋ฌธ์์ด์ ์ ๋ ฅ๋ฐ๋๋ค.(*๊ฐ ์ ๋ณด๋ ๊ณต๋ฐฑ์ผ๋ก ๋๋ ์ ธ์๋ค)
- ์์ ์ ๋ ฅ
- kyuree datascience 21
- ์์ ์ถ๋ ฅ
- kyuree-datascience-21
→ split, join ํจ์ ํ์ฉ
- ์์ ์ฝ๋
word = input()
print(len(word))
-
word=input() num = int(input()) print(word[num-1])
informations = input() information = informations.split() dsob_id ='-'.join(information) print(dsob_id)
'๐ ์คํฐ๋ > ํ์ด์ฌ ์คํฐ๋ ๊ฐ์์๋ฃ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[3ํ/๊น๊ฒฝ์] 6์ฐจ์ ํ์ด์ฌ ์คํฐ๋ - ๋ฌธ์์ด (0) | 2023.05.02 |
---|---|
[2ํ/๊น์ธ์ฐ] 6์ฐจ์ ํ์ด์ฌ ์คํฐ๋ - ๋ฌธ์์ด (0) | 2023.05.01 |
[4ํ/๊น๋ฏผํ] 6์ฐจ์ ํ์ด์ฌ ์คํฐ๋ - ๋ฌธ์์ด (0) | 2023.04.28 |
[4ํ/์ด์ ์] 6์ฐจ์ ํ์ด์ฌ ์คํฐ๋ - ๋ฌธ์์ด (0) | 2023.04.28 |
[4ํ/๊น๋ฏผํ] 5์ฐจ์ ํ์ด์ฌ ์คํฐ๋ - ํจ์ (0) | 2023.04.13 |