Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1854
  • Last Modified:

convert string to list

Hi Experts,

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

for example

alpha("1up2down33sideways77")  to return something like ["up", "down", "sideways"].
0
Student_101
Asked:
Student_101
  • 4
  • 3
2 Solutions
 
Roger BaklundCommented:
Try this:
def alpha(s):
    res = ''
    for c in s:
        if c.isalpha(): res+=c
        else: res+=' '
    return res.split()
 
print(alpha('1up2down33sideways77'))

Open in new window

0
 
Student_101Author 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

0
 
Roger BaklundCommented:
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
0
[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

 
Student_101Author 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'))
0
 
Roger BaklundCommented:
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. :)
0
 
Student_101Author Commented:
ok, im typing up my questions with more clarity, and i will paste the link here.
thank you
0
 
Roger BaklundCommented:
FYI: When you use the "related question" feature I will automatically get an alert, because I answered this question.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now