Jens Braun

Learn Python quick


Скачать книгу

we are asking the interpreter to replace {0:s} with the parameter in position 0 and that it is a string (because the formatter is ‘s’).

      When we write {1:d}, we are referring to the parameter in position 1, which is an integer (formatter is d).

      When we write {2:4.2f}, we are referring to the parameter in position 2, which is a float and we want it to be formatted with 2 decimal places and a total length of 4 (formatter is 4.2f).

      If we print message, we’ll get

      The price of this Apple laptop is 1299 USD and the exchange rate is 1.24 USD to 1 EUR

      Note: If you do not want to format the string, you can simply write

      message = ‘The price of this {} laptop is {} USD and the exchange rate is {} USD to 1 EUR’.format(‘Apple’, 1299, 1.235235245)

      Here we do not have to specify the position of the parameters. The interpreter will replace the curly brackets based on the order of the parameters provided. We’ll get

      The price of this Apple laptop is 1299 USD and the exchange rate is 1.235235245 USD to 1 EUR

      The format() method can be kind of confusing to beginners. In fact, string formatting can be more fanciful than what we’ve covered here, but what we’ve covered is sufficient for most purposes. To get a better understanding of the format() method, try the following program.

      message1 = ‘{0} is easier than {1}’.format(‘Python’, ‘Java’)

      message2 = ‘{1} is easier than {0}’.format(‘Python’, ‘Java’)

      message3 = ‘{:10.2f} and {:d}’.format(1.234234234, 12)

      message4 = ‘{}’.format(1.234234234)

      print (message1)

      #You’ll get ‘Python is easier than Java’

      print (message2)

      #You’ll get ‘Java is easier than Python’

      print (message3)

      #You’ll get ‘ 1.23 and 12’

      #You do not need to indicate the positions of the parameters.

      print (message4)

      #You’ll get 1.234234234. No formatting is done.

      You can use the Python Shell to experiment with the format() method. Try typing in various strings and see what you get.

      Конец ознакомительного фрагмента.

      Текст предоставлен ООО «ЛитРес».

      Прочитайте эту книгу целиком, купив полную легальную версию на ЛитРес.

      Безопасно оплатить книгу можно банковской картой Visa, MasterCard, Maestro, со счета мобильного телефона, с платежного терминала, в салоне МТС или Связной, через PayPal, WebMoney, Яндекс.Деньги, QIWI Кошелек, бонусными картами или другим удобным Вам способом.

/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcG BwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwM DAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/wAARCA+gC7gDASIA AhEBAxEB/8QAHgABAAAGAwEAAAAAAAAAAAAAAAIGBwgJCgEEBQP/xAB+EAABAwMCAwQECAgJCAQE AS0AAQIDBAURBgcIEiEJEzFBFCJRYQoVGTJYcYGRGiNWlZeh1NUWOUJSd5axtdMXJDNicsHR8BiC kuE0Q1OFtvElJjVIVGNzhqKywyg2NzhEV3WTo6azxWZ0doPE5ScpVaSltMLSR2VnhIeU4v/EABsB AQADAQEBAQAAAAAAAAAAAAACAwQBBQYH/8QAPxEBAAICAAUDAwEHBAICAQALAAECAxEEEiExQQVR YRMicYEGFDKRobHBI