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
볡μ¬