capitalize() returns a copy of the string with only the first character capitalized. Remember, strings are immutable so the original string is not altered.
The capitalize function does not have any required or optional parameters and is always called as .capitalize()
# example # open your favourite Python editor and use the interactive window >>> my_sentence = "hello, my name is Glen" >>> my_sentence.capitalize() 'Hello, my name is glen'
NOTICE that it also forced the rest of the characters after the initial capitalized character to be lowercase.
If we print out the my_sentence variable we can see that even though we called the capitalize function it did not alter the original string.
>>> print my_sentence hello, my name is Glen
Assign the capitalized string to a new variable…
>>> my_new_sentence = my_sentence.capitalize() >>> print my_new_sentence Hello, my name is glen
or overwrite the original…
>>> my_sentence = my_sentence.capitalize() >>> print my_sentence Hello, my name is glen
Next: center()
Previous: Strings
Notes:
– does not take any required or optional parameters
– capitalizes the first character of a string only
– forces all other alphabetical characters to lowercase
– does not alter the original string
– capitalizes the first character only is it is from the alphabet
i.e. it will do nothing if first character is a digit or a symbol.