Преобразование «commander_k» в «Commander K» с примерами кода

Вот несколько методов с примерами кода:

Метод 1: использование функции Python «split()»

string = "commander_k"
words = string.split("_")
title = " ".join(words).title()
print(title)

Выход:

Commander K

Метод 2. Использование регулярных выражений Python (regex) и функции re.sub()

import re
string = "commander_k"
title = re.sub(r"_", " ", string).title()
print(title)

Выход:

Commander K

Метод 3. Использование функции Python replace()

string = "commander_k"
title = string.replace("_", " ").title()
print(title)

Выход:

Commander K

Метод 4. Использование функции Python join() и понимания списков

string = "commander_k"
title = ' '.join([word.capitalize() for word in string.split('_')])
print(title)

Выход:

Commander K