Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert list into string in python3

Tags:

python

Right now I am having a list

>>> deints
[10, 10, 10, 50]

I want to print it as 10.10.10.50. I made it as

Method 1

>>> print(str(deints[0])+'.'+str(deints[1])+'.'+str(deints[2])+'.'+str(deints[3]))
10.10.10.50

Are there any other ways we can acheivie this ?

Thank you

like image 415
rɑːdʒɑ Avatar asked Apr 15 '26 16:04

rɑːdʒɑ


2 Answers

You can do it with:

print('.'.join(str(x) for x in deints))
like image 50
Keiwan Avatar answered Apr 17 '26 07:04

Keiwan


This is very simple. Take a look at str.join

print '.'.join([str(a) for a in deints])

Citation from the docs:

str.join(iterable)

Return a string which is the concatenation of the strings in the iterable iterable. The separator between elements is the string providing this method.

like image 44
ForceBru Avatar answered Apr 17 '26 06:04

ForceBru