+ 2
There is a syntax mistake in the last line of this code. Could someone fix it please? import string list=[] for s in dir(string): list.append(s) for i in range(len(list)): n=list[i] print (string.str(n))
"String has no attribute named str"
4 odpowiedzi
+ 4
OK. I see. So what you want to print is the values of those objects? Many of them are classes and functions and you could only get meaningless outputs like:
<function (or class) XXX at 0xXXXXXX>
Well, if you insist:
import string
for i in dir(string):
exec 'print(string.{0})'.format(i)
Exec is not safe though. Especially when you use a string inputed from a user.
Exec and eval() could run shell commands, so use them carefully.
+ 1
No need to put everything of dir() into a list. Dir() already made a list for you.
It is the same with your list[i]. That is already a string and you don't need to convert it using str()
So I would modify your code into:
for i in dir(__import__('string')):print(i)
One single line is enough :)
+ 1
I don't want to print only the name of the function, I want to print its value. String has lots of printable elements like ascii_lowercase, ascii_uppercase, ...
print(string.ascii_lowercase)
- 1
str(n)