We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

# convert string to list

Student_101 asked
on
Medium Priority
1,883 Views
Last Modified: 2012-05-06
Hi Experts,

I need some help with converting a string to a list,

for example

alpha("1up2down33sideways77")  to return something like ["up", "down", "sideways"].
Comment
Watch Question

## View Solutions Only

Top Expert 2008
Commented:
Try this:
``````def alpha(s):
res = ''
for c in s:
if c.isalpha(): res+=c
else: res+=' '
return res.split()

print(alpha('1up2down33sideways77'))
``````

Not the solution you were looking for? Getting a personalized solution is easy.

Commented:
hi cxr,

I am actually just trying to learn Python, so I was trying many things,

I will verify the code that you sent me, at the same time, I can give you 50 points for each question that I have,

1) I still need to know how to reverse a list,

2) and for example, if I have a string that has letters and digits, how Do I just get the sum of the digits

3) If I have values with strings and digits, I want to pass the string and get the digits assoicated to it

4) If I have a list of strings, how do I make the first letter capital in all of them, and rest all lower case, for example `pyThon`` to ``Python``

Thanks for your help CXR

also, can you provide any link that provides beginners some guidance to Python.

This question is worth 500 points now

Top Expert 2008
Commented:
1) A list object have many builtin methods, one of them is called "reverse". This method will reverse the list in-place, it will not return a list! It can be used like this:

L = [1,2,3,4]
L.reverse()
print(L)  # outputs [4, 3, 2, 1]

2) Loop over the string like in the alpha() function. Use string method .isdigit() to test if the character is a digit. Transform any non-digit to a space, and use .split() to get the numbers. Sum the numbers in a simple loop.

3) What is "values with strings and digits"? When you want a group of something associated with something, you could use a list of tuples or a dictionary, if each key is unique.

4) One of the builtin string methos are called "capitalize", another is called "lower". If you start with "pyTHon" and first lower it, then capitalize it, you get "Python". This can be done in one operation like this: "pyTHon".lower().capitalize()

The documentation following every python install is rather good. Start with the "Tutorial", then read the "Library Reference".

python.org also have a lot of documentation.

http://wiki.python.org/moin/BeginnersGuide

Commented:
Cxr,
you gave me the code on how I could print the up down sideways, but what if i wanted the program to return a list whose elements are those substrings of the given string?? The string that has the consecutive letters only?

def alpha(s):
res = ''
for c in s:
if c.isalpha(): res+=c
else: res+=' '
return res.split()

print(alpha('1up2down33sideways77'))
Top Expert 2008

Commented:
I don't understand the question. The alpha() function DOES return a list of substrings. And what do you mean by consecutive letters?

Please clarify, and please use the "ask a related question" feature. :)

Commented:
ok, im typing up my questions with more clarity, and i will paste the link here.
thank you
Top Expert 2008

Commented:
FYI: When you use the "related question" feature I will automatically get an alert, because I answered this question.
Access more of Experts Exchange with a free account
##### Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the