Skip to content

30 Days Of Python: Day 4 - Strings

Twitter Follow

Author: Asabeneh Yetayeh
Second Edition: July, 2021

<< Day 3 | Day 5 >>

30DaysOfPython

Day 4

๋ฌธ์ž์—ด

ํ…์ŠคํŠธ๋Š” ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ ์œ ํ˜•์ž…๋‹ˆ๋‹ค. ํ…์ŠคํŠธ๋กœ ์ž‘์„ฑ๋œ ๋ชจ๋“  ๋ฐ์ดํ„ฐ ์œ ํ˜•์€ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค. ์ž‘์€๋”ฐ์˜ดํ‘œ, ํฐ๋”ฐ์˜ดํ‘œ ๋˜๋Š” ์‚ผ์ค‘๋”ฐ์˜ดํ‘œ ์•„๋ž˜์˜ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋Š” ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ ์œ ํ˜•์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ๋‹ค์–‘ํ•œ ๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ์™€ ๋‚ด์žฅ ํ•จ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ํ™•์ธํ•˜๋ ค๋ฉด len() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์‹ญ์‹œ์˜ค.

๋ฌธ์ž์—ด ๋งŒ๋“ค๊ธฐ

letter = 'P'                # A string could be a single character or a bunch of texts
print(letter)               # P
print(len(letter))          # 1
greeting = 'Hello, World!'  # String could be made using a single or double quote,"Hello, World!"
print(greeting)             # Hello, World!
print(len(greeting))        # 13
sentence = "I hope you are enjoying 30 days of Python Challenge"
print(sentence)

์—ฌ๋Ÿฌ ์ค„ ๋ฌธ์ž์—ด์€ ์„ธ ๊ฐœ์˜ ์ž‘์€๋”ฐ์˜ดํ‘œ(''') ๋˜๋Š” ์„ธ ๊ฐœ์˜ ํฐ๋”ฐ์˜ดํ‘œ(""")๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ์˜ˆ๋ฅผ ์ฐธ์กฐํ•˜์‹ญ์‹œ์˜ค.

multiline_string = '''I am a teacher and enjoy teaching.
I didn't find anything as rewarding as empowering people.
That is why I created 30 days of python.'''
print(multiline_string)

# Another way of doing the same thing
multiline_string = """I am a teacher and enjoy teaching.
I didn't find anything as rewarding as empowering people.
That is why I created 30 days of python."""
print(multiline_string)

๋ฌธ์ž์—ด ์—ฐ๊ฒฐ

๋ฌธ์ž์—ด์„ ํ•จ๊ป˜ ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์„ ๋ณ‘ํ•ฉํ•˜๊ฑฐ๋‚˜ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒƒ์„ ์—ฐ๊ฒฐ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ์˜ˆ๋ฅผ ์ฐธ์กฐํ•˜์‹ญ์‹œ์˜ค.

first_name = 'Asabeneh'
last_name = 'Yetayeh'
space = ' '
full_name = first_name  +  space + last_name
print(full_name) # Asabeneh Yetayeh
# Checking the length of a string using len() built-in function
print(len(first_name))  # 8
print(len(last_name))   # 7
print(len(first_name) > len(last_name)) # True
print(len(full_name)) # 16

๋ฌธ์ž์—ด์˜ ์ด์Šค์ผ€์ดํ”„ ์‹œํ€€์Šค

Python ๋ฐ ๊ธฐํƒ€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—์„œ ๋‹ค์Œ์— ์˜ค๋Š” ๋ฌธ์ž๋Š” ์ด์Šค์ผ€์ดํ”„ ์‹œํ€€์Šค์ž…๋‹ˆ๋‹ค. ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ ์ด์Šค์ผ€์ดํ”„ ๋ฌธ์ž๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

  • \n: ์ƒˆ๋กœ์šด ๋ผ์ธ
  • \t: ํƒญ์€(8์นธ)์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
  • \: ๋ฐฑ์Šฌ๋ž˜์‹œ
  • \': ์ž‘์€๋”ฐ์˜ดํ‘œ(')
  • \": ํฐ๋”ฐ์˜ดํ‘œ(")

์ด์ œ ์œ„์˜ ์ด์Šค์ผ€์ดํ”„ ์‹œํ€€์Šค๋ฅผ ์˜ˆ์ œ์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

print('I hope everyone is enjoying the Python Challenge.\nAre you ?') # line break
print('Days\tTopics\tExercises') # adding tab space or 4 spaces
print('Day 1\t3\t5')
print('Day 2\t3\t5')
print('Day 3\t3\t5')
print('Day 4\t3\t5')
print('This is a backslash  symbol (\\)') # To write a backslash
print('In every programming language it starts with \"Hello, World!\"') # to write a double quote inside a single quote

# output
I hope every one is enjoying the Python Challenge.
Are you ?
Days    Topics  Exercises
Day 1   5       5
Day 2   6       20
Day 3   5       23
Day 4   1       35
This is a backslash  symbol (\)
In every programming language it starts with "Hello, World!"

๋ฌธ์ž์—ด ํฌ๋งคํŒ…

์˜ฌ๋“œ ์Šคํƒ€์ผ ๋ฌธ์ž์—ด ํ˜•์‹ํ™”(% ์—ฐ์‚ฐ์ž)

Python์—๋Š” ๋ฌธ์ž์—ด ํ˜•์‹์„ ์ง€์ •ํ•˜๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์„น์…˜์—์„œ๋Š” ๊ทธ ์ค‘ ์ผ๋ถ€๋ฅผ ๋‹ค๋ฃฐ ๊ฒƒ์ž…๋‹ˆ๋‹ค. "%" ์—ฐ์‚ฐ์ž๋Š” "์ธ์ˆ˜ ์ง€์ •์ž", "%s"์™€ ๊ฐ™์€ ํŠน์ˆ˜ ๊ธฐํ˜ธ์™€ ํ•จ๊ป˜ ์ผ๋ฐ˜ ํ…์ŠคํŠธ๋ฅผ ํฌํ•จํ•˜๋Š” ํ˜•์‹ ๋ฌธ์ž์—ด๊ณผ ํ•จ๊ป˜ "ํŠœํ”Œ"(๊ณ ์ • ํฌ๊ธฐ ๋ชฉ๋ก)๋กœ ๋ฌถ์ธ ๋ณ€์ˆ˜ ์„ธํŠธ์˜ ํ˜•์‹์„ ์ง€์ •ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. , "%d", "%f", "%. ์ž๋ฆฟ์ˆ˜ f".

  • %s - ๋ฌธ์ž์—ด(๋˜๋Š” ์ˆซ์ž์™€ ๊ฐ™์€ ๋ฌธ์ž์—ด ํ‘œํ˜„์ด ์žˆ๋Š” ๋ชจ๋“  ๊ฐ์ฒด)
  • %d - ์ •์ˆ˜
  • %f - ๋ถ€๋™ ์†Œ์ˆ˜์  ์ˆซ์ž
  • "%. number of digits f" - ์ •๋ฐ€๋„๊ฐ€ ๊ณ ์ •๋œ ๋ถ€๋™ ์†Œ์ˆ˜์  ์ˆซ์ž
# Strings only
first_name = 'Asabeneh'
last_name = 'Yetayeh'
language = 'Python'
formated_string = 'I am %s %s. I teach %s' %(first_name, last_name, language)
print(formated_string)

# Strings  and numbers
radius = 10
pi = 3.14
area = pi * radius ** 2
formated_string = 'The area of circle with a radius %d is %.2f.' %(radius, area) # 2 refers the 2 significant digits after the point

python_libraries = ['Django', 'Flask', 'NumPy', 'Matplotlib','Pandas']
formated_string = 'The following are python libraries:%s' % (python_libraries)
print(formated_string) # "The following are python libraries:['Django', 'Flask', 'NumPy', 'Matplotlib','Pandas']"

์ƒˆ๋กœ์šด์Šคํƒ€์ผ ๋ฌธ์ž์—ด ํ˜•์‹ํ™”(str.format)

์ด ํ˜•์‹์€ Python ๋ฒ„์ „ 3์—์„œ ๋„์ž…๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

first_name = 'Asabeneh'
last_name = 'Yetayeh'
language = 'Python'
formated_string = 'I am {} {}. I teach {}'.format(first_name, last_name, language)
print(formated_string)
a = 4
b = 3

print('{} + {} = {}'.format(a, b, a + b))
print('{} - {} = {}'.format(a, b, a - b))
print('{} * {} = {}'.format(a, b, a * b))
print('{} / {} = {:.2f}'.format(a, b, a / b)) # limits it to two digits after decimal
print('{} % {} = {}'.format(a, b, a % b))
print('{} // {} = {}'.format(a, b, a // b))
print('{} ** {} = {}'.format(a, b, a ** b))

# output
4 + 3 = 7
4 - 3 = 1
4 * 3 = 12
4 / 3 = 1.33
4 % 3 = 1
4 // 3 = 1
4 ** 3 = 64

# Strings  and numbers
radius = 10
pi = 3.14
area = pi * radius ** 2
formated_string = 'The area of a circle with a radius {} is {:.2f}.'.format(radius, area) # 2 digits after decimal
print(formated_string)

๋ฌธ์ž์—ด Interpolation / f-Strings (Python 3.6+)

๋˜ ๋‹ค๋ฅธ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด ํ˜•์‹ํ™”๋Š” ๋ฌธ์ž์—ด ๋ณด๊ฐ„๋ฒ•์ธ f-๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์€ f๋กœ ์‹œ์ž‘ํ•˜๊ณ  ํ•ด๋‹น ์œ„์น˜์— ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ์ž…ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

a = 4
b = 3
print(f'{a} + {b} = {a +b}')
print(f'{a} - {b} = {a - b}')
print(f'{a} * {b} = {a * b}')
print(f'{a} / {b} = {a / b:.2f}')
print(f'{a} % {b} = {a % b}')
print(f'{a} // {b} = {a // b}')
print(f'{a} ** {b} = {a ** b}')

๋ฌธ์ž ์‹œํ€€์Šค๋กœ์„œ์˜ Python ๋ฌธ์ž์—ด

Python ๋ฌธ์ž์—ด์€ ๋ฌธ์ž ์‹œํ€€์Šค์ด๋ฉฐ, ๊ธฐ๋ณธ ์•ก์„ธ์Šค ๋ฐฉ๋ฒ•์„ ๋‹ค๋ฅธ Python ์ˆœ์„œ ๊ฐ์ฒด ์‹œํ€€์Šค(๋ชฉ๋ก ๋ฐ ํŠœํ”Œ)์™€ ๊ณต์œ ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด(๋ฐ ๋ชจ๋“  ์‹œํ€€์Šค์˜ ๊ฐœ๋ณ„ ๋ฉค๋ฒ„)์—์„œ ๋‹จ์ผ ๋ฌธ์ž๋ฅผ ์ถ”์ถœํ•˜๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์€ ํ•ด๋‹น ๋ณ€์ˆ˜๋กœ ์••์ถ•์„ ํ‘ธ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์–ธํŒจํ‚น ๋ฌธ์ž

language = 'Python'
a,b,c,d,e,f = language # unpacking sequence characters into variables
print(a) # P
print(b) # y
print(c) # t
print(d) # h
print(e) # o
print(f) # n

์ธ๋ฑ์Šค๋กœ ๋ฌธ์ž์—ด์˜ ๋ฌธ์ž์— ์•ก์„ธ์Šค

ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ์นด์šดํŒ…์€ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ฌธ์ž์—ด์˜ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž๋Š” ์ธ๋ฑ์Šค๊ฐ€ 0์ด๊ณ  ๋ฌธ์ž์—ด์˜ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋Š” ๋ฌธ์ž์—ด์˜ ๊ธธ์ด์—์„œ 1์„ ๋บ€ ๊ฐ’์ž…๋‹ˆ๋‹ค.

String index

language = 'Python'
first_letter = language[0]
print(first_letter) # P
second_letter = language[1]
print(second_letter) # y
last_index = len(language) - 1
last_letter = language[last_index]
print(last_letter) # n

์˜ค๋ฅธ์ชฝ ๋์—์„œ ์‹œ์ž‘ํ•˜๋ ค๋ฉด ์Œ์ˆ˜ ์ธ๋ฑ์‹ฑ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. -1์€ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค์ž…๋‹ˆ๋‹ค.

language = 'Python'
last_letter = language[-1]
print(last_letter) # n
second_last = language[-2]
print(second_last) # o

ํŒŒ์ด์ฌ ๋ฌธ์ž์—ด ์Šฌ๋ผ์ด์‹ฑ

ํŒŒ์ด์ฌ์—์„œ๋Š” ๋ฌธ์ž์—ด์„ ํ•˜์œ„ ๋ฌธ์ž์—ด๋กœ ์Šฌ๋ผ์ด์Šคํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

language = 'Python'
first_three = language[0:3] # starts at zero index and up to 3 but not include 3
print(first_three) #Pyt
last_three = language[3:6]
print(last_three) # hon
# Another way
last_three = language[-3:]
print(last_three)   # hon
last_three = language[3:]
print(last_three)   # hon

๋ฌธ์ž์—ด ๋ฆฌ๋ฒ„์Šค

ํŒŒ์ด์ฌ์—์„œ ๋ฌธ์ž์—ด์„ ์‰ฝ๊ฒŒ ๋’ค์ง‘์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

greeting = 'Hello, World!'
print(greeting[::-1]) # !dlroW ,olleH

์Šฌ๋ผ์ด์‹ฑํ•˜๋Š” ๋™์•ˆ ๋ฌธ์ž ๊ฑด๋„ˆ๋›ฐ๊ธฐ

์Šฌ๋ผ์ด์Šค ๋ฉ”์†Œ๋“œ์— ๋‹จ๊ณ„ ์ธ์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ์Šฌ๋ผ์ด์Šคํ•˜๋Š” ๋™์•ˆ ๋ฌธ์ž๋ฅผ ๊ฑด๋„ˆ๋›ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

language = 'Python'
pto = language[0:6:2] #
print(pto) # Pto

๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ

๋ฌธ์ž์—ด์„ ํ˜•์‹ํ™”ํ•  ์ˆ˜ ์žˆ๋Š” ๋งŽ์€ ๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ์˜ˆ์ œ์—์„œ ์ผ๋ถ€ ๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ๋ฅผ ์ฐธ์กฐํ•˜์‹ญ์‹œ์˜ค.

  • capitalize(): ๋ฌธ์ž์—ด์˜ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜
challenge = 'thirty days of python'
print(challenge.capitalize()) # 'Thirty days of python'
  • count(): ๋ฌธ์ž์—ด์—์„œ ํ•˜์œ„ ๋ฌธ์ž์—ด์˜ ๋ฐœ์ƒ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. count(substring, start=.., end=..). ์‹œ์ž‘์€ ์นด์šดํŠธ๋ฅผ ์œ„ํ•œ ์‹œ์ž‘ ์ธ๋ฑ์‹ฑ์ด๊ณ  ๋์€ ์นด์šดํŠธํ•  ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค์ž…๋‹ˆ๋‹ค.
challenge = 'thirty days of python'
print(challenge.count('y')) # 3
print(challenge.count('y', 7, 14)) # 1,
print(challenge.count('th')) # 2`
  • endswith(): ๋ฌธ์ž์—ด์ด ์ง€์ •๋œ ๋์œผ๋กœ ๋๋‚˜๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
challenge = 'thirty days of python'
print(challenge.endswith('on'))   # True
print(challenge.endswith('tion')) # False
  • expandtabs(): ํƒญ ๋ฌธ์ž๋ฅผ ๊ณต๋ฐฑ์œผ๋กœ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ํƒญ ํฌ๊ธฐ๋Š” 8์ž…๋‹ˆ๋‹ค. ํƒญ ํฌ๊ธฐ ์ธ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
challenge = 'thirty\tdays\tof\tpython'
print(challenge.expandtabs())   # 'thirty  days    of      python'
print(challenge.expandtabs(10)) # 'thirty    days      of        python'
  • find(): ํ•˜์œ„ ๋ฌธ์ž์—ด์ด ์ฒ˜์Œ ๋‚˜ํƒ€๋‚˜๋Š” ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ฐพ์„ ์ˆ˜ ์—†์œผ๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
challenge = 'thirty days of python'
print(challenge.count('y')) # 3
print(challenge.count('y', 7, 14)) # 1,
print(challenge.count('th')) # 2`
  • rfind(): ํ•˜์œ„ ๋ฌธ์ž์—ด์ด ๋งˆ์ง€๋ง‰์œผ๋กœ ๋‚˜ํƒ€๋‚˜๋Š” ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ฐพ์„ ์ˆ˜ ์—†์œผ๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
challenge = 'thirty days of python'
print(challenge.rfind('y'))  # 5
print(challenge.rfind('th')) # 1
  • format(): ๋ฌธ์ž์—ด์„ ๋” ๋‚˜์€ ์ถœ๋ ฅ์œผ๋กœ ํฌ๋งทํ•ฉ๋‹ˆ๋‹ค.
    ๋ฌธ์ž์—ด ํ˜•์‹์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์ด ๋งํฌ ๋ฅผ ํ™•์ธํ•˜์„ธ์š”.
first_name = 'Asabeneh'
last_name = 'Yetayeh'
age = 250
job = 'teacher'
country = 'Finland'
sentence = 'I am {} {}. I am a {}. I am {} years old. I live in {}.'.format(first_name, last_name, age, job, country)
print(sentence) # I am Asabeneh Yetayeh. I am 250 years old. I am a teacher. I live in Finland.

radius = 10
pi = 3.14
area = pi * radius ** 2
result = 'The area of a circle with radius {} is {}'.format(str(radius), str(area))
print(result) # The area of a circle with radius 10 is 314
  • index(): ํ•˜์œ„ ๋ฌธ์ž์—ด์˜ ๊ฐ€์žฅ ๋‚ฎ์€ ์ƒ‰์ธ์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์ถ”๊ฐ€ ์ธ์ˆ˜๋Š” ์‹œ์ž‘ ๋ฐ ๋ ์ƒ‰์ธ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค(๊ธฐ๋ณธ๊ฐ’ 0 ๋ฐ ๋ฌธ์ž์—ด ๊ธธ์ด - 1). ํ•˜์œ„ ๋ฌธ์ž์—ด์„ ์ฐพ์„ ์ˆ˜ ์—†์œผ๋ฉด valueError๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.
challenge = 'thirty days of python'
sub_string = 'da'
print(challenge.index(sub_string))  # 7
print(challenge.index(sub_string, 9)) # error
  • rindex(): ํ•˜์œ„ ๋ฌธ์ž์—ด์˜ ๊ฐ€์žฅ ๋†’์€ ์ƒ‰์ธ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ถ”๊ฐ€ ์ธ์ˆ˜๋Š” ์‹œ์ž‘ ๋ฐ ๋ ์ƒ‰์ธ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค(๊ธฐ๋ณธ๊ฐ’ 0 ๋ฐ ๋ฌธ์ž์—ด ๊ธธ์ด - 1).
challenge = 'thirty days of python'
sub_string = 'da'
print(challenge.rindex(sub_string))  # 8
print(challenge.rindex(sub_string, 9)) # error
  • isalnum(): ์˜์ˆซ์ž ํ™•์ธ
challenge = 'ThirtyDaysPython'
print(challenge.isalnum()) # True

challenge = '30DaysPython'
print(challenge.isalnum()) # True

challenge = 'thirty days of python'
print(challenge.isalnum()) # False, space is not an alphanumeric character

challenge = 'thirty days of python 2019'
print(challenge.isalnum()) # False
  • isalpha(): ๋ชจ๋“  ๋ฌธ์ž์—ด ์š”์†Œ๊ฐ€ ์•ŒํŒŒ๋ฒณ ๋ฌธ์ž(az ๋ฐ AZ)์ธ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
challenge = 'thirty days of python'
print(challenge.isalpha()) # False, space is once again excluded
challenge = 'ThirtyDaysPython'
print(challenge.isalpha()) # True
num = '123'
print(num.isalpha())      # False
  • isdecimal(): ๋ฌธ์ž์—ด์˜ ๋ชจ๋“  ๋ฌธ์ž๊ฐ€ ์‹ญ์ง„์ˆ˜(0-9)์ธ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
challenge = 'thirty days of python'
print(challenge.isdecimal())  # False
challenge = '123'
print(challenge.isdecimal())  # True
challenge = '\u00B2'
print(challenge.isdigit())   # False
challenge = '12 3'
print(challenge.isdecimal())  # False, space not allowed
  • isdigit(): ๋ฌธ์ž์—ด์˜ ๋ชจ๋“  ๋ฌธ์ž๊ฐ€ ์ˆซ์ž์ธ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค(์ˆซ์ž๋Š” 0-9 ๋ฐ ์ผ๋ถ€ ๋‹ค๋ฅธ ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž).
challenge = 'Thirty'
print(challenge.isdigit()) # False
challenge = '30'
print(challenge.isdigit())   # True
challenge = '\u00B2'
print(challenge.isdigit())   # True
  • isnumeric(): ๋ฌธ์ž์—ด์˜ ๋ชจ๋“  ๋ฌธ์ž๊ฐ€ ์ˆซ์ž์ธ์ง€ ๋˜๋Š” ์ˆซ์ž์™€ ๊ด€๋ จ๋œ ๊ฒƒ์ธ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค(isdigit()์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ยฝ๊ณผ ๊ฐ™์€ ๋” ๋งŽ์€ ๊ธฐํ˜ธ๋ฅผ ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค).
num = '10'
print(num.isnumeric()) # True
num = '\u00BD' # ยฝ
print(num.isnumeric()) # True
num = '10.5'
print(num.isnumeric()) # False
  • isidentifier(): ์œ ํšจํ•œ ์‹๋ณ„์ž๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์ด ์œ ํšจํ•œ ๋ณ€์ˆ˜ ์ด๋ฆ„์ธ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
challenge = '30DaysOfPython'
print(challenge.isidentifier()) # False, because it starts with a number
challenge = 'thirty_days_of_python'
print(challenge.isidentifier()) # True
  • islower(): ๋ฌธ์ž์—ด์˜ ๋ชจ๋“  ์•ŒํŒŒ๋ฒณ ๋ฌธ์ž๊ฐ€ ์†Œ๋ฌธ์ž์ธ์ง€ ํ™•์ธ
challenge = 'thirty days of python'
print(challenge.islower()) # True
challenge = 'Thirty days of python'
print(challenge.islower()) # False
  • islower(): ๋ฌธ์ž์—ด์˜ ๋ชจ๋“  ์•ŒํŒŒ๋ฒณ ๋ฌธ์ž๊ฐ€ ์†Œ๋ฌธ์ž์ธ์ง€ ํ™•์ธ
challenge = 'thirty days of python'
print(challenge.isupper()) #  False
challenge = 'THIRTY DAYS OF PYTHON'
print(challenge.isupper()) # True
  • join(): ์—ฐ๊ฒฐ๋œ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
web_tech = ['HTML', 'CSS', 'JavaScript', 'React']
result = ' '.join(web_tech)
print(result) # 'HTML CSS JavaScript React'
web_tech = ['HTML', 'CSS', 'JavaScript', 'React']
result = '# '.join(web_tech)
print(result) # 'HTML# CSS# JavaScript# React'
  • strip(): ๋ฌธ์ž์—ด์˜ ์‹œ์ž‘๊ณผ ๋์—์„œ ์‹œ์ž‘ํ•˜์—ฌ ์ฃผ์–ด์ง„ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
challenge = 'thirty days of pythoonnn'
print(challenge.strip('noth')) # 'irty days of py'
  • replace(): ํ•˜์œ„ ๋ฌธ์ž์—ด์„ ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด๋กœ ๋Œ€์ฒดํ•ฉ๋‹ˆ๋‹ค.
challenge = 'thirty days of python'
print(challenge.replace('python', 'coding')) # 'thirty days of coding'
  • split(): ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด ๋˜๋Š” ๊ณต๋ฐฑ์„ ๊ตฌ๋ถ„ ๊ธฐํ˜ธ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด์„ ๋ถ„ํ• ํ•ฉ๋‹ˆ๋‹ค.
challenge = 'thirty days of python'
print(challenge.split()) # ['thirty', 'days', 'of', 'python']
challenge = 'thirty, days, of, python'
print(challenge.split(', ')) # ['thirty', 'days', 'of', 'python']
  • title(): ์ œ๋ชฉ ์ผ€์ด์Šค ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
challenge = 'thirty days of python'
print(challenge.title()) # Thirty Days Of Python
  • swapcase(): ๋ชจ๋“  ๋Œ€๋ฌธ์ž๋ฅผ ์†Œ๋ฌธ์ž๋กœ, ๋ชจ๋“  ์†Œ๋ฌธ์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜
challenge = 'thirty days of python'
print(challenge.swapcase())   # THIRTY DAYS OF PYTHON
challenge = 'Thirty Days Of Python'
print(challenge.swapcase())  # tHIRTY dAYS oF pYTHON
  • startswith(): ๋ฌธ์ž์—ด์ด ์ง€์ •๋œ ๋ฌธ์ž์—ด๋กœ ์‹œ์ž‘ํ•˜๋Š”์ง€ ํ™•์ธ
challenge = 'thirty days of python'
print(challenge.startswith('thirty')) # True

challenge = '30 days of python'
print(challenge.startswith('thirty')) # False

๐ŸŒ• ๋‹น์‹ ์€ ํŠน๋ณ„ํ•œ ์‚ฌ๋žŒ์ด๊ณ  ๋†€๋ผ์šด ์ž ์žฌ๋ ฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹น์‹ ์€ ๋ฐฉ๊ธˆ 4์ผ ์ฐจ ๋„์ „์„ ์™„๋ฃŒํ–ˆ๊ณ  ๋‹น์‹ ์€ ์œ„๋Œ€ํ•จ์„ ํ–ฅํ•œ ๋‹น์‹ ์˜ ๊ธธ์— 4๊ฑธ์Œ ๋‚จ์•˜์Šต๋‹ˆ๋‹ค. ์ด์ œ ๋‡Œ์™€ ๊ทผ์œก์„ ์œ„ํ•œ ๋ช‡ ๊ฐ€์ง€ ํ›ˆ๋ จ์„ ํ•˜์‹ญ์‹œ์˜ค.

๐Ÿ’ป Exercises - Day 4

  1. ๋ฌธ์ž์—ด 'Thirty', 'Days', 'Of', 'Python'์„ ๋‹จ์ผ ๋ฌธ์ž์—ด 'Thirty Days Of Python'์— ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค.
  2. ๋ฌธ์ž์—ด 'Coding', 'For' , 'All'์„ ๋‹จ์ผ ๋ฌธ์ž์—ด 'Coding For All'์— ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค.
  3. company๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ  ์ดˆ๊ธฐ ๊ฐ’ "Coding For All"์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค.
  4. print() ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํšŒ์‚ฌ ๋ณ€์ˆ˜๋ฅผ ์ธ์‡„ํ•ฉ๋‹ˆ๋‹ค.
  5. len() ๋ฉ”์„œ๋“œ์™€ print() ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํšŒ์‚ฌ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ์ธ์‡„ํ•ฉ๋‹ˆ๋‹ค.
  6. upper() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค.
  7. lower() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ์†Œ๋ฌธ์ž๋กœ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค.
  8. ๋ฌธ์ž์—ด Coding For All ์˜ ๊ฐ’์„ ํ˜•์‹ํ™”ํ•˜๋ ค๋ฉด capitalize(), title(), swapcase() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์‹ญ์‹œ์˜ค.
  9. Coding For All ๋ฌธ์ž์—ด์˜ ์ฒซ ๋ฒˆ์งธ ๋‹จ์–ด๋ฅผ ์ž˜๋ผ๋ƒ…๋‹ˆ๋‹ค.
  10. Index, find ๋˜๋Š” ๊ธฐํƒ€ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ Coding For All ๋ฌธ์ž์—ด์— ๋‹จ์–ด Coding์ด ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
  11. ๋ฌธ์ž์—ด 'Coding For All'์˜ ์ฝ”๋”ฉ์ด๋ผ๋Š” ๋‹จ์–ด๋ฅผ Python์œผ๋กœ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค.
  12. replace ๋ฉ”์„œ๋“œ ๋˜๋Š” ๊ธฐํƒ€ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋‘๋ฅผ ์œ„ํ•œ Python์„ ๋ชจ๋‘๋ฅผ ์œ„ํ•œ Python์œผ๋กœ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค.
  13. ๊ณต๋ฐฑ์„ ๊ตฌ๋ถ„ ๊ธฐํ˜ธ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด 'Coding For All'์„ ๋ถ„ํ• ํ•ฉ๋‹ˆ๋‹ค(split()).
  14. "Facebook, Google, Microsoft, Apple, IBM, Oracle, Amazon"์€ ์‰ผํ‘œ์—์„œ ๋ฌธ์ž์—ด์„ ๋‚˜๋ˆ•๋‹ˆ๋‹ค.
  15. ๋ฌธ์ž์—ด Coding For All ์—์„œ ์ธ๋ฑ์Šค 0์— ์žˆ๋Š” ๋ฌธ์ž๋Š” ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?
  16. ๋ฌธ์ž์—ด Coding For All ์—์„œ ์ธ๋ฑ์Šค 0์— ์žˆ๋Š” ๋ฌธ์ž๋Š” ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?
  17. "Coding For All" ๋ฌธ์ž์—ด์—์„œ ์ธ๋ฑ์Šค 10์— ์žˆ๋Š” ๋ฌธ์ž๋Š” ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?
  18. 'Python For Everyone'์ด๋ผ๋Š” ์ด๋ฆ„์˜ ์•ฝ์–ด ๋˜๋Š” ์•ฝ์–ด๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
  19. 'Coding For All'์ด๋ผ๋Š” ์ด๋ฆ„์˜ ์•ฝ์–ด ๋˜๋Š” ์•ฝ์–ด๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
  20. Index๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Coding For All์—์„œ C๊ฐ€ ์ฒ˜์Œ ๋‚˜ํƒ€๋‚˜๋Š” ์œ„์น˜๋ฅผ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค.
  21. Index๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Coding For All์—์„œ F๊ฐ€ ์ฒ˜์Œ ๋‚˜ํƒ€๋‚˜๋Š” ์œ„์น˜๋ฅผ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค.
  22. Coding For All People์—์„œ l์ด ๋งˆ์ง€๋ง‰์œผ๋กœ ๋‚˜ํƒ€๋‚˜๋Š” ์œ„์น˜๋ฅผ ๊ฒฐ์ •ํ•˜๋ ค๋ฉด rfind๋ฅผ ์‚ฌ์šฉํ•˜์‹ญ์‹œ์˜ค.
  23. ์ƒ‰์ธ ๋˜๋Š” ์ฐพ๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์Œ ๋ฌธ์žฅ์—์„œ 'because'๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ์ฒ˜์Œ ๋‚˜ํƒ€๋‚˜๋Š” ์œ„์น˜๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค.
  24. ์ƒ‰์ธ ๋˜๋Š” ์ฐพ๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์Œ ๋ฌธ์žฅ์—์„œ 'because'๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ์ฒ˜์Œ ๋‚˜ํƒ€๋‚˜๋Š” ์œ„์น˜๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค.
  25. ์ƒ‰์ธ ๋˜๋Š” ์ฐพ๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์Œ ๋ฌธ์žฅ์—์„œ 'because'๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ์ฒ˜์Œ ๋‚˜ํƒ€๋‚˜๋Š” ์œ„์น˜๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค.
  26. ์ƒ‰์ธ ๋˜๋Š” ์ฐพ๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์Œ ๋ฌธ์žฅ์—์„œ 'because'๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ์ฒ˜์Œ ๋‚˜ํƒ€๋‚˜๋Š” ์œ„์น˜๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค.
  27. ๋‹ค์Œ ๋ฌธ์žฅ์—์„œ 'because because because'๋ผ๋Š” ๊ตฌ๋ฌธ์„ ์ž˜๋ผ๋ƒ…๋‹ˆ๋‹ค.
  28. 'Coding For All'์€ ํ•˜์œ„ ๋ฌธ์ž์—ด Coding ์œผ๋กœ ์‹œ์ž‘ํ•ฉ๋‹ˆ๊นŒ?
  29. 'Coding For All'์€ ํ•˜์œ„ ๋ฌธ์ž์—ด ์ฝ”๋”ฉ ์œผ๋กœ ๋๋‚ฉ๋‹ˆ๊นŒ?
  30. ' Coding For All ' , ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์—์„œ ์™ผ์ชฝ ๋ฐ ์˜ค๋ฅธ์ชฝ ํ›„ํ–‰ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
  31. ๋‹ค์Œ ๋ณ€์ˆ˜ ์ค‘ isidentifier() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ True๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ณ€์ˆ˜๋Š” ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?
    • 30DaysOfPython
    • thirty_days_of_python
  32. ๋‹ค์Œ ๋ชฉ๋ก์—๋Š” ์ผ๋ถ€ ํŒŒ์ด์ฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์ด๋ฆ„์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค: ['Django', 'Flask', 'Bottle', 'Pyramid', 'Falcon']. ๊ณต๋ฐฑ ๋ฌธ์ž์—ด์ด ์žˆ๋Š” ํ•ด์‹œ๋กœ ๋ชฉ๋ก์— ๊ฐ€์ž…ํ•˜์‹ญ์‹œ์˜ค.
  33. ์ƒˆ ์ค„ ์ด์Šค์ผ€์ดํ”„ ์‹œํ€€์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์Œ ๋ฌธ์žฅ์„ ๊ตฌ๋ถ„ํ•ฉ๋‹ˆ๋‹ค.
    I am enjoying this challenge.
    I just wonder what is next.
    
  34. ์ƒˆ ์ค„ ์ด์Šค์ผ€์ดํ”„ ์‹œํ€€์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์Œ ๋ฌธ์žฅ์„ ๊ตฌ๋ถ„ํ•ฉ๋‹ˆ๋‹ค.
    Name      Age     Country   City
    Asabeneh  250     Finland   Helsinki
    
  35. ๋ฌธ์ž์—ด ํ˜•์‹ ์ง€์ • ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์Œ์„ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค:
radius = 10
area = 3.14 * radius ** 2
The area of a circle with radius 10 is 314 meters square.
  1. ๋ฌธ์ž์—ด ํ˜•์‹ํ™” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์Œ์„ ์ž‘์„ฑํ•˜์‹ญ์‹œ์˜ค:
8 + 6 = 14
8 - 6 = 2
8 * 6 = 48
8 / 6 = 1.33
8 % 6 = 2
8 // 6 = 1
8 ** 6 = 262144

๐ŸŽ‰ ์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค! ๐ŸŽ‰

<< Day 3 | Day 5 >>