Format a list in Python?

2

I have the following list in Python, which can be changed according to the user's data:

Dialer = [1,9,2,'.',1,6,8,'.',0,'.',2,6]

When I print the list I get this:

  

[1, 9, 2, '.', 1, 6, 8, '.', 0, '.', 2, 6]

How can I format the list to get something like this at the end?:

  

192.168.0.26

At the same time, how can I save this in a variable? Something like this:

Cadena = '192.168.0.26'
    
asked by Dyanko Cisneros Mendoza 10.11.2016 в 01:46
source

3 answers

3

If your list is strings:

dialer = ['1','9','2','.','1','6','8','.','0','.','2','6']
cadena = "".join(dialer)

print(cadena)
  

"192.168.0.26"

If your list contains integers and / or strings you must pass each integer within the list to str. You can do it with a for but as you already commented it that way I will do it with map

dialer = [1, 9, 2, '.', 1, 6, 8, '.', 0, '.', 2, 6]
cadena = "".join(map(str, dialer))

print(cadena)
  

'192.168.0.26'

    
answered by 10.11.2016 / 02:55
source
1

What you need to do is assign the list, then go through it by applying a join

Dialer = [1,9,2,'.',1,6,8,'.',0,'.',2,6]
str2 = "".join(str(x) for x in Dialer)
print(str2)
192.168.0.26 // salida

That should work

    
answered by 10.11.2016 в 02:07
0

An easier and more understandable solution is:

Dialer = [1,9,2,'.',1,6,8,'.',0,'.',2,6]
Cadena = ""
for i in Dialer:
    Cadena = Cadena + str(i) # donde i va a ser cada elemento de Dialer
print(Cadena) # imprimo la lista y ya está guardada en una variable también
    
answered by 24.09.2018 в 05:10