Left justify a Python string

using ljust method for string

We can format a string to make it left justify by using ljust method. ljust has the following syntax.

s.ljust(width [, fill])

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


s = 'buildConnectionString'
print s.ljust(30)
print s.ljust(20)

The code above generates the following result.


hexStr = "3f8"
print "\nHex String: " + hexStr.upper().ljust(8,'0')

The code above generates the following result.





















Home »
  Python »
    Data Types »




Data Types
String
String Format
Tuple
List
Set
Dictionary