To convert the first character in a string to uppercase in Python, you can use string slicing and the upper()
method.
In this example,
str1
with a value of "hello world"
.str1
to uppercase using string slicing and the upper()
method.str1 = "hello world"
modifiedStr1 = str1[0].upper() + str1[1:]
print("Modified string is:", modifiedStr1)
Modified string is: Hello world
In this example,
str2
with a value of "python"
.str2
to uppercase using string slicing and the upper()
method.str2 = "python"
modifiedStr2 = str2[0].upper() + str2[1:]
print("Modified string is:", modifiedStr2)
Modified string is: Python
In this tutorial, we learned How to convert First Character in String to Uppercase in Python language with well detailed examples.