String

๋ฌธ์ž์—ด ๋‚˜๋ˆ„๊ธฐ

Pithon โ†’ Python ์œผ๋กœ ๋ฐ”๊พธ๋Š” ์ž‘์—…์„ ์ง„ํ–‰ํ•ด๋ณด์ž.

# ์ž˜๋ชป๋œ ์˜ˆ
a = "Pithon"
a[1] = 'y'

๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. ๋ฌธ์ž์—ด์˜ ์š”์†Œ๊ฐ’์€ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋Š” ๊ฐ’์ด ์•„๋‹ˆ๋‹ค.

# ์˜ณ์€ ์˜ˆ
a = "Pithon"
a[:1] + 'y' +a[2:]

์Šฌ๋ผ์ด์‹ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ, ์Šฌ๋ผ์ด์‹ฑ์„ ํ• ๋•Œ ๋งˆ์ง€๋ง‰ ๊ฐ’์€ ๋ฏธ๋งŒ์œผ๋กœ ์ธ์‹ํ•œ๋‹ค. ์ด๋Ÿฐ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜์ง€๋ง๊ณ , ๊ธฐ๋ณธ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋” ์‰ฝ๊ฒŒ ๊ฐ€๋Šฅํ•˜๋‹ค.

๋ฌธ์ž์—ด ๊ด€๋ จ ํ•จ์ˆ˜๋“ค

# ๋ฌธ์ž ๊ฐœ์ˆ˜ ์„ธ๊ธฐ
a.count('b')
 
# ์œ„์น˜ ์•Œ๋ ค์ฃผ๊ธฐ 1
a.find('b')			# ์žˆ์œผ๋ฉด ์ฒ˜์Œ์œผ๋กœ ๋‚˜์˜จ ์œ„์น˜ ๋ฐ˜ํ™˜, ์—†์œผ๋ฉด -1
 
# ์œ„์น˜ ์•Œ๋ ค์ฃผ๊ธฐ 2
a.index('t')		# ์žˆ์œผ๋ฉด ์ฒ˜์Œ์œผ๋กœ ๋‚˜์˜จ ์œ„์น˜ ๋ฐ˜ํ™˜, ์—†์œผ๋ฉด ์—๋Ÿฌ
 
# ๋ฌธ์ž์—ด ์‚ฝ์ž…
a = ","
a.join('abcd')		# 'a,b,c,d'
 
# ์†Œ๋ฌธ์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ
a = "hi"
a.upper()			# 'HI'
 
# ๋ฌธ์ž์—ด ๋ฐ”๊พธ๊ธฐ
a = "Life is too short"
a.replace("Life", "Your leg")		# โ€™Your leg is too shortโ€™
 
# ๋ฌธ์ž์—ด ๋‚˜๋ˆ„๊ธฐ
a = "Life is too short"
a.split()						# [โ€™Lifeโ€™, โ€™isโ€™, โ€™tooโ€™, โ€™shortโ€™]
 
a = "a:b:c:d"
a.split(โ€™:โ€™) 				# [โ€™aโ€™, โ€™bโ€™, โ€™cโ€™, โ€™dโ€™]

๋ฌธ์ž์—ด ํ• ๋‹น

nameVar1 = 'Issac'
nameVar1 = "Issac"

๋ฌธ์ž์—ด ๊ธฐ๋ณธ ์—ฐ์‚ฐ์ž, ๋‚ด์žฅ ํ•จ์ˆ˜

  1. len() ์ž…๋ ฅ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฌธ์ž์—ด์„ ์ „๋‹ฌ ๋ฐ›๋Š”๋‹ค. ๊ธธ์ด๋ฅผ ๊ฒฐ๊ณผ๊ฐ’์œผ๋กœ ๋Œ๋ ค์ค€๋‹ค.
  2. + ๋‘ ๋ฌธ์ž์—ด์„ ํ•˜๋‚˜๋กœ ์—ฐ๊ฒฐํ•˜์—ฌ ๊ฒฐ๊ณผ๊ฐ’์„ ๋Œ๋ ค์ค€๋‹ค.
  3. * ๋ฌธ์ž์—ด์„ ๋’ค์— ๊ณฑํ•œ ์ˆ˜๋งŒํผ ๋ฐ˜๋ณตํ•œ ํ˜•ํƒœ์˜ ๋ฌธ์ž์—ด๋กœ ๋Œ๋ ค์ค€๋‹ค.
  4. int() ์ž…๋ ฅ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌ๋ฐ›์€ ๋ฌธ์ž์—ด์ด ์ˆซ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ๊ฒฝ์šฐ ์ •์ˆ˜ํƒ€์ž…์œผ๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ๋Œ๋ ค์ค€๋‹ค.
  5. float() ์‹ค์ˆ˜ ํƒ€์ž…์œผ๋กœ ๋ฐ”๊ฟ”์„œ ๋Œ๋ ค์ค€๋‹ค.

๋ฌธ์ž์—ด ํ• ๋‹น ์‹œ โ€™ ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ

  1. "" ์œผ๋กœ ๊ฐ์‹ผ๋‹ค.
  2. \' ์„ ์‚ฌ์šฉํ•œ๋‹ค.

Print() ํ•จ์ˆ˜

print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
  1. value,... ์ž…๋ ฅ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์—ฌ๋Ÿฌ๊ฐœ ์ค„ ์ˆ˜ ์žˆ๋‹ค.
  2. sep=' ' ๊ฐ๊ฐ์˜ ์ž…๋ ฅ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ถœ๋ ฅํ•  ๋•Œ์— ๊ตฌ๋ถ„ํ•˜๋Š” ๋ฌธ์ž์—ด์€ ๊ณต๋ฐฑ์ด๋‹ค.
  3. end='\n' ๋ชจ๋“  ์ž…๋ ฅ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ถœ๋ ฅํ•œ ์ดํ›„์— ๋งˆ์ง€๋ง‰์—๋Š” ๊ฐœํ–‰์„ ํ•˜๊ณ  ๋งˆ์นœ๋‹ค.
  4. file, flush ์ถ”ํ›„ ์„ค๋ช…

input() ํ•จ์ˆ˜

input() ํ•จ์ˆ˜์˜ ๋ฆฌํ„ด๊ฐ’์€ ๊ธฐ๋ณธ์ด ๋ฌธ์ž ์ด๋‹ค.

string = input("๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•˜์„ธ์š” : ")		# char
 
num_string = input("์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ")	# char
 
num = int(input("์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : "))	# int

๋ฌธ์ž์—ด ์ฃผ์˜ ์‚ฌํ•ญ

๋ฌธ์ž์—ด์€ ํ•œ๋ฒˆ ์ž…๋ ฅํ•˜๋ฉด ์ˆ˜์ •์ด ๋ถˆ๊ฐ€ํ•˜๋‹ค!

์‹ค์Šต

๋ฌธ์ž์—ด ๋’ค์ง‘๊ธฐ ํ”„๋กœ๊ทธ๋žจ ๋งŒ๋“ค๊ธฐ

๋ฌธ์ž์—ด์ด ๋“ค์–ด์˜ค๋ฉด ์•ž๋’ค๋ฅผ ๋’ค์ง‘์–ด์„œ ํ™”๋ฉด์— ์ถœ๋ ฅํ•œ๋‹ค.

def reverseString(string):
    result = ""
    for i in range(len(string)-1, -1, -1):
        result += string[i]
    return result

๋‚˜์ด ๊ณ„์‚ฐ ํ”„๋กœ๊ทธ๋žจ ๋งŒ๋“ค๊ธฐ

name = input("์ด๋ฆ„ ์ž…๋ ฅํ•ด!!! : ")
old = int(input("๋ช‡ ์‚ด์ด๋‹ˆ??? : "))
now_year = int(input("์˜ฌ ํ•ด ๋ช‡๋…„??? :"))
future_year = int(input("๊ถ๊ธˆํ•œ ๋…„์ˆ˜ : "))
 
def calcAge(old, now_year, future_year):
    result = old + (future_year - now_year)
    return result
 
 
print(calcAge(old, now_year, future_year), "์‚ด ์ž…๋‹ˆ๋‹ค.")

๋‚˜์ด ์ถœ๋ ฅ ํ”„๋กœ๊ทธ๋žจ ๋งŒ๋“ค๊ธฐ

  • ์ž…๋ ฅ : ์ฃผ๋ฏผ๋“ฑ๋ก๋ฒˆํ˜ธ ์•ž์ž๋ฆฌ๋ฅผ ๋ฐ›๋Š”๋‹ค.
  • ์ถœ๋ ฅ : ๋‹น์‹ ์˜ ์ƒ์ผ์€ OOOO๋…„ OO์›” OO์ผ ์ž…๋‹ˆ๋‹ค.
number = input("์ฃผ๋ฏผ๋“ฑ๋ก๋ฒˆํ˜ธ ์•ž์ž๋ฆฌ 6์ž๋ฆฌ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” : ")
 
def makeBirthdayString(number):
    #print("๋‹น์‹ ์˜ ์ƒ์ผ์€ ", "19"+number[:1]+"๋…„", int(number[2:3],"์›”", int(number[4:5]),"์ผ ์ž…๋‹ˆ๋‹ค.")
    print("๋‹น์‹ ์˜ ์ƒ์ผ์€ ", "19" , number[:2], "๋…„ ", int(number[2:4]), "์›” ", int(number[4:]), "์ผ ์ž…๋‹ˆ๋‹ค.", sep = "")
 
makeBirthdayString(number)

์‡ผํ•‘ ์ด์•ก ๊ณ„์‚ฐ ํ”„๋กœ๊ทธ๋žจ ๋งŒ๋“ค๊ธฐ

apple = int(input("how many apple?"))
banana = int(input("how many apple?"))
melon = int(input("how many apple?"))
peach = int(input("how many apple?"))
grape = int(input("how many apple?"))
 
def calcProductSum(apple, banana, melon, peach, grape):
    a = 3000
    b = 2500
    c = 10000
    d = 5000
    e = 4000
    result = apple * a +banana * b + melon * c + peach * d + grape * e
    return result

๋ฌธ์ž์—ด์—์„œ ํ•ด๋‹น ๋ฌธ์ž ์ฐพ๊ธฐ

  1. string method์ธ find()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ โ€˜tomatoโ€™๋‹จ์–ด ์•ˆ์— ์ฒซ๋ฒˆ์งธ โ€˜oโ€™ ์˜ ์œ„์น˜(์ธ๋ฑ์Šค)๋ฅผ ์ฐพ์•„๋‚ด์–ด ์ถœ๋ ฅํ•˜์‹œ์˜ค.
  2. string method์ธ find()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ โ€˜tomatoโ€™๋‹จ์–ด ์•ˆ์— ๋‘๋ฒˆ์งธ โ€˜oโ€™ ์˜ ์œ„์น˜(์ธ๋ฑ์Šค)๋ฅผ ์ฐพ์•„๋‚ด์–ด ์ถœ๋ ฅํ•˜์‹œ์˜ค.
print("tomato".find('o'))
print("tomato".find('o', "tomato".find('o')+1))
print("tomato".find('z'))
 
>>> 1
>>> 5
>>> -1

find ํ•จ์ˆ˜์˜ ์ž…๋ ฅ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์–ด๋””์„œ ๋ถ€ํ„ฐ ์กฐ์‚ฌํ• ์ง€ ๊ฐ’์„ ์ฃผ์–ด์ฃผ๋ฉด ๊ทธ ๋ถ€๋ถ„๋ถ€ํ„ฐ ์กฐ์‚ฌํ•œ๋‹ค. ๋งŒ์•ฝ์— ์—†๋Š” ๋ฌธ์ž์—ด์„ ์ฐพ๋Š”๋‹ค๋ฉด -1 ์„ ์ถœ๋ ฅํ•œ๋‹ค.