Python Program to Capitalize the First Character of a String

To understand this example, you should have the knowledge of the following Python programming topics:


Example 1: Using list slicing

my_string = "programiz is Lit"

print(my_string[0].upper() + my_string[1:])

Output

Programiz is Lit

In the above example, my_string[0] selects the first character and upper() converts it to uppercase. Likewise, my_string[1:] selects the remaining characters as they are. Finally they are concatenated using +.


Example 2: Using inbuilt method capitalize()

my_string = "programiz is Lit"

cap_string = my_string.capitalize()

print(cap_string)

Output

Programiz is lit

Note: capitalize() changes the first character to uppercase; however, changes all other characters to lowercase.

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges