Program for Python String center() Method

In the previous article, we have discussed Python Program for casefold() Function
center() Function in Python:

The center () method will center align the string, using a specified character as the fill character (space is the default).

Syntax:

String.center(Length, character)

Parameters:

length: This is required. It is the length of the string returned.

character: This is optional. Fills the blanks on each side with a character. The default value is ” ” (space).

Note: This parameter is optional, and if omitted, the white spaces are used as the default parameter by the centre Function. Please specify the Characters you want to use in the remaining width if you want to change the default value.

Examples:

Example1:

Input:

Given first string = "python"
Given length = 30 
Given character = '-'

Output:

The above given first string is : python
The given first string after applying center() function: ------------python------------

Example2:

Input:

Given second string = "GOODMORNING"
Given length = 17
Given character = '#'

Output:

The above given second string is : GOODMORNING
The given second string after applying center() function: ###GOODMORNING###

Program for center() Function in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the first string as static input and store it in a variable.
  • Give the length as static input and store it in another variable.
  • Give the character as static input and store it in another variable.
  • Apply center () method to the given string for the given length and character which will center align the string.
  • Store it in another variable.
  • Print the above-given string.
  • Print the above-given string after applying the center() function.
  • Similarly, do the same for the other string and print the result string.
  • The Exit of Program.

Below is the implementation:

# Give the first string as static input and store it in a variable.
gvn_fststr = "python"
# Give the length as static input and store it in another variable.
gvn_lengt = 30
# Give the character as static input and store it in another variable.
gvn_chactr = '-'
# Apply center () method to the given string for the given length and character
# which will center align the string.
# Store it in another variable.
rslt_str1 = gvn_fststr.center(gvn_lengt, gvn_chactr)
# Print the above given string
print("The above given first string is :", gvn_fststr)
# Print the above given string after applying center() function.
print("The given first string after applying center() function:", rslt_str1)
# Similarly do the same for other string and print the result string.
gvn_scndstr = "GOODMORNING"
gvn_lengt2 = 17
gvn_chactr2 = '#'
rslt_str2 = gvn_scndstr.center(gvn_lengt2, gvn_chactr2)
print("The above given second string is :", gvn_scndstr)
print("The given second string after applying center() function:", rslt_str2)

Output:

The above given first string is : python
The given first string after applying center() function: ------------python------------
The above given second string is : GOODMORNING
The given second string after applying center() function: ###GOODMORNING###

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the first string as user input using the input() function and store it in a variable.
  • Give the length as user input using the int(input()) function and store it in another variable.
  • Give the character as user input using the input() function and store it in another variable.
  • Apply center () method to the given string for the given length and character which will center align the string.
  • Store it in another variable.
  • Print the above-given string.
  • Print the above-given string after applying the center() function.
  • Similarly, do the same for the other string and print the result string.
  • The Exit of Program.

Below is the implementation:

# Give the first string as user input using the input() function and store it in a variable.
gvn_fststr =  input("Enter some random string = ")
# Give the length as user input using the int(input()) function and store it in another variable.
gvn_lengt = int(input("Enter some random number = "))
# Give the character as user input using the input() function and store it in another variable.
gvn_chactr = input("Enter some random charcter = ")
# Apply center () method to the given string for the given length and character
# which will center align the string.
# Store it in another variable.
rslt_str1 = gvn_fststr.center(gvn_lengt, gvn_chactr)
# Print the above given string
print("The above given first string is :", gvn_fststr)
# Print the above given string after applying center() function.
print("The given first string after applying center() function:", rslt_str1)
# Similarly do the same for other string and print the result string.
gvn_scndstr = input("Enter some random string = ")
gvn_lengt2 =  int(input("Enter some random number = "))
gvn_chactr2 = input("Enter some random charcter = ")
rslt_str2 = gvn_scndstr.center(gvn_lengt2, gvn_chactr2)
print("The above given second string is :", gvn_scndstr)
print("The given second string after applying center() function:", rslt_str2)

Output:

Enter some random string = welcome
Enter some random number = 16
Enter some random charcter = !
The above given first string is : welcome
The given first string after applying center() function: !!!!welcome!!!!!
Enter some random string = PYTHONPLATFORM
Enter some random number = 8
Enter some random charcter = *
The above given second string is : PYTHONPLATFORM
The given second string after applying center() function: PYTHONPLATFORM

Go through our tutorial and learn about various Python String Method Examples and learn how to apply the knowledge while dealing with strings.