python - How to convert list to string

ID : 1451

viewed : 47

Tags : pythonstringlistpython

Top 5 Answer for python - How to convert list to string

vote vote

91

By using ''.join

list1 = ['1', '2', '3'] str1 = ''.join(list1) 

Or if the list is of integers, convert the elements before joining them.

list1 = [1, 2, 3] str1 = ''.join(str(e) for e in list1) 
vote vote

81

>>> L = [1,2,3]        >>> " ".join(str(x) for x in L) '1 2 3' 
vote vote

79

L = ['L','O','L'] makeitastring = ''.join(map(str, L)) 
vote vote

63

On angular2@2.0.0-alpha.44:

Html-Binding will not work when using an {{interpolation}}, use an "Expression" instead:

invalid

<p [innerHTML]="{{item.anleser}}"></p> 

-> throws an error (Interpolation instead of expected Expression)

correct

<p [innerHTML]="item.anleser"></p> 

-> this is the correct way.

you may add additional elements to the expression, like:

<p [innerHTML]="'<b>'+item.anleser+'</b>'"></p> 

hint

HTML added using [innerHTML] (or added dynamically by other means like element.appenChild() or similar) won't be processed by Angular in any way except sanitization for security purposed.
Such things work only when the HTML is added statically to a components template. If you need this, you can create a component at runtime like explained in How can I use/create dynamic template to compile dynamic Component with Angular 2.0?

vote vote

60

Using [innerHTML] directly without using Angular's DOM sanitizer is not an option if it contains user-created content. The safeHtml pipe suggested by @GünterZöchbauer in his answer is one way of sanitizing the content. The following directive is another one:

import { Directive, ElementRef, Input, OnChanges, Sanitizer, SecurityContext,   SimpleChanges } from '@angular/core';  // Sets the element's innerHTML to a sanitized version of [safeHtml] @Directive({ selector: '[safeHtml]' }) export class HtmlDirective implements OnChanges {   @Input() safeHtml: string;    constructor(private elementRef: ElementRef, private sanitizer: Sanitizer) {}    ngOnChanges(changes: SimpleChanges): any {     if ('safeHtml' in changes) {       this.elementRef.nativeElement.innerHTML =         this.sanitizer.sanitize(SecurityContext.HTML, this.safeHtml);     }   } } 

To be used

<div [safeHtml]="myVal"></div> 

Top 3 video Explaining python - How to convert list to string

Related QUESTION?