Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get string length
To start off, I just want to print the length of a hard coded string as follows:

#!/usr/bin/env python3

password = "dink"

But I get the error: AttributeError: 'str' object has no attribute 'len'

What's wrong?
you would do it this way
I need to convert user string input to a number. My statement on line 7 isn't working:

import itertools
import sys
alphaNum = 'abc'
password = 'cab'
guessLength = input("How many characters would you like to guess up to: ")

for x in range(0, guessLength+1):
    guessList = [''.join(i) for i in itertools.product(alphaNum, repeat = x)]
    for guess in guessList:
        if (password == guess):
            print("the password is " + password)
Traceback (most recent call last): File "E:/Python/Python36-32/SamsPrograms/", line 10, in <module> for x in range(0, guessLength+1): TypeError: Can't convert 'int' object to str implicitly
What do I need to do?
You have forgotten to assign the integer in line 7:

guessLength = int(guessLength)
Almost dead, but too lazy to die:
All humans together. We don't need politicians!

Possibly Related Threads…
Thread Author Replies Views Last Post
  How can i limit length string? perrfect 3 2,454 Feb-10-2020, 04:52 PM
Last Post: Jendker
  python gives wrong string length and wrong character thienson30 2 1,439 Oct-15-2019, 08:54 PM
Last Post: Gribouillis
  Highlight/Underline a string | ValueError: zero length field name in format searching1 1 1,784 Jul-01-2019, 03:06 AM
Last Post: metulburr
  calculating length of string desul 12 6,102 May-22-2019, 10:50 AM
Last Post: ankit
  Python find the minimum length of string to differentiate dictionary items zydjohn 3 2,138 Mar-03-2018, 05:23 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020