OneBite.Dev - Coding blog in a bite size

format a string in python

Code snippet on how to format a string in python

example_string = "This is an example string"
formatted_string = example_string.title()
print(formatted_string)

This code sample formats a string in Python. The first line creates a variable called “example_string” and assigns it the value “This is an example string”. The second line uses the title() method to format the string and assigns the value of this to the “formatted_string” variable. The third line prints the value of the “formatted_string” variable, which should be “This Is An Example String”, the string with the first letter of each word capitalized. The title() method can be used on any string to make the first letter of each word capitalized.

python