2

How do I join all the strings in a stringList into one without printing it?

For example,

s = joinStrings([’very’, ’hot’, ’day’]) # returns string 
print(s)                                # Veryhotday

here is the actual problem my professor gave me

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • 4
    Strings already have a method that does this. `''.join([’very’, ’hot’, ’day’])`. just use that. – tdelaney Feb 10 '17 at 00:44
  • 1
    For questions regarding tertiary-level Python assignment problems, you may want to try **reading** the relevant documentation / paying attention in course. This is one of the first things you should learn in Python, and is easily found on Google. – Obsidian Age Feb 10 '17 at 00:53

4 Answers4

16

it feels a little backwards, but you join with a chosen uhh seperator ''.join(['your','list','here']) you can fill in the '' and it will use what ever is inside between each pair of items i.e '---'.join(['your','list','here']) will produce your---list---here

Nullman
  • 4,179
  • 2
  • 14
  • 30
6

You can solve it using single line for loop.

def joinStrings(stringList):
    return ''.join(string for string in stringList)

Everything is described in Python Documentation: Python Docs

E.g.: String join method: Python string methods

Jacek Zygiel
  • 153
  • 1
  • 9
1

Unfortunately, I am only learning python 2.7 so this probably won't help:

def joinStrings(stringList):
    list=""
    for e in stringList:
        list = list + e
    return list

s = ['very', 'hot', 'day']
print joinStrings(s)
-1

All the above solution are good... You can also use extend(object) function...

String1.extend(["very", "hot", "day"] )

Enjoy....

Jeroen Heier
  • 3,520
  • 15
  • 31
  • 32