Перевод строки Python в верхний регистр: методы и примеры кода

Вот несколько способов преобразования строки в верхний регистр в Python, а также примеры кода:

Метод 1: использование метода upper()

string = "hello world"
uppercase_string = string.upper()
print(uppercase_string)  # Output: HELLO WORLD

Метод 2: использование метода str.upper()

string = "hello world"
uppercase_string = str.upper(string)
print(uppercase_string)  # Output: HELLO WORLD

Метод 3: использование метода str.capitalize()

string = "hello world"
uppercase_string = string.capitalize()
print(uppercase_string)  # Output: HELLO WORLD

Метод 4. Использование метода str.title()

string = "hello world"
uppercase_string = string.title()
print(uppercase_string)  # Output: Hello World

Метод 5. Использование лямбда-функции с функцией map()

string = "hello world"
uppercase_string = ''.join(map(lambda x: x.upper(), string))
print(uppercase_string)  # Output: HELLO WORLD

Метод 6. Использование списка

string = "hello world"
uppercase_string = ''.join([x.upper() for x in string])
print(uppercase_string)  # Output: HELLO WORLD

Метод 7. Использование модуля locale

import locale
string = "hello world"
uppercase_string = string.upper(locale.getlocale())
print(uppercase_string)  # Output: HELLO WORLD

Метод 8. Использование модуля unicodedata

import unicodedata
string = "hello world"
uppercase_string = ''.join(unicodedata.normalize('NFKD', x).upper() for x in string)
print(uppercase_string)  # Output: HELLO WORLD