본문 바로가기
Python

[Python] String

by llHoYall 2021. 10. 31.

Let's take a look at the string in Python.

Changing Case

We can simply change the case of string via upper() and lower() functions.

test_string = "Hello, World"
print(test_string.upper())
# HELLO, WORLD
print(test_string.lower())
# hello, world

It works well in the English alphabet case.

What about the other language in Unicode?

test_string = "ß"
print(test_string.upper())
# SS
print(test_string.lower())
# ß

This is the German alphabet.

As you can see in this example, the lower() function doesn't work.

Then, how should we do that for it?

test_string = "ß"
print(test_string.upper())
# SS
print(test_string.casefold())
# ss

If you consider Unicode string, casefold() function is the better choice than lower().

And, do you want to swap the cases?

test_string = "Hello, World"
print(test_string.swapcase())
# hELLO, wORLD

Use swapcase() function!

'Python' 카테고리의 다른 글

[Python] finterstellar를 사용하여 RSI로 주식 매매 시그널 만들기  (0) 2021.12.09
[Python] Singleton Pattern  (0) 2021.11.02
[Python] Testing  (0) 2021.10.30
[Python] Debugging  (0) 2021.10.27
[Python] logging  (0) 2021.10.25

댓글