C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Python String Center() MethodPython center() method alligns string to the center by filling paddings left and right of the string. This method takes two parameters, first is a width and second is a fillchar which is optional. The fillchar is a character which is used to fill left and right padding of the string. Signaturecenter(width[,fillchar]) Parameters
Return TypeIt returns modified string. Python String Center() Method Example 1:default fill charHere, we did not pass second parameter. By default it takes spaces. # Python center() function example # Variable declaration str = "Hello TheDeveloperBlog" # Calling function str2 = str.center(20) # Displaying result print("Old value:", str) print("New value:", str2) Output: Old value: Hello TheDeveloperBlog New value: Hello TheDeveloperBlog Python String Center() Method Example 2Here, we are providing padding char (optional) parameter as #. See the example. # Python center() function example # Variable declaration str = "Hello TheDeveloperBlog" # Calling function str2 = str.center(20,'#') # Displaying result print("Old value:", str) print("New value:", str2) Output: Old value: Hello TheDeveloperBlog New value: ##Hello TheDeveloperBlog## Python String Center() Method Example 3# Python center() function example # Variable declaration str = "Hello TheDeveloperBlog" if str == "Hell0 TheDeveloperBlog": str2 = str.center(20,'#') else: str2 = str.center(20,'!') # Displaying result print("Old value:", str) print("New value:", str2) Output: Old value: Hello TheDeveloperBlog New value: !!Hello TheDeveloperBlog!!
Next TopicPython Strings
|