I am trying to get list of numbers from:
numbers= 1,2
to:
'1','2'
I tried ",".join(str(n) for n in numbers)
but it wont give the targeted format.
Use the join() method of Python. First convert the list of integer into a list of strings( as join() works with strings only). Then, simply join them using join() method.
The extend method() adds all the elements of the list till the end. To concatenate the list on integers “+” is used. The print(newlist) is used to get the output.
Use the Built-In Map Function to Print a List in Python. If you want to print a list of integers, you can use a map() function to transform them into strings. Then you can use the join() method to merge them into one string and print them out.
Python Join List. We can use the python string join() function for joining a list of the strings. However, this function takes iterable as an argument and the list is iterable, so we are able to use it with the list.
How about that?
>>> numbers=1,2 >>> numbers (1, 2) >>> map(str, numbers) ['1', '2'] >>> ",".join(map(str, numbers)) '1,2'
>>> numbers = 1,2 >>> print ",".join("'{0}'".format(n) for n in numbers) '1','2'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With