printf("ho_tari\n");

문자열 본문

Python

문자열

호타리 2023. 10. 16. 11:44

<yesterday.py>

f = open("chapter6/yesterday.txt", 'r')
yesterday_lyric = f.readlines()
f.close()

contents = ""

for line in yesterday_lyric:
    contents = contents + line.strip() + "\n"

n_of_yesterday = contents.upper().count("YESTERDAY")
print("Number of a Word 'Yesterday'", n_of_yesterday)

<yesterday.txt>

[Verse 1]
Yesterday
All my troubles seemed so far away
Now it looks as though they're here to stay
Oh, I believe in yesterday

[Verse 2]
Suddenly
I'm not half the man I used to be
There's a shadow hanging over me
Oh, yesterday came suddenly

[Bridge]
Why she had to go
I don't know, she wouldn't say
I said something wrong
Now I long for yesterday

[Verse 3]
Yesterday
Love was such an easy game to play
Now I need a place to hide away
Oh, I believe in yesterday

[Bridge]
Why she had to go
I don't know, she wouldn't say
I said something wrong
Now I long for yesterday

[Verse 3]
Yesterday
Love was such an easy game to play
Now I need a place to hide away
Oh, I believe in yesterday

[Outro]
Mmm-mmm-mmm-mmm-mmm, hmm-hmm

<compile 결과>

 

<formatting1.py>

print(1, 2, 3)
print("a" + " " + "b" + " " + "c")
print("%d %d %d" % (1, 2, 3))
print("{} {} {}".format("a", "b", "c"))

<compile 결과>

 

<formatting2.py>

print('%s %s' % ('one', 'two'))
print('%d %d' % (1, 2))

<compile 결과>

 

<formatting3.py>

print("I eat %d apples." % 3)
print("I eat %s apples." % "five")

<compile 결과>

 

<formatting4.py>

number = 3
day = "three"

print("I ate %d apples. I was sick for %s days." % (number, day))

<compile 결과>

 

<formatting5.py>

age = 24; name = 'Hotari'

print("I'm {0} years old.".format(age))
print("My name is {0} and {1} years old.".format(name, age))
print("Product: {0}, Price per unit: {1:.2f}.".format("Apple", 5.243))

<compile 결과>

 

'Python' 카테고리의 다른 글

파이썬 스타일 코드 I (join, split, list_comprehension, loop, enumerate)  (0) 2023.10.16
자료구조  (0) 2023.10.16
함수  (0) 2023.10.16
반복문  (0) 2023.10.12
조건문  (0) 2023.10.12