์ƒˆ์†Œ์‹

๐Ÿ ํŒŒ์ด์ฌ (Python)

ํŒŒ์ด์ฌ (Python) ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๋ฌธ์ž์—ด ํ•จ์ˆ˜ (์ฝ”๋”ฉํ…Œ์ŠคํŠธ, ์•Œ๊ณ ๋ฆฌ์ฆ˜)

  • -

 

*๋ฌธ์ž์—ด (String)

 

 

1. ๋Œ€๋ฌธ์ž, ์†Œ๋ฌธ์ž

str.upper() : str์„ ๋Œ€๋ฌธ์ž๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

str.lower() : str์„ ์†Œ๋ฌธ์ž๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

str.isupper() : str์ด ๋Œ€๋ฌธ์ž์ธ์ง€ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค. (True or False๋กœ ๋ฐ˜ํ™˜)

str.islower() : str์ด ์†Œ๋ฌธ์ž์ธ์ง€ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค. (True or False๋กœ ๋ฐ˜ํ™˜)

 

 

 

 

*๋Œ€๋ฌธ์ž๋Š” ์†Œ๋ฌธ์ž๋กœ, ์†Œ๋ฌธ์ž๋Š” ๋Œ€๋ฌธ์ž๋กœ ์ถœ๋ ฅํ•˜๊ธฐ

string = "Apple, IPhone7"

for i in string:
    if i.islower() : print(i.upper(),end="") 
    elif i.isupper() : print(i.lower(),end="")
    else: print(i,end="")   # ์ˆซ์ž์ถœ๋ ฅ์„ ์œ„ํ•œ else๋ฌธ
    
>>>>>>์ถœ๋ ฅ๊ฒฐ๊ณผ : aPPLE, ipHONE7

 

 

2. ๋ฌธ์ž์—ด ์ˆ˜์ • ๋ฐ ์œ„์น˜(index)์ฐพ๊ธฐ

 

str.replace(old , new) : str์†์˜ ๋ฌธ์ž์—ด์ค‘, old๋ฅผ ์ฐพ์•„์„œ new๋กœ ๋ฐ”๊ฟ”์ค๋‹ˆ๋‹ค.

*i am์„ You are๋กœ ์ˆ˜์ •ํ•˜๊ธฐ

string = "i am happy"
new_string = string.replace("i am", "You are")
print(new_string)

>>>>>>>์ถœ๋ ฅ๊ฒฐ๊ณผ : You are happy

 

 

 

str.find(์ฐพ์„๋ฌธ์ž)

str.index(์ฐพ์„๋ฌธ์ž)

*index์™€ find๋กœ ๋ฌธ์ž์—ด ์œ„์น˜ ์ฐพ๊ธฐ

string = "i am happy"
print(string.index("am"))
print(string.find("happy"))


>>>>>>>>์ถœ๋ ฅ๊ฒฐ๊ณผ
2
5

find์™€ index๋Š” ๋‘˜๋‹ค ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ํ•˜์ง€๋งŒ, ์ฐจ์ด์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

string์— ํฌํ•จ๋˜์ง€ ์•Š์€ ๋ฌธ์ž๋ฅผ ์ฐพ๊ฒŒ๋˜๋ฉด index()๋Š” ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒํ•˜์ง€๋งŒ, find()๋Š” -1์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  index()๋Š” list์—์„œ๋„ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ find๋Š” ๋ฌธ์ž์—ด์—์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

 

3.๋ฌธ์ž์—ด ๊ฑฐ๊พธ๋กœ, ์—ญ์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•˜๊ธฐ (reversed)

*์Šฌ๋ผ์ด์‹ฑ [::-1]

string = "i am happy"
print(string[::-1])

>>>>>>>>>>>>>์ถœ๋ ฅ๊ฒฐ๊ณผ
yppah ma i

*reversed()

string = "i am happy"
print("".join(reversed(string))

>>>>>>>>>>>>>์ถœ๋ ฅ๊ฒฐ๊ณผ
yppah ma i

๊ทธ๋ƒฅ reversed(string)์„ ์ถœ๋ ฅํ•˜๊ฒŒ๋˜๋ฉด, ๋ฉ”๋ชจ๋ฆฌ๊ฐ’์ด ๋‚˜์˜ค๊ฒŒ๋ฉ๋‹ˆ๋‹ค. "".join()์„ ์ด์šฉํ•ด์ค˜์•ผํ•ฉ๋‹ˆ๋‹ค.

Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.