[Tutor] String to list conversion

Kent Johnson kent37 at tds.net
Tue Feb 24 16:33:44 CET 2009


On Tue, Feb 24, 2009 at 10:16 AM, Taylan Karaman
<taylankaraman at gmail.com> wrote:
> Hello,
>
> I am a beginner. And I am trying to get a user input converted to a list.
>
> print 'Enter your first name :'
> firstname = raw_input()
>
> So if the user input is
>
> firstname = 'foo'    ----------->should become-------->
> firstlist['f','o','o']

Strings behave as sequences of characters, so you can just do
firstname = 'foo'
firstlist = list(firstname)

If you just want to iterate over the letters, there is no need to
create a separate list, you can iterate over the string directly, e.g.
for letter in firstname:
  print letter

Kent


More information about the Tutor mailing list