python
print "{:<10}".format("python")
# => "python "
print "{:!<10}".format(123)
# => "123!!!!!!!"
python
print "{:>10}".format("python")
# => " python"
print "{:0>10}".format(123)
# => "0000000123"
python
print "{:^10}".format("python")
# => " python "
print "{:#^10}".format('python')
# => "##python##"
String methods (2.7.x) String method (3.5.1)
python
print '1'.zfill(5)
# => 00001
print '1'.rjust(5)
# => 1
print '1'.ljust(5)
# => 1
print 'python'.center(10, '#')
# => ##python##
format "This string formatting method is a new standard in Python 3.0, and new code is better than formatting with% as specified in string formatting operations." [^ 1]
python
print ('address:{city},{state},{country}'.format(city='Osaka-shi', state='Osaka', country='Japan'))
# => address:Osaka-shi,Osaka,Japan
print ('address:{0},{1},{2}'.format('Osaka-shi', 'Osaka', 'Japan'))
# => address:Osaka-shi,Osaka,Japan
[^ 1]: String method (str.format)
Recommended Posts