This should be simple, but I think I am missing up the syntax. I am trying to make a list of the letters that appear in a string.

import string
values=[]
values[5]="12234.7B"
holder=[]
holder.append(values[5].letters)

This code gives me an error on the letters function. Any idea why?

Many thanks.

Print is debuggers best friend

import string
values=['']*6
values[5]="12234.7B"
holder=[]
print string.letters
print values
print values[5].letters
holder.append(values[5].letters)

Tonyjv,

Can you tell me how the code, values=*6, is different from just values=[]?

Thanks again.

The array [] has no elements. You can append to it, but you cannot insert an item at index 5 by assigning as you did. The array ['']*6 has the empty string at each index from 0 through 5 inclusive.

If you are trying to make a list of the characters in a given string, you should probably use list comprehension:

theList = [x for x in 'my string']

I fear I have a problem with my python complier then? I am using 2.6.5. When I type in the code above, specifically,

import string
values=['']*6
values[5]="12234.7B"
holder=[]
holder.append(values[5].letters)

Python gives me an error. It says,

Traceback (most recent call last):
File "/Users/Home/untitled", line 5, in <module>
holder.append(values[5].letters)
AttributeError: 'str' object has no attribute 'letters'

Does this make sense?

Thanks.

It is not bug this is how it is. letters is method of string module not string object property. It produces the letters available:
See:
http://docs.python.org/library/string.html

What you want to accomplish with your statement?

Tony,

I must have misunderstood the documentation on what letters does. I had hoped to strip off the last letter (which will be either M or B) from a string of numbers and append that letter in a list. So for example,

"25167537.89B" or "123.0M"

I would want to know the first string is 25167537.89 billions and the second is 123.0 millions.

If you are sure it is always there you can do:

for numstr in ["25167537.89B" , "123.0M"]:
    print [numstr[:-1],numstr[-1]]

Thanks again Tony. So just to conclude, for any other beginners who might want to follow this one day, if I incorporate your logic, the final code will be:

holder=[]
values=["25167537.89B" , "123.0M"]
for x in values:holder.append(x[-1])
print holder

This will save the last characters in the holder list.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.