Back

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

How might I turn a string (like 'hello') into the following list (like [h,e,l,l,o])?

1 Answer

0 votes
by (26.4k points)

The list() function will change over a string into a list of single-character strings. 

>>> list('hello')

['h', 'e', 'l', 'l', 'o']

https://docs.python.org/3/library/functions.html#list

Indeed, even without changing them over to lists, strings as of now act like lists severally. For instance, you can get to individual characters (as single-character strings) utilizing brackets: 

>>> s = "hello"

>>> s[1]

'e'

>>> s[4]

'o'

You can likewise loop over the characters in the string so that you can loop over the components of a list:

>>> for c in 'hello':

...     print c + c,

... 

hh ee ll ll oo

Interested to learn python in detail? Come and Join the python course.

Related questions

0 votes
1 answer
0 votes
1 answer
+1 vote
1 answer

Browse Categories

...