Converting a double digit int to a str(Python) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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']

4th Nov 2020, 6:26 PM
Ben Broz
Ben Broz - avatar
3 Answers
+ 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]
4th Nov 2020, 8:44 PM
Lothar
Lothar - avatar
+ 3
Use append() instead of extend()
4th Nov 2020, 6:48 PM
Vijay Raj Jain
+ 1
List can contain ints too, why do you need to convert them? Anyway, the answer is: list_one +=[str(number)]
4th Nov 2020, 6:50 PM
Shadoff
Shadoff - avatar