Sobes.tech
Junior — Middle

How can you convert the first letter of a string to uppercase in Python?

sobes.tech AI

Answer from AI

In Python, to convert the first letter of a string to uppercase, the capitalize() method is used. It returns a new string where the first character is converted to uppercase, and the rest are in lowercase.

Example:

text = "пример строки"
capitalized_text = text.capitalize()
print(capitalized_text)  # Outputs: "Пример строки"

If you only want to make the first letter uppercase and leave the rest unchanged, you can use slices:

text = "пример строки"
result = text[0].upper() + text[1:]
print(result)  # "Пример строки"