-3

I need to convert 'a b c' to 'abc' so made this code:

string = 'a b c'

result = [ch for ch in string if (ch != ' ')]
print(type(result))

result = str(result)
print(type(result))

print(result)

result of this code is expected as:

<class 'list'>
<class 'str'>
'abc'

but result is as:

<class 'list'>
<class 'str'>
['a', 'b', 'c']

why result is printed list? this makes faults in other part of my code.

  • 1
    Unlike how `list(thing)` means "iterate over `thing` and put the elements in a list" and `tuple(thing)` means "iterate over `thing` and put the elements in a tuple", `str(thing)` doesn't mean "iterate over `thing` and put the elements in a string". You need to use `''.join(thing)` for that. – user2357112 Jul 07 '20 at 11:23
  • Does this answer your question? [Concatenate item in list to strings](https://stackoverflow.com/questions/12453580/concatenate-item-in-list-to-strings) – SiHa Jul 07 '20 at 11:38

6 Answers6

4

str() on a list does not magically turn said list into a no delimiter string. When you do str(['a', 'b', 'c']) - what you actually get is '['a', 'b', 'c']', which is indeed a string.

If you'd like the result to be 'abc' please, use .join.

''.join(['a', 'b', 'c'])

Output-

'abc'
Chase
  • 5,315
  • 2
  • 15
  • 41
1

Here result = [ch for ch in string if (ch != ' ')] you eliminate spaces and save the chars into list. But to have string again, you need to join them:

string = 'a b c'

result = [ch for ch in string if (ch != ' ')]

s = "".join(result)

print(s)  # abc

Here is the related doc page.

Sercan
  • 2,081
  • 2
  • 10
  • 23
1

you can try this :

string = 'a b c'
print(string.replace(' ' ,''))

Output :

abc
Bhargav Desai
  • 941
  • 1
  • 5
  • 17
0

you are getting a list cause you are using list comprehension. here I have split the string into spaces using the string method split which returns a list I have printed the type of the list and type of element in the list and joined them back using the string method join

string = 'a b c'

string = string.split(' ')
print(type(string))
print(type(string[0]))

print("".join(string))
Akash g krishnan
  • 469
  • 5
  • 16
0

The line of code

result = [ch for ch in string if (ch != ' ')]

is an example of a list comprehension in Python.

The for loop within this list takes each value from the string, one at a time, and places it into the list - this is why the list ['a', 'b', 'c'] appears in your code.

To obtain a string without spaces from a string with spaces:

string = 'a b c'
# obtain characters from the string that are not spaces
result = [ch for ch in string if (ch != ' ')]
# put the string back together without spaces
string_no_spaces = ''.join(result)
>>>'abc'
Homer
  • 398
  • 2
  • 6
0

What does str(['a', 'b', 'c']) actually do is, str calls up the __str__ method of the given object (which is list now) to its constructor which makes: '['a', 'b', 'c']' string.

why result is printed list? this makes faults in other part of my code.

What print does before dumping is str('['a', 'b', 'c']'), which effectively does nothing here, printing it just strips out ' making you think that it's a list.

tripulse
  • 1,059
  • 11
  • 18