본문 바로가기
Python/Python_basic

문자열 함수

by Mr.DonyStark 2023. 11. 27.

□ 특정단어가 포함된 개수 : .count()

print(변수명.count('값'))

 

□ 특정단어가 최초 등장하는 순번 : .find()

print(변수명.find('값'))

 

□ 특정 문자열 길이 : .len()

print(len(변수명))

 

□ 특정 값 변경 : .replace()

some_string_edit = 변수명.replace("기존값", "대체값")

 

□ 공백제거 : .split()

blank_word = '   python   '
edit_blank_word = blank_word.strip()
print(edit_blank_word)

 

□ 예제

#문자열 string
some_string = """You may have watched grisly Korean thriller Squid Game or dark comedy Parasite. But did you know the music in these films was composed by the same man?
Squid Game - which follows a cast of debt-ridden contestants who play twisted children's games to win a huge cash prize - would not work without its hair-raising soundtrack.
Simple, childlike melodies are laid over scenes of terror. A soft, jazzy rendition of Fly Me to the Moon plays as contestants are massacred on-screen.
This spine-chilling contrast set the tone for the whole series.
Parasite's score features low-key piano pieces - emotional and tense - as a biting satire on class and capitalism unfolds.
A scene in which its cash-strapped protagonists hatch a dramatic plan to infiltrate a well-to-do family is set to The Belt of Faith, a semi-classical, semi-Baroque symphony.
These are seriously different sounds. But one person masterminded them both."""

#기능 : 특정단어가 포함된 개수
print(some_string.count('score'))
#기능 : 입력한 단어거 최초 등장하는 순번
print(some_string.find('o'))
#기능 : 문자열 길이
print(len(some_string))
#기능 : 특정 문자열 변경
some_string_edit = some_string.replace("You", "HaHaHaHoHoHo")
print(some_string_edit)