+ 1
Converting a double digit int to a str(Python)
I am having an issue in which I am converting an int to str so I can add it to a list. However, once the number has double-digit it prints the digits separately. How can I fix this? code example number = 10 list_one.extend(str(number)) print(list_one) Output: ['1','0']
3 Réponses
+ 13
Ben Broz , the reason why list.extend("your_string") is separating the digits of the string-representation of an int:
extend() expects one argument, which has to be an ▶️ iterable ◀️. This is the case for strings. I suppose you converted the number to string, because by passing an int to the function, it will create an error.
You can use append as suggested, or you can use an expression like this:
list_one = [2,9,0]
number = 10
list_one.extend([number])
print(list_one)
#Output:
[2, 9, 0, 10]
+ 3
Use append() instead of extend()
+ 1
List can contain ints too, why do you need to convert them?
Anyway, the answer is:
list_one +=[str(number)]