Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)

I want to know the other way of concatenating string items in the form of the list into a single string. Is this str.join() function can be used?

Let's say this is the input:

 ['this','is','a','sentence'] 

And this is the desired output:

 this-is-a-sentence

sentence = ['this','is','a','sentence']

sent_str = ""

for i in sentence:

    sent_str += str(i) + "-"

sent_str = sent_str[:-1]

print sent_str

1 Answer

0 votes
by (107k points)

You can use the join() that will help you in concatenating the strings:

>>> sentence = ['this','is','a','sentence']

>>> '-'.join(sentence)

The output:

'this-is-a-sentence'

For more information regarding other in-built functions, you can refer the Python tutorial that will help you out in a better way. 

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...