Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Need help converting a string to a list, deleting, then converting back to an intelligible string.

Kenneth mentions in one of the early Python videos that in order to delete something from a string using this method, you must convert to a list, delete, then convert back to a string. I tried this and it worked, but my final string looks terrible because everything's broken into individual characters (like a list...)

How could I clean this up?

myString = 'I shall delete X from this sentence'

myString 'I shall delete X from this sentence'

myList = list(myString)

myList ['I', ' ', 's', 'h', 'a', 'l', 'l', ' ', 'd', 'e', 'l', 'e', 't', 'e', ' ', 'X', ' ', 'f', 'r', 'o', 'm', ' ', 't', 'h', 'i', 's', ' ', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e'] myList.remove("X")

myList ['I', ' ', 's', 'h', 'a', 'l', 'l', ' ', 'd', 'e', 'l', 'e', 't', 'e', ' ', ' ', 'f', 'r', 'o', 'm', ' ', 't', 'h', 'i', 's', ' ', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e']

myNewString = str(myList)

myNewString "['I', ' ', 's', 'h', 'a', 'l', 'l', ' ', 'd', 'e', 'l', 'e', 't', 'e', ' ', ' ', 'f', 'r', 'o', 'm', ' ', 't', 'h', 'i', 's', ' ', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e']"

Python 2.7.10 (default, Oct 23 2015, 19:19:21) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> myString = 'I shall delete X from this sentence'
>>> myString    
'I shall delete X from this sentence'
>>> myList = list(myString)
>>> myList
['I', ' ', 's', 'h', 'a', 'l', 'l', ' ', 'd', 'e', 'l', 'e', 't', 'e', ' ', 'X', ' ', 'f', 'r', 'o', 'm', ' ', 't', 'h', 'i', 's', ' ', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e']
>>> myList.remove("X")
>>> myList
['I', ' ', 's', 'h', 'a', 'l', 'l', ' ', 'd', 'e', 'l', 'e', 't', 'e', ' ', ' ', 'f', 'r', 'o', 'm', ' ', 't', 'h', 'i', 's', ' ', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e']
>>> "".join(myList)
'I shall delete  from this sentence'
>>> 

1 Answer

HI Robert,

you can use the join() method.

" ".join(myList)

Grigorij

Or you can create a for loop that adds every letter from list to a new String. But this approach is very inefficient and overcomplicated.

for letter in list:
    print(letter, end='')

Thanks. This did the trick.

I see that it works, but I guess I still don't understand why. So.... by using " ".join(myList) we're basically telling Python to join the list back into a string and to remove the whitespace between each character?

Hi Robert,

you are right. join takes an iterable thing as an argument. Usually it's a list.

join connects strings together with a separator. The separator is what you place right before the join.

You can choose a separator. Like '&' or '%' or ' :) '

See the result:

>>> myString = 'I shall delete X from this sentence'
>>> list = list(myString)
>>> list
['I', ' ', 's', 'h', 'a', 'l', 'l', ' ', 'd', 'e', 'l', 'e', 't', 'e', ' ', 'X', ' ', 'f', 'r', 'o', 'm', ' ', 't', 'h', 'i', 's', ' ', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e']
>>> '&'.join(list)
'I& &s&h&a&l&l& &d&e&l&e&t&e& &X& &f&r&o&m& &t&h&i&s& &s&e&n&t&e&n&c&e'
>>> '%'.join(list)
'I% %s%h%a%l%l% %d%e%l%e%t%e% %X% %f%r%o%m% %t%h%i%s% %s%e%n%t%e%n%c%e'
>>> '   :)   '.join(list)
'I   :)       :)   s   :)   h   :)   a   :)   l   :)   l   :)       :)   d   :)   e   :)   l   :)   e   :)   t   :)   e   :)       :)   X   :)       :)   f   :)   r   :)   o   :)   m   :)       :)   t   :)   h   :)   i   :)   s   :)       :)   s   :)   e   :)   n   :)   t   :)   e   :)   n   :)   c   :)   e'
>>> 

The last one is my favorite :)

G