Hướng dẫn python format 3 digit

Format a number to 3 digits in Python #

Use a formatted string literal to format a number to 3 digits, e.g. result = f'{my_int:03d}'. The formatted string literal will format the number to the specified fixed length by adding leading zeros.

Copied!

from random import randint my_int = 5 # ✅ pad number with leading zeros [formatted-string literal] result = f'{my_int:03d}' print[result] # 👉️ '005' # ----------------------------------- # ✅ pad number with leading zeros [zfill] result = str[my_int].zfill[3] print[result] # 👉️ '005' # ---------------------------------- # ✅ get first 3 digits of number result = str[1234567][:3] print[result] # 👉️ 123 # ---------------------------------- # ✅ generate random 3 digit number result = randint[100, 999] print[result] # 👉️ 465 # ---------------------------------- # ✅ generate list of 3 digit numbers my_list = [f'{item:03d}' for item in range[10]] # 👇️ ['000', '001', '002', '003', '004', '005', '006', '007', '008', '009'] print[my_list]

The first example uses a formatted string literal to format a number to 3 digits by adding leading zeros.

Copied!

my_int = 5 result = f'{my_int:03d}' print[result] # 👉️ '005' print[f'{9:03d}'] # 👉️ 009

Formatted string literals [f-strings] let us include expressions inside of a string by prefixing the string with f.

Copied!

my_str = 'The number is:' my_int = 137 result = f'{my_str} {my_int}' print[result] # 👉️ The number is: 137

Make sure to wrap expressions in curly braces - {expression}.

Formatted string literals also enable us to use the format specification mini-language in expression blocks.

Alternatively, you can use the str.zfill[] method.

Format a number to 3 digits using str.zfill[] #

To format a number to 3 digits:

  1. Use the str[] class to convert the number to a string.
  2. Use the str.zfill[] method to format the number to 3 digits.
  3. The str.zfill[] method will format the number to 3 digits by left-filling it with 0 digits.

Copied!

my_int = 5 result = str[my_int].zfill[3] print[result] # 👉️ '005'

The str.zfill method takes the width of the string and left-fills the string with 0 digits to make it of the specified width.

Copied!

num = 13 result_1 = str[num].zfill[3] print[result_1] # 👉️ '013' result_2 = str[num].zfill[4] print[result_2] # 👉️ '0013'

Converting the number 13 to a string gives us a string with a length of 2.

Passing 3 as the width to the zfill[] method means that the string will get left-filled with a single 0 digit.

If you need to get the first 3 digits of an integer, convert the integer to a string and use string slicing.

Copied!

result = str[1234567][:3] print[result] # 👉️ '123' my_int = int[result] print[my_int] # 👉️ 123

The slice goes from index 0 up to, but not including the digit at index 3.

If you need to generate a random 3-digit number, use the randint[] function.

Copied!

from random import randint result = randint[100, 999] print[result] # 👉️ 465

The random.randint function takes 2 numbers - a and b as parameters and returns a random integer in the range.

Note that the range is inclusive - meaning both a and b can be returned.

If you need to generate a list of 3-digit numbers, use a list comprehension.

Copied!

my_list = [f'{item:03d}' for item in range[10]] # 👇️ ['000', '001', '002', '003', '004', '005', '006', '007', '008', '009'] print[my_list]

We used a list comprehension to iterate over a range of numbers.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we use a formatted string literal to pad the current number with leading zeros to a length of 3.

Chủ Đề