Search
Duplicate

Strings

νƒœκ·Έ
λ¬Έμžμ—΄
Cut Me a Slice of String
λ¬Έμžμ—΄μ˜ νŠΉμ •λΆ€λΆ„λ§Œ 자λ₯΄κ³  μ‹Άλ‹€λ©΄ μ•„λž˜μ™€ 같은 ꡬ문을 μ‚¬μš©ν•˜λ©΄ λœλ‹€.
String[fisrt_index:last_index]
index μ—¬μ„œ μ•ŒνŒŒλ²³ ν•˜λ‚˜μ— index λ²ˆν˜Έκ°€ 맀겨져 μžˆλ‹€κ³  생각할 수 μžˆμ§€λ§Œ μ•„λž˜μ˜ 예λ₯Ό 보면 싀상은 λ‹€λ₯΄λ‹€λŠ” 것을 μ•Œ 수 μžˆλ‹€.
favorite_fruit = "blueberry" print(favorit_fruit[4:6]) # Output : be print(favorite_fruit[:4]) # Output: blue print (favorite_fruit[4:]) # Output: berry
Python
볡사
How Long is that String?
len() ν•¨μˆ˜λŠ” λ°°μ—΄μ˜ 길이λ₯Ό λ°˜ν™˜ν•΄μ€€λ‹€. μ΄λ•Œ μ£Όμ˜ν•΄μ•Ό ν•  점은 길이 β‰  λ°°μ—΄μ˜ λ§ˆμ§€λ§‰ 인덱슀 λΌλŠ” 것이닀. 배열은 0 λΆ€ν„° μ‹œμž‘ν•˜κΈ° λ•Œλ¬Έμ— λ¬Έμžμ—΄μ˜ λ§ˆμ§€λ§‰ 인덱슀 λ„˜λ²„λŠ” len([λ¬Έμžμ—΄]) -1 이닀.
Strings are Immutable
String 은 immutable ν•˜κΈ° λ•Œλ¬Έμ— λ¬Έμžμ—΄μ΄ λ°”λ€Œμ—ˆλ‹€λŠ” 것은 λ¬Έμžμ—΄ κ·Έ μžμ²΄κ°€ λ°”λ€κ²Œ μ•„λ‹ˆλΌ λ‹€λ₯Έ λ¬Έμžμ—΄μ„ μƒˆλ‘œ μƒμ„±ν–ˆλ‹€κ³  λ³Ό 수 μžˆλ‹€. λ¬Έμžμ—΄μ€ 일단 ν•œλ²ˆ λ§Œλ“€μ–΄μ§€λ©΄ 값이 λ°”λ€” 수 μ—†λ‹€.
Escape Characters
λ¬Έμžμ—΄μ— β€œ κ³Ό 같은 문자λ₯Ό ν¬ν•¨ν•˜κ³  싢을 수 μžˆλ‹€. μ΄λ•Œ \ λ₯Ό μΆ”κ°€ν•¨μœΌλ‘œμ¨ λ¬Έμžμ—΄μ— 포함 μ‹œν‚¬ 수 μžˆλ‹€.
Strings and Conditionals
ν•˜λ‚˜μ˜ String 이 λ‹€λ₯Έ String μ•ˆμ— μžˆλŠ”μ§€ μ•Œκ³ μ‹Άλ‹€λ©΄ in 을 μ‚¬μš©ν•˜λ©΄ λœλ‹€.
λ§Œμ•½ string1 κ³Ό string2 에 κ³΅ν†΅μœΌλ‘œ μžˆλŠ” μš”μ†Œλ₯Ό λ°°μ—΄λ‘œ λ‚˜νƒ€λ‚Έλ‹€κ³  μƒκ°ν•΄λ³΄μž. λ˜ν•œ μ΄λ•Œ κ·Έ κ³΅ν†΅μœΌλ‘œ μžˆλŠ” μš”μ†Œκ°€ string1 에 μ—¬λŸ¬κ°œ μžˆλ‹€κ³  κ°€μ •ν•˜μž. κ·Έλ ‡λ‹€λ©΄ λ°°μ—΄μ—λŠ” λ˜‘κ°™μ€ 값이 μ—¬λŸ¬λ²ˆ λ“€μ–΄κ°ˆ 것이닀. μ΄λŸ¬ν•œ 쀑볡을 μ—†μ• λ €λ©΄ μ–΄λ–»κ²Œ μ½”λ“œλ₯Ό μž‘μ„±ν•΄μ•Ό ν• κΉŒ? μ•„λž˜ μ˜ˆμ‹œ μ½”λ“œλ₯Ό 보자.
[μ˜ˆμ‹œ μ½”λ“œ]
def common_letters(string_one, string_two): common = [] for letter in string_one: if (letter in string_two) and not (letter in common): common.append(letter) return common common_letters("banana", "cream") # It will return ['a']
Python
볡사
String Methods
ꡬ문
string_name.string_method(arguments)
string λ©”μ„œλ“œ
μ—­ν• 
.lower()
λͺ¨λ“  λ¬Έμžμ—΄μ„ μ†Œλ¬Έμžλ‘œ λ°˜ν™˜ν•œλ‹€.
.upper()
λͺ¨λ“  λ¬Έμžμ—΄μ„ λŒ€λ¬Έμžλ‘œ λ°˜ν™˜ν•œλ‹€.
.title()
각각의 단어 μ²«κΈ€μžλ₯Ό λŒ€λ¬Έμžλ‘œ, λ‚˜λ¨Έμ§€λ₯Ό μ†Œλ¬Έμžλ‘œ λ°˜ν™˜ν•œλ‹€.
.split()
λ¬Έμžμ—΄μ˜ 단어λ₯Ό λ°°μ—΄λ‘œ λ°˜ν™˜ν•œλ‹€. () μ•ˆμ— λ¬Έμžμ—΄μ΄ λ“€μ–΄κ°€λ©΄ κ·Έ λ¬Έμžμ—΄μ„ κΈ°μ€€μœΌλ‘œ λ¬Έμžμ—΄μ„ 잘라 λ°°μ—΄λ‘œ λ°˜ν™˜ν•œλ‹€. (() μ•ˆμ˜ λ¬Έμžμ—΄μ€ 배열에 ν¬ν•¨λ˜μ§€ μ•ŠμœΌλ©°, () μ•ˆμ˜ λ¬Έμžκ°€ λ¬Έμžμ—΄μ˜ 맨 끝에 μžˆλ‹€λ©΄ λ°°μ—΄μ˜ 맨 λ§ˆμ§€λ§‰ μš”μ†ŒλŠ” β€˜β€™ 이닀.)
.join()
ꡬ문은 β€˜delimiter’.join([λ°°μ—΄λͺ…]) 이닀. delimiter λ₯Ό κΈ°μ€€μœΌλ‘œ λ°°μ—΄μ•ˆμ˜ λ¬Έμžμ—΄λ“€μ„ 합쳐쀀닀.
.strip()
λ¬Έμžμ—΄μ—μ„œ () μ•ˆμ˜ argument λ₯Ό 벗겨낸 결과값을 좜λ ₯ν•œλ‹€.(() μ•ˆμ— μ•„λ¬΄λŸ° argument 도 μ—†λ‹€λ©΄ 곡백을 벗겨낸닀.)
.replace()
ꡬ문은 [λ¬Έμžμ—΄ λͺ…].replace(substring_being_replacesd, new_substring) 이닀. λ¬Έμžμ—΄μ— μ‘΄μž¬ν•˜λŠ” 첫번째 argument λ₯Ό λ‘λ²ˆμ§Έ argument 둜 μ „λΆ€ λ°”κΎΌλ‹€.
.find()
λ¬Έμžμ—΄μ— μžˆλŠ” νŠΉμ • μš”μ†Œμ˜ 인덱슀 값을 λ°˜ν™˜ν•œλ‹€. (μ—†λ‹€λ©΄ -1 을 λ°˜ν™˜ν•œλ‹€.)
.format()
argument λ₯Ό νŠΉμ • ν˜•νƒœλ‘œ λ°˜ν™˜ν•΄μ€€λ‹€. (μ•„λž˜ μ˜ˆμ‹œλ₯Ό ν™•μΈν•˜κΈ°)
list(set())
μ§‘ν•©ν•¨μˆ˜μ΄λ©°, λ¬Έμžμ—΄μ˜ 쀑볡 값이 제거된 리슀트λ₯Ό λ°˜ν™˜ν•΄μ€€λ‹€.
.split() 예제
[예제 μ½”λ“œ]
authors = "Audre Lorde,Gabriela Mistral,Jean Toomer,An Qi,Walt Whitman,Shel Silverstein,Carmen Boullosa,Kamala Suraiyya,Langston Hughes,Adrienne Rich,Nikki Giovanni" # author μ΄λ¦„μ˜ 배열을 λ§Œλ“ λ‹€. # λ°°μ—΄μ˜ ν˜•μ‹ : ["이름 μ„±", "이름 μ„±" . . . ] author_names = authors.split(',') print(author_names) # 전체 μ΄λ¦„μ—μ„œ μ„±λ§Œ 뽑아내고 μ‹Άλ‹€λ©΄, 각각의 이름을 μ„±κ³Ό μ΄λ¦„μœΌλ‘œ λ‚˜λˆ μ•Ό 할것이닀. # split() λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜λ©΄ 배열은 λ‹€μŒκ³Ό 같아진닀. # [["이름 μ„±"], ["이름 μ„±"] . . .] # name = ["이름 μ„±"] # name.split() = ["이름", "μ„±"] # name.split()[-1] = ["μ„±"] author_last_names = [] for name in author_names: author_last_names.append(name.split()[-1]) print(author_last_names)
Python
볡사
use escape sequences in split method
β€’
\n Newline 을 κΈ°μ€€μœΌλ‘œ split
β€’
\t Tab 을 κΈ°μ€€μœΌλ‘œ split
νŠΉμ •ν•œ dataset 을 μ‚¬μš©ν•˜λŠ” 데 μœ μš©ν•˜λ‹€.
[예제 μ½”λ“œ]
smooth_chorus = \ """And if you said, "This life ain't good enough." I would give my world to lift you up I could change my life to better suit your mood Because you're so smooth""" chorus_lines = smooth_chorus.split('\n') print(chorus_lines) # Output: # ['And if you said, "This life ain\'t good enough."', # 'I would give my world to lift you up', # 'I could change my life to better suit your mood', "Because you're so smooth"]
Python
볡사
μƒˆλ‘­κ²Œ μƒμ„±λœ λ°°μ—΄μ—λŠ” μ›λž˜ λ¬Έμžμ—΄μ˜ 각 쀄이 λ°°μ—΄μ˜ μš”μ†Œκ°€ λœλ‹€.
Python은 μƒˆ λͺ©λ‘μ„ λ§Œλ“€ λ•Œ 첫 번째 μ€„μ˜ ' 문자λ₯Ό μžλ™μœΌλ‘œ μ΄μŠ€μΌ€μ΄ν”„ν•˜κ³  ν°λ”°μ˜΄ν‘œλ‘œ μ‘°μ •ν•œλ‹€. μ΄λŠ” 첫째쀄과 λ§ˆμ§€λ§‰ 쀄에 β€˜ 이 μžˆλŠ” 것을 보면 μ•Œ 수 μžˆλ‹€.
λ°°μ—΄μ˜ 첫번째 μš”μ†ŒλŠ” 'And if you said, "This life ain\'t good enough."' 이닀.
.format()
def favorite_song_statement(song, artist): return "My favorite song is {} by {}.".format(song, artist) print(favorite_song_statement("Smooth", "Santana")) # => "My favorite song is Smooth by Santana."
Python
볡사
λ³€μˆ˜κ°€ μœ„μΉ˜ν•΄μ•Ό ν•  μžλ¦¬λŠ” {} 둜 ν‘œν˜„ν•˜λ©°, λ§Œμ•½ parameter 의 μˆœμ„œμ™€ format λ©”μ„œλ“œμ˜ arguments 의 μœ„μΉ˜κ°€ λ‹€λ₯΄λ‹€λ©΄ 값을 μ •ν™•ν•˜κ²Œ μ§€μ •ν•΄μ€˜μ•Ό ν•œλ‹€. μ•„λž˜μ˜ μ˜ˆμ‹œλ₯Ό 보면 μ‰½κ²Œ 이해 갈 것이닀.
def poem_description(publishing_date, author, title, original_work): # parameter 의 μˆœμ„œμ™€ format λ©”μ„œλ“œμ˜ argument μœ„μΉ˜κ°€ λ‹€λ₯΄λ‹€! poem_desc = "The poem {title} by {author} was originally published in {original_work} in {publishing_date}.".format(publishing_date = publishing_date, author = author, title = title, original_work = original_work) return poem_desc author = "Shel Silverstein" title = "My Beard" original_work = "Where the Sidewalk Ends" publishing_date = "1974" my_beard_description = poem_description(publishing_date, author, title, original_work) print(my_beard_description) # Output: # The poem My Beard by Shel Silverstein was originally published in Where the Sidewalk Ends in 1974.
Python
볡사