
Python center() - Center a string in Python
The center()
returns the centered string filled with the specified string.
s = 'apple'
s7 = s.center(7, '$')
s8 = s.center(8, '$')
s9 = s.center(9, '$')
s10 = s.center(10, '$')
s11 = s.center(11, '$')
print(s7) # $apple$
print(s8) # $apple$$
print(s9) # $$apple$$
print(s10) # $$apple$$$
print(s11) # $$$apple$$$
The first argument is the length of return string. apple
has 5 letters so center(9, '$')
returns a 9 letter string filled with $
in the left and right.
Comments
Powered by Markdown