The following contents, along with sample code, explain how to convert lists (arrays) of strings (str) and lists of numbers (int, float) to each other in Python.
- Convert a list of numbers to a list of strings
- Convert a number to a decimal string
- Converts numeric values to binary, octal, and hexadecimal strings
- Converts a numeric value to a string in exponential notation
- Convert a list of strings to a list of numbers
- Convert decimal string to numeric
- Converts binary, octal, and hexadecimal strings to numbers
- Converts strings in exponential notation to numerical values
- Convert only strings that can be converted to numbers
When generating a new list from a list, list comprehensions are simpler to write than for loops. The sample code in this article also uses list comprehensions. For details of list comprehensions, see the following article.
Note that lists can store different types of data and are strictly different from arrays. Use array (standard library) or NumPy in the following cases.
- I want to handle processes that require memory size and memory addresses.
- Want to handle arrays for numerical processing of large data sets, etc.
Convert a list of numbers to a list of strings
Convert a number to a decimal string
Use str() to convert from numeric to string.
In Python, numbers can be expressed in a variety of formats, including exponential, hexadecimal, and binary (hexadecimal and binary notation). str() conversion results in a string in normal decimal notation.
Depending on the number of digits, exponential notation may be used automatically.
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']
Converts numeric values to binary, octal, and hexadecimal strings
To convert to binary, octal, or hexadecimal (binary notation, octal notation, or hexadecimal notation) strings, the following methods are available.
bin()
oct()
hex()
format()
str.format()
With the format() function, it is possible to fill in the zeros and adjust the digits.
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']
Converts a numeric value to a string in exponential notation
As mentioned above, some cases may automatically be in exponential notation depending on the number of digits. However, to always convert to a string in exponential notation, use one of the following
format()
str.format()
For more information on the format() function and the string method str.format(), see the following article.
The number of digits of the mantissa part can be specified. If an uppercase E is used as an argument, the output string is also an uppercase 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 to a list of numbers
Convert decimal string to numeric
Use int() or float() to convert from string to number.
int() is a conversion to an integer, and float() is a conversion to a floating-point number.
In float(), strings with the integer part omitted are complemented with 0 for the integer part.
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]
Converts binary, octal, and hexadecimal strings to numbers
The second argument of int() can be a radix: 2 for binary, 8 for octal, and 16 for hexadecimal, converting a string to a number.
If 0 is specified, each of the following prefixed strings is converted to an integer.
0b
- binary digits
0o
- octal
0x
- hexadecimal
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]
Converts strings in exponential notation to numerical values
Strings in exponential notation can be converted directly with float() without the need for special specification.
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() will result in a ValueError.
If a new function is defined that returns false on error, only elements that can be converted can be converted to numbers and become elements of the list.
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]