Python

[Python] String

llHoYall 2021. 10. 31. 22:24

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!

반응형