note.nkmk.me

Convert a list of strings and a list of numbers to each other in Python

Posted: 2021-01-04 / Tags: Python, String, List

This article describes how to convert a list of strings (str) and a list of numbers (int, float) to each other in Python, with sample code.

  • Convert a list of numbers (int, float) to a list of strings (str)
    • Convert numbers to decimal strings
    • Convert numbers to binary, octal and hexadecimal strings
    • Convert numbers to exponential strings
  • Convert a list of strings (str) to a list of numbers (int, float)
    • Convert decimal strings to numbers
    • Convert binary, octal and hexadecimal strings to numbers
    • Convert exponential strings to numbers
    • Convert only strings that can be converted to numbers

List comprehension is useful to generate a new list from another list. The sample code in this article also uses the list comprehensions. See the following article for details.

See the following article for more details on the conversion from strings to numbers.

Sponsored Link

Convert a list of numbers (int, float) to a list of strings (str)

Convert numbers to decimal strings

Use str() to convert a number to a string.

Numbers are converted to decimal strings with str().

l_n = [-0.5, 0, 1.0, 100, 1.2e-2, 0xff, 0b11]

l_n_str = [str(n) for n in l_n]
print(l_n_str)
# ['-0.5', '0', '1.0', '100', '0.012', '255', '3']

Depending on the number of digits, it may be automatically expressed in exponential notation.

Convert numbers to binary, octal and hexadecimal strings

To convert numbers to strings in binary, octal, or hexadecimal notation, use bin(), oct(), hex() or format().

l_i = [0, 64, 128, 192, 256]

l_i_hex1 = [hex(i) for i in l_i]
print(l_i_hex1)
# ['0x0', '0x40', '0x80', '0xc0', '0x100']

l_i_hex2 = [format(i, '04x') for i in l_i]
print(l_i_hex2)
# ['0000', '0040', '0080', '00c0', '0100']

l_i_hex3 = [format(i, '#06x') for i in l_i]
print(l_i_hex3)
# ['0x0000', '0x0040', '0x0080', '0x00c0', '0x0100']

Convert numbers to exponential strings

As mentioned above, some numbers are automatically converted to exponential notation depending on the number of digits. You can always convert them to exponential strings with format().

If you specify E instead of e as an argument, the output string will also be E.

l_f = [0.0001, 123.456, 123400000]

l_f_e1 = [format(f, 'e') for f in l_f]
print(l_f_e1)
# ['1.000000e-04', '1.234560e+02', '1.234000e+08']

l_f_e2 = [format(f, '.3E') for f in l_f]
print(l_f_e2)
# ['1.000E-04', '1.235E+02', '1.234E+08']

Convert a list of strings (str) to a list of numbers (int, float)

Convert decimal strings to numbers

Use int() or float() to convert a string to a number.

l_si = ['-10', '0', '100']

l_si_i = [int(s) for s in l_si]
print(l_si_i)
# [-10, 0, 100]

l_sf = ['.123', '1.23', '123']

l_sf_f = [float(s) for s in l_sf]
print(l_sf_f)
# [0.123, 1.23, 123.0]

Convert binary, octal and hexadecimal strings to numbers

You can specify the radix in the second argument of int(). Convert a string to a number as binary if the second argument is 2, octal if 8 or hexadecimal if 16.

If 0 is specified as the second argument of int(), strings prefixed with 0b, 0o or 0x are converted to integers as binary, octal or hexadecimal numbers, respectively.

l_sb = ['0011', '0101', '1111']

l_sb_i = [int(s, 2) for s in l_sb]
print(l_sb_i)
# [3, 5, 15]

l_sbox = ['100', '0b100', '0o77', '0xff']

l_sbox_i = [int(s, 0) for s in l_sbox]
print(l_sbox_i)
# [100, 4, 63, 255]

Convert exponential strings to numbers

Strings in exponential notation can be converted directly with float().

l_se = ['1.23e3', '0.123e-1', '123']

l_se_f = [float(s) for s in l_se]
print(l_se_f)
# [1230.0, 0.0123, 123.0]

Convert only strings that can be converted to numbers

Passing a string that cannot be converted to a number to int() or float() raises ValueError.

By defining a function that returns False on error, only elements that can be converted will be converted to numbers.

def is_int(s):
    try:
        int(s)
    except ValueError:
        return False
    else:
        return True

def is_float(s):
    try:
        float(s)
    except ValueError:
        return False
    else:
        return True

l_multi = ['-100', '100', '1.23', '1.23e2', 'one']

l_multi_i = [int(s) for s in l_multi if is_int(s)]
print(l_multi_i)
# [-100, 100]

l_multi_f = [float(s) for s in l_multi if is_float(s)]
print(l_multi_f)
# [-100.0, 100.0, 1.23, 123.0]

See the following article for exception handling try ... except ....

Sponsored Link
Share

Related Categories

Related Articles