3

I have a list that contains a word. Each letter is separated by a space (as seen below).

word = ["h", " ", "e", " ", "l", " ", "l", " ", "o", " "]

I am trying to get it to print in the format:

h e l l o

I tried using a print statement (among other things) but it just came out:

["h", " ", "e", " ", "l", " ", "l", " ", "o", " "]

How do I fix this?

2 Answers 2

2

You could str.join(iterable) to join them together as one string:

"".join(word)

This will join all elements of the array with empty strings, essentially concatenating the strings together into one. Then you can print it:

print("".join(word))

This will produce

h e l l o
Sign up to request clarification or add additional context in comments.

Comments

1

Just use the join function to convert the List into a string:

print ("".join(my_word))

The "" before .join means that between the characters an empty space will be added. If you want you can put whatever you like, even spaces or digits or strings.

1 Comment

A minor note, but the OP tagged their question with python-3.x, meaning your code won't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.