The center() Function is used to centrally align a String.
x = "hello" y = x.center(10) print(y)
In the above code, we have declared a String 'hello'.
Then we have used the 'x.center(10)' function to reserve '10' blocks and place 'hello' in between.
And what about the empty places in left and right? Well! They would be filled by white spaces ( i.e. ' ')
And assign to variable 'y'.
So, 10 blocks are reserved for the String and the left side of the String 'hello' is filled with 2 white spaces and the right side is filled with 3 white spaces.
And, we print the value of 'y'.
And we got the below output.
Now, let us say we want to place '10' Asterix '*', and insert the String 'hello' in between them.
x = "hello" y = x.center(10,'*') print(y)
In the above code, we have declared a String 'hello'.
Then we have used the 'x.center(10,'*')' function to reserve '10' blocks and place 'hello' in between.
And what about the empty places in left and right? Well! They would be filled by Asterix '*' this time.
And assign to variable 'y'.
So, 10 blocks are reserved for the String and the left side of the String 'hello' is filled with 2 Asterix '*' and the right side is filled with 3 Asterix '*'.
And, we print the value of 'y'.
And we got the below output.