35217

How to transform elements of a list into one string?

Question:

community,

I have a list, that consists of different strings (sentences, words...) I would like to join them all together to one string. I tried:

kafka = ['Das ist ein schöner Tag.', '>>I would like some ice cream and a big cold orange juice!',...] ''.join(kafka) #but strings stay the same as before

Answer1:

You need to do:

kafka = ['Das ist ein schöner Tag.', '>>I would like some ice cream and a big cold orange juice!',...] s = ''.join(kafka)

s now contains your concatenated string.

Answer2:

Basically you are on the right track, but you need to assign the join operation to a new variable or use it directly. the original list won't be affected.

concatenate them with or without any spaces in between:

no_spaces = ''.join(kafka) with_spaces = ' '.join(kafka) print no_spaces print with_spaces

Answer3:

The reason is that

''.join(kafka)

returns the new string without modifying kafka. Try:

my_string = ''.join(kafka)

And if you want spaces between sentences then use:

my_string = ' '.join(kafka)

Answer4:

''.join(kafka) returns a string. To make use of it, store it into a variable:

joined = ''.join(kafka) print joined

Note: you may want to join with a space ' ' instead.

Answer5:

You can join it using ,

Assuming your list is

['Das ist ein schöner Tag.', '>>I would like some ice cream and a big cold orange juice!']

>>> kafka = ['Das ist ein schöner Tag.', '>>I would like some ice cream and a big cold orange juice!'] >>>' '.join(kafka)

Output :

'Das ist ein sch\xf6ner Tag. >>I would like some ice cream and a big cold orange juice!'

Recommend

  • PHP cURL not returning XML
  • Clingo: assert partial constraints
  • StanfordCoreNLP differs from StanfordCoreNLPServer
  • Change an element's background color when clicking a different element
  • JuiceUI Change Theme / Style
  • R data.table how to replace positive values with column names across multiple binary data columns
  • ViewPager, compatibility package and ice cream sandwich
  • NER interfere with REGEXNER
  • What is the difference when adding a filter criteria to an outer join instead of a where clause?
  • Recommended way to insert elements into map [duplicate]
  • Saxon produces empty XML from Schematron
  • asort not working properly alphabetically
  • Limit SQL query to only the top two counts per group [duplicate]
  • Node Tutorial one to Many has no loading the remote server Stream Spinner moving endless
  • Processing input before giving input to parser
  • Does WEBSQL support Phonegap Apps on Ice Cream Sandwich?
  • Simple Normalizing Text Example Does Not Work
  • Deep link into eBay iOS app with custom URL scheme?
  • Incrementing value and setting it as var to onClick event
  • How to implement a filter from another stream using RxJava
  • Showing a concatenated string from multiple values of observable array
  • Extracting infromation from multiple JSON files to single CSV file in python
  • How to prompt user that edits have been made upon changing pages or sorting in Kendo Grid
  • concatenating select menus into a single form input
  • WebApp in AppServices vs CloudService
  • How to distribute an event to all nodes in a (Wildfly) cluster?
  • How do i find all references to a user control
  • How to concat Pandas dataframe columns
  • Action Pack components in Rails
  • IE6 changes DOCTYPE to a bad one
  • Android full screen on only one activity?
  • Atlas images wrong size on iPad iOS 9
  • Get one-time binding to work for ng-if
  • Volley JsonObjectRequest send headers in GET Request
  • Importing jscolor library in angular 2
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?