OneBite.Dev - Coding blog in a bite size

capitalize a string in python

Code snippet on how to capitalize a string in python

  myString = 'hello world'
  newString = myString.title()
  print(newString)

This code capitalizes the first letter of each word in a given string. The string “hello world” is stored in the variable myString. The .title() method is then called on myString, which capitalizes the first letter of each word and creates a new string stored in the variable newString. Finally, the newString is printed so the result is displayed. In this case, the result would be “Hello World”.

python