- Home
- Course Detail
regularpython@gmail.com
You are now watching:
Python Strings / of python string formats
''' Basic formatting Value conversion Padding and aligning strings Truncating long strings Combining truncating and padding Numbers Padding numbers Signed numbers Named placeholders Getitem and Getattr Datetime Parametrized formats Custom objects '''
#====================================================== #Basic format # # old style print('Old style : %s %s' % ('one', 'two')) # #new style print('New style : {} {}'.format('one', 'two')) # # # # # # #old style print('Old style : %d %d' % (1, 2)) # #New Style print('New style : {} {}'.format(1, 2))
#====================================================== # #Padding and aligning strings # #Right print('%10s' % ('test')) print('{:>10}'.format('test')) # # # # # # #left print('%-10s' % ('test',)) print('{:10}'.format('test')) # # print('{:.<10}'.format('test'))#this is not available in old styel print('{:^10}'.format('test'))#this is not available in old styels
#====================================================== # # Truncating long strings print('%.5s' % ('python is a program',)) print('{:.5}'.format('python is a program'))
#====================================================== # # Combining truncating and padding print('%-10.5s' % ('xylophone',)) print('{:10.5}'.format('xylophone')) # # # With Numbers print('%d' % (42,)) print('{:d}'.format(42)) # # #with floats print('%f' % (3.141592653589793,)) print('{:f}'.format(3.141592653589793))
#====================================================== # # Padding numbers print('%4d' % (42,)) print('{:4d}'.format(42)) # # #floats print('%06.2f' % (3.141592653589793,)) print('{:06.2f}'.format(3.141592653589793)) # print('%04d' % (42,)) print('{:04d}'.format(42))
# ======================================================== # # Signed numbers print('%+d' % (42,)) print('{:+d}'.format(42)) # print('% d' % ((- 23),)) print('{: d}'.format((- 23))) # print('{:=5d}'.format((- 23))) #not available in old style. print('{:=+5d}'.format(23)) #not available in old style.
#======================================================= # # Named placeholders data = {'first': 'Hodor', 'last': 'Hodor!'} print('%(first)s %(last)s' % data) # print('{first} {last}'.format(**data)) # # print('{first} {last}'.format(first='Hodor', last='Hodor!'))#not available in old style
#========================================================== # # Getitem and Getattr person = {'first': 'Jean-Luc', 'last': 'Picard'} print('{p[first]} {p[last]}'.format(p=person)) #not available in old style # data = [4, 8, 15, 16, 23, 42] print('{d[4]} {d[5]}'.format(d=data)) #Not available in old style # # class Plant(object): type = 'tree' print('{p.type}'.format(p=Plant())) #Not available in old class Plants(object): type = 'tree' kinds = [{'name': 'oak'}, {'name': 'maple'}] print('{p.type}: {p.kinds[0][name]}'.format(p=Plants())) #Not available in old
#======================================================================== # # Datetime from datetime import datetime print('{:%Y-%m-%d %H:%M}'.format(datetime(2001, 2, 3, 4, 5))) #NOt available in old
#======================================================================== # # Datetime from datetime import datetime print('{:%Y-%m-%d %H:%M}'.format(datetime(2001, 2, 3, 4, 5))) #NOt available in old
#========================================================================= # # Parametrized formats print('{:{align}{width}}'.format('test', align='^', width='10')) #Not available in old # print('%.*s = %.*f' % (3, 'Gibberish', 3, 2.7182)) #old style print('{:.{prec}} = {:.{prec}f}'.format('Gibberish', 2.7182, prec=3)) #New Style # # # width and precision print('%*.*f' % (5, 2, 2.7182)) #old print('{:{width}.{prec}f}'.format(2.7182, width=5, prec=2)) #New # # print('{:{prec}} = {:{prec}}'.format('Gibberish', 2.7182, prec='.3')) #this is not available in old
#=================================================================================== # Custom objects class HAL9000(object): def __format__(self, format): if (format == 'open-the-pod-bay-doors'): return "I'm afraid I can't do that." return 'HAL 9000' print('{:open-the-pod-bay-doors}'.format(HAL9000()))