PYTHON Formatowanie ciągów znaków – ćwiczenia interaktywne
Co będzie wynikiem działania następujących fragmentów kodu języka Python? Jeżeli błąd wpisz error.
napis = 'x = {} i y = {}'.format (45, 2.3)
print(napis)
napis = 'x = {} i y = {}'.format ('kot', 12)
print(napis)
napis = 'x = {} i y ='.format (10, 'pies')
print(napis)
napis = 'x = {} i y = {}'.format (2.9 ,'pies', 4)
print(napis)
tinfpre_startnapis = 'x = {} i y = {}'.format ('chomik')
print(napis)tinfpre_koniec
napis = 'x = {1} i y = {0}'.format ( 5.6 , 'kot')
print(napis)
napis = 'x = {2} i y = {1}'.format ( 10, 25, 'pies')
print(napis)
napis = 'x = {0} i y = {0}'.format ( 123, 4)
print(napis)
napis = 'x = {0} i y = {1}'.format ([2, 'lis'], 3)
print(napis)
napis = '{1} ma już {0} lat'.format (18 , 'Maciek')
print(napis)
napis = 'x = {1:5.2f} i y = {0:3d}'.format (14 , 3.5)
print(napis)
napis = 'x = {0:3d} i y = {1:2.3f}'.format (2 , 2.1)
print(napis)
napis = 'x = {0:5d} i y = {1:3d}'.format ('pies' , 3.4)
print(napis)
napis = 'x = {0:6s} i y = {1:4.3f} '.format ('miś' , 1.23456)
print(napis)
napis = 'x = {1:4d} i y = {0:4s}'.format ('kot' , 2.0)
print(napis)
napis = 'x = {0:%<7d} i y = {1:@>8d}'.format(7, 21)
print(napis)
napis = 'x = {0:#^6d} i y = {1:@>7.4f}'.format(4, 1.1)
print(napis)
napis = 'x = {0:*^8s} i y = {1:?<8.2f}'.format('kos', 4.256)
print(napis)
a = 'Jacek'
b = 'chory'
napis = '{0:*^10} jest {1:+^9s}'.format(a,b)
print(napis)
a = 'Bartek'
b = 'wysoki'
napis = '{0:5s} jest {1:5s} i {}'.format(a,b)
print(napis)
napis = 'x = {1:*>6.4f} i y = {1:+5.3f}'.format(1.4, -9.12345678)
print(napis)
napis = 'x = {1:*3d} i y = {0:-4.2f}'.format(3, -0.2)
print(napis)
napis = 'x = {0:+4.2f} i y = {1:+5.3f}'.format(2.11, -3.22)
print(napis)
napis = 'x = {0:-3.1f} i y = {1:+3.2f}'.format(5.12, -1.75)
print(napis)
napis = 'x = {0: 3d} i y = {1:+4d}'.format(123, -12)
print(napis)
print('x{2:}y{1:}z'.format('A','B','C'))
print('x{1:z^9}y'.format('jeden','dwa'))
print('{0:0.2f}'.format(12.12345))
print('A{1:}y{0:}B'.format('a','b','c'))
print('{2}{1}' .format('kos', 1.23, 'kot'))
print('x{0:z^8d}Y'.format(4321))
print('A{0:x^9.2f}B'.format(98.7654321))
print('A{0:,}B'.format(12345678))
print('A{1:C>10.3f}B'.format(12.34,567.8910))
print('A{0:Z^8}B'.format('dcba'))
print('X{0:Y<7d}Z'.format(123))
print('x{0:z<10.3f}y'.format(765.432345))
print('x{0:0.0f}y'.format(2.3456))
print('xx{0:C<+7}yy'.format(+12))
print('AA{1:x^5}BB'.format(123,'kot'))
print('A{0:C<8.2f}B'.format(12.345678))
print('A{0:0.0f}B'.format(5.67))
print('AA{0:C<7}BB'.format(987))
print('A{0:1.3f}B'.format(12.34567))
print('x{0:z>6d}y'.format(4321))
print('AA{0:+5.3f}BB'.format(123.456789))
print('xx{1:B>5}YY'.format(12))
print('A{0:x<8.2f}B'.format(12.3456789))
print('A{1:6}B'.format(12))
print('x{0:z^-6d}y'.format(4321))
print('{1}AA{0}{1}BB' .format('kot', 'kos', 2))
print('{0:#>6d}'.format(23))
print('X{0:Y^9}Z'.format('abc'))
print('A{0:C<7d}B'.format(4321))
print('A{0:C>7d}B'.format(5432))