TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python String | center() method with Examples

Python String center() method with Examples on capitalize(), center(), count(), encode(), find(), format(), index(), join(), lower(), ljust(), isupper(), istitle(), isspace(), isprintable(), isnumeric(), islower() etc.

<< Back to PYTHON

Python String Center() Method

Python 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.

Signature

center(width[,fillchar])

Parameters

  • width (required)
  • fillchar (optional)

Return Type

It returns modified string.

Python String Center() Method Example 1:default fill char

Here, 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 2

Here, 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




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf