3

If have string stored as list under name

>>> name
['Aaron']

Using str(name) i get

>>> str(name)
"['Aaron']"

Output Required is

'Aaron'

Not

"['Aaron']"

Because my regular expression is not recognizing it as a string.

hacke john
  • 49
  • 1
  • 1
  • 5

3 Answers3

14

To join list of multiple elements (strings) in the list, you may use str.join as

>>> name = ['Aaron', 'Sheron']

#    v  to join the words in the list using space ' '
>>> ' '.join(name)
'Aaron Sheron'

However, you are having a list of just one element. In order to access the element at 0th index, you need to pass index as (PS: str.join will work here too, but it is not required):

>>> name = ['Aaron']

#        v fetch `0`th element in the list
>>> name[0]
'Aaron'

Please also refer:

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
6

You can also use:

''.join(name)

join joins all elements of the list into one string.

Nurjan
  • 5,889
  • 5
  • 34
  • 54
3

Already answered in this thread

print(''.join(name))
Community
  • 1
  • 1
Fahadsk
  • 1,099
  • 10
  • 24