๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด/Python

ํŒŒ์ด์ฌ ๋ฌธ์ž์—ด(string)์˜ ๋ชจ๋“  ๊ฒƒ

by ์„œ์•„๋ž‘๐Ÿ˜ 2023. 12. 1.

 

๋ฌธ์ž์—ด์€ Immutable

ํŒŒ์ด์ฌ์—์„œ ๋ฌธ์ž์—ด์€ immutable์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•˜๋ฉด ๋ฌธ์ž์—ด ์ž๋ฃŒํ˜•์€ ๊ทธ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ํ•œ๋ฒˆ ๋ฌธ์ž์—ด์ด ํ• ๋‹น๋˜๋ฉด ์š”์†Œ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๊ณ , ์น˜ํ™˜์˜ ํ˜•ํƒœ๋กœ ๋ณ€๊ฒฝํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด a = 'All you need is love' ๋ผ๊ณ  ํ–ˆ์„ ๋•Œ 'All you need is love'๋Š” ๋ฉ”๋ชจ๋ฆฌ ์ƒ์— ๋ฐ•์ œ(?)๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— a[0] = 'B' ์™€ ๊ฐ™์€ ๋™์ž‘์€ ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค. b = 'B' + a[1:] ๋กœ ์ฒซ ๊ธ€์ž 'A'๋ฅผ 'B'๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์€ ๋ฐ”๊ฟ€ ์ˆ˜ ์—†๋‹ค๋Š” ์ ๋งŒ ๊ธฐ์–ตํ•ด์ฃผ์„ธ์š”!

 

์ธ๋ฑ์‹ฑ

a = "Life is too short"
a[3]  # 'e'
a[-1]  # 't'

a[3] = 'f' # ์—๋Ÿฌ, ๋ฌธ์ž์—ด์€ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์Œ

 

์Šฌ๋ผ์ด์‹ฑ

a = "Life is too short"
a[0:3]   # 'Lif'
a[5:7]   # 'is'
a[5:]    # 'is too short'
a[:6]    # Life i
# Pithon -> python ๋ฌธ์ž์—ด ๋ณ€๊ฒฝ
a = "Pithon"	
a[:1] + 'y' + a[2:]    # python

 

ํฌ๋งทํŒ…

1. ์ˆซ์ž ๋ฐ”๋กœ ๋Œ€์ž…

"number is %d" % 5
"[%s] number is %d" % (tag, num)
"[%s] number is %s" % (tag, num)    # ๋ฌด์ ์˜ %s(๋ญ๋“  ๋จ)

 

2. format ํ•จ์ˆ˜๋กœ ๋Œ€์ž…

"number is {0}".format(num)
"[{0}]number is {1}".format(tag, num)
"[{tag}]number is {num}".format(tag=tag, num=num)
"{0:<10}".format("hi")    # 'hi        ' -> ์™ผ์ชฝ ํฌ๋งทํŒ…
"{0:>10}".format("hi")    # '        hi' -> ์˜ค๋ฅธ์ชฝ ํฌ๋งทํŒ…
"{0:^10}".format("hi")    # '    hi    ' -> ๊ฐ€์šด๋ฐ ์ •๋ ฌ
"{0:=^10}".format("hi")    # '====hi====' -> ๊ณต๋ฐฑ ์ฑ„์šฐ๊ธฐ
"{-:!<10}".format("hi")    # 'hi!!!!!!!!'  -> ๊ณต๋ฐฑ ์ฑ„์šฐ๊ธฐ
"{0:0.4f}".format(3.42134234)    # '3.4213' -> ์†Œ์ˆ˜์  ํ‘œํ˜„

 

3. f ๋ฌธ์ž์—ด

name = '์œ ์žฌ์„'
age = 30
f'๋‚ด ์ด๋ฆ„์€ {name}์ž…๋‹ˆ๋‹ค. ๋‚˜์ด๋Š” {age + 1}์ž…๋‹ˆ๋‹ค.'    # ๋‚ด ์ด๋ฆ„์€ ์œ ์žฌ์„์ž…๋‹ˆ๋‹ค.๋‚˜์ด๋Š” 31์ž…๋‹ˆ๋‹ค.
f'{"hi":<10}'    # ์™ผ์ชฝ
f'{"hi":>10}'    # ์˜ค๋ฅธ์ชฝ
f'{"hi":^10}'    # ๊ฐ€์šด๋ฐ ์ •๋ ฌ
f'{"hi":=^10}'    # ์ฑ„์šฐ๊ธฐ
f'{"hi":!^10}'    # ์ฑ„์šฐ๊ธฐ
y = 3.4213234
f'{y:0.4f}'    # '3.4213' -> ์†Œ์ˆ˜์  ํ‘œํ˜„

 

ํ•จ์ˆ˜

1. ๋ฌธ์ž์—ด ๊ฐœ์ˆ˜(count)

a = 'hobby'
a.count('b')    # 2

 

2.๋ฌธ์ž์—ด ์ฐพ๊ธฐ(find)

a.find('y')  # 4
a.find('f')  # -1(์—†๋Š” ๊ฒฝ์šฐ)

a.index('y')  # 4
a.index('f')  # ์˜ค๋ฅ˜ ๋ฐœ์ƒ

 

3. ๋ฌธ์ž์—ด ์‚ฝ์ž…(join)

','.join('abcd')  # a,b,c,d -> join์œผ๋กœ ๋ฌธ์ž์—ด ์‚ฌ์ด์— ์‰ผํ‘œ ์‚ฝ์ž…
','.join(['a','b','c','d'])  # a,b,c,d -> join์œผ๋กœ ๋ฌธ์ž์—ด ์‚ฌ์ด์— ์‰ผํ‘œ ์‚ฝ์ž…

 

4. ๋Œ€์†Œ๋ฌธ์ž ๋ณ€ํ™˜(upper, lower)

a = "hi"
a.upper()	# 'HI'
a.lower()	# 'hi'

 

5. ๊ณต๋ฐฑ ์ง€์šฐ๊ธฐ(strip)

a = "  hi"
a.lstrip()   # 'hi' -> ์™ผ์ชฝ ๊ณต๋ฐฑ ์ง€์šฐ๊ธฐ

a = "hi   "
a.rstrip()  # 'hi' -> ์˜ค๋ฅธ์ชฝ ๊ณต๋ฐฑ ์ง€์šฐ๊ธฐ

a = " hi "
a.strip()   # 'hi' -> ์–‘์ชฝ ๊ณต๋ฐฑ ์ง€์šฐ๊ธฐ

 

6. ๋ฌธ์ž์—ด ๋ฐ”๊พธ๊ธฐ(replace)

a = "Life is too short"
a.replace("Life", "Your leg")  # 'Your leg is too short'

# ๋ฌธ์ž์—ด์€ ๋ฐ”๊ฟ€ ์ˆ˜ ์—†์œผ๋ฏ€๋กœ replace๋Š” ๋‹ค๋ฅธ ๊ฐ’์œผ๋กœ ์ƒ์„ฑ ํ›„ ์น˜ํ™˜ํ•ด์ค€๋‹ค.

 

7. ๋ฌธ์ž์—ด ๋‚˜๋ˆ„๊ธฐ(split)

a = "Life is too short"
a.split()   # ['Life','is','too','short']  -> ์ธ์ž๊ฐ€ ์—†์œผ๋ฉด ๊ณต๋ฐฑ ๊ธฐ์ค€

b = "symbol,name,volume,tag"
b.split(',')    # ['symbol','name','volume','tag'] -> ๊ธฐํ˜ธ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด ๋‚˜๋ˆ”

 

 

์ฐธ๊ณ : ์ ํ”„ ํˆฌ ํŒŒ์ด์ฌ

๋Œ“๊ธ€