Utility of "" .join ()


I would like to know the usefulness and functionality of this method (actually I do not know if it is a method) ".".join . I have seen it in several examples and forums but its concept has not yet become clear to me. If you could give me an example of its usefulness as simple as possible I would appreciate it.

In particular I am trying to understand its functionality with the following example:

 #Complementariedad de cadenas de ADN
#solicitar al usuario la cadena:
 sequence = str(input('Enter DNA sequence:'))

# creacion del diccionario con sensibilidad de mayusculas y minisculas.

dic = {"A":"T" or "t", 
      "G":"C", "a": "T", "t": "A", "c": "G", "g": "C" }
#salida de datos. Cadena complementaria.
print(''.join([dic[bases] for bases in sequence]))
asked by user7491985 24.09.2017 в 19:41

1 answer


str.join is an instance method of the class str that receives as an input argument an iterable that contains strings and returns a string by joining the elements of the iterable with the string that owns the method:

>>> iterable = ["1", "2", "3", "4"]
>>> cadena_de_unión = " - "
>>> resultado = cadena_de_unión.join(iterable)
>>> resultado
'1 - 2 - 3 - 4'

If the union string is an empty string, it will be limited to concatenating the iterable elements in a single string.

In your case, for an entry AGGTCCGAA the code (list by compression):

[dic[bases] for bases in sequence]

returns a list of strings:

['T', 'C', 'C', 'A', 'G', 'G', 'C', 'T', 'T']

When on that list applies "".join() is limited to joining (concatenate) each element of the list (since the union string is an empty string), that is:

answered by 24.09.2017 / 20:05