How to use Python String center method

Center a string

We can align a string to the center with center method. It has the following syntax.

s.center(width [, fill])

It centers string s in a field of the given width; pads on left and right with character fill (which defaults to a space). The String formatting expression and method can achieve similar effects.


string1 = "java2s.com"
#   w w  w  . j  a  v a  2  s .co m
print string1.center( 50 )
print string1.rjust( 50 )
print string1.ljust( 50 )

The code above generates the following result.





















Home »
  Python »
    Data Types »




Data Types
String
String Format
Tuple
List
Set
Dictionary