C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Python String capitalize() MethodPython capitalize() method converts first character of the string into uppercase without altering the whole string. It changes the first character only and skips rest of the string unchanged. Signaturecapitalize() ParametersNo parameter is required. Return TypeIt returns a modified string. Python String Capitalize() Method Example 1# Python capitalize() function example # Variable declaration str = "TheDeveloperBlog" # Calling function str2 = str.capitalize() # Displaying result print("Old value:", str) print("New value:", str2) Output: Old value: TheDeveloperBlog New value: TheDeveloperBlog Python String Capitalize() Method Example 2What if first character already exist in uppercase. # Python capitalize() function example # Variable declaration str = "TheDeveloperBlog" # Calling function str2 = str.capitalize() # Displaying result print("Old value:", str) print("New value:", str2) Output: Old value: TheDeveloperBlog New value: TheDeveloperBlog It returns the same string without any alteration. Python String Capitalize() Method Example 3What if first character is a digit or non-alphabet character? This method does not alter character if it is other than a string character. # Python capitalize() function example # Variable declaration str = "#TheDeveloperBlog" # Calling function str2 = str.capitalize() # Displaying result print("Old value:", str) print("New value:", str2) print("--------digit---------") str3 = "1-TheDeveloperBlog" str4 = str3.capitalize() print("Old value:", str3) print("New value:", str4) Output: Old value: #TheDeveloperBlog New value: #TheDeveloperBlog --------digit--------- Old value: 1-TheDeveloperBlog New value: 1-TheDeveloperBlog
Next TopicPython Strings
|