Search code examples
listpython-2.7strip

print password.strip(",") AttributeError: 'list' object has no attribute 'strip'


I am fairly new to python. I am working on a random password generator. I was able to get my program to generate a random password. However it has commas (,) in it. I would like to strip the commas out.

It looks like I am getting an error with the strip command. The program below produces the output:

AttributeError: 'list' object has no attribute 'strip'

#! /usr/bin/python

import random
import string

def letter_num():
letter = random.choice(string.letters)
number = random.randint(0,9)
letter_number = [letter, number]
random_letnum = random.choice(letter_number)
return random_letnum

#print letter_num(),letter_num(),letter_num(),letter_num(),letter_num(),letter_num,letter_num(),letter_num()

password = [letter_num(),letter_num(),letter_num(),letter_num(),letter_num(),letter_num(),letter_num(),letter_num()]
print password.strip(",")

Solution

  • That is because in your code password is a list and you cannot use strip on list.

    Try the below code

     #! /usr/bin/python
    
    import random
    import string
    
    def letter_num():
        letter = random.choice(string.letters)
        number = random.randint(0,9)
        letter_number = [letter, number]
        random_letnum = random.choice(letter_number)
        return random_letnum
    
    password = ""
    for a in xrange(10):
        password += str(letter_num())
    
    print password.strip(",")