Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Advanced Formatting: Float, Round, and Percent | Strings Formatting
String Manipulation in Python
course content

Зміст курсу

String Manipulation in Python

String Manipulation in Python

1. Basic Concepts
2. String Methods
3. Strings Formatting

Advanced Formatting: Float, Round, and Percent

Within a simple {} block, we can round a float number with the necessary precision, or represent a number as a percent.

Let's consider the pattern we will use in this chapter:

{:[thousands separator].[number][type]}

Please note, that like in the previous chapters, we don't need to place square brackets (I placed it for convenience).

  • [thousands separator] - the symbol used to separate every thousand (possible values are , and _).
  • [number] - is the precision, number of decimal places (used for rounding number).
  • [type] - type of number representing (e - scientific notation, % - percentage (will multiply number by 100), g - general format, f - fixed-point notation). You can dive deeper into possible options in Python documentation.
123
print("Original number: {0}, formatted number: {0:.2f}".format(255/8)) print("Original number: {0}, formatted number: {0:.2%}".format(15/48)) print("Original number: {0}, formatted number: {0:,.2f}".format(35*6327))
copy

Завдання

As of 2020, the population of the USA was 331002651. The total land area is 9147420 sq.km. Population density is the population-to-area ratio. Your tasks are:

  1. Format the first string so the population and area will be printed in format: 9,147,420, and insert variables in the correct order.
  2. Within the second .format function calculate the population density and format the number in format 28.45.

Завдання

As of 2020, the population of the USA was 331002651. The total land area is 9147420 sq.km. Population density is the population-to-area ratio. Your tasks are:

  1. Format the first string so the population and area will be printed in format: 9,147,420, and insert variables in the correct order.
  2. Within the second .format function calculate the population density and format the number in format 28.45.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

Секція 3. Розділ 8
toggle bottom row

Advanced Formatting: Float, Round, and Percent

Within a simple {} block, we can round a float number with the necessary precision, or represent a number as a percent.

Let's consider the pattern we will use in this chapter:

{:[thousands separator].[number][type]}

Please note, that like in the previous chapters, we don't need to place square brackets (I placed it for convenience).

  • [thousands separator] - the symbol used to separate every thousand (possible values are , and _).
  • [number] - is the precision, number of decimal places (used for rounding number).
  • [type] - type of number representing (e - scientific notation, % - percentage (will multiply number by 100), g - general format, f - fixed-point notation). You can dive deeper into possible options in Python documentation.
123
print("Original number: {0}, formatted number: {0:.2f}".format(255/8)) print("Original number: {0}, formatted number: {0:.2%}".format(15/48)) print("Original number: {0}, formatted number: {0:,.2f}".format(35*6327))
copy

Завдання

As of 2020, the population of the USA was 331002651. The total land area is 9147420 sq.km. Population density is the population-to-area ratio. Your tasks are:

  1. Format the first string so the population and area will be printed in format: 9,147,420, and insert variables in the correct order.
  2. Within the second .format function calculate the population density and format the number in format 28.45.

Завдання

As of 2020, the population of the USA was 331002651. The total land area is 9147420 sq.km. Population density is the population-to-area ratio. Your tasks are:

  1. Format the first string so the population and area will be printed in format: 9,147,420, and insert variables in the correct order.
  2. Within the second .format function calculate the population density and format the number in format 28.45.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

Секція 3. Розділ 8
toggle bottom row

Advanced Formatting: Float, Round, and Percent

Within a simple {} block, we can round a float number with the necessary precision, or represent a number as a percent.

Let's consider the pattern we will use in this chapter:

{:[thousands separator].[number][type]}

Please note, that like in the previous chapters, we don't need to place square brackets (I placed it for convenience).

  • [thousands separator] - the symbol used to separate every thousand (possible values are , and _).
  • [number] - is the precision, number of decimal places (used for rounding number).
  • [type] - type of number representing (e - scientific notation, % - percentage (will multiply number by 100), g - general format, f - fixed-point notation). You can dive deeper into possible options in Python documentation.
123
print("Original number: {0}, formatted number: {0:.2f}".format(255/8)) print("Original number: {0}, formatted number: {0:.2%}".format(15/48)) print("Original number: {0}, formatted number: {0:,.2f}".format(35*6327))
copy

Завдання

As of 2020, the population of the USA was 331002651. The total land area is 9147420 sq.km. Population density is the population-to-area ratio. Your tasks are:

  1. Format the first string so the population and area will be printed in format: 9,147,420, and insert variables in the correct order.
  2. Within the second .format function calculate the population density and format the number in format 28.45.

Завдання

As of 2020, the population of the USA was 331002651. The total land area is 9147420 sq.km. Population density is the population-to-area ratio. Your tasks are:

  1. Format the first string so the population and area will be printed in format: 9,147,420, and insert variables in the correct order.
  2. Within the second .format function calculate the population density and format the number in format 28.45.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

Within a simple {} block, we can round a float number with the necessary precision, or represent a number as a percent.

Let's consider the pattern we will use in this chapter:

{:[thousands separator].[number][type]}

Please note, that like in the previous chapters, we don't need to place square brackets (I placed it for convenience).

  • [thousands separator] - the symbol used to separate every thousand (possible values are , and _).
  • [number] - is the precision, number of decimal places (used for rounding number).
  • [type] - type of number representing (e - scientific notation, % - percentage (will multiply number by 100), g - general format, f - fixed-point notation). You can dive deeper into possible options in Python documentation.
123
print("Original number: {0}, formatted number: {0:.2f}".format(255/8)) print("Original number: {0}, formatted number: {0:.2%}".format(15/48)) print("Original number: {0}, formatted number: {0:,.2f}".format(35*6327))
copy

Завдання

As of 2020, the population of the USA was 331002651. The total land area is 9147420 sq.km. Population density is the population-to-area ratio. Your tasks are:

  1. Format the first string so the population and area will be printed in format: 9,147,420, and insert variables in the correct order.
  2. Within the second .format function calculate the population density and format the number in format 28.45.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 3. Розділ 8
Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt