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

#!/usr/bin/env python3
#NestedIfStuff.py

password = "dink"

print(str(password.len()))
But I get the error: AttributeError: 'str' object has no attribute 'len'

What's wrong?
Reply
#2
you would do it this way
print(len(password))
Reply
#3
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: ")
int(guessLength)

 
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)
            sys.exit()
        print(guess)
Error:
Traceback (most recent call last): File "E:/Python/Python36-32/SamsPrograms/FinalPasswordGuesser.py", 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?
Reply
#4
You have forgotten to assign the integer in line 7:

guessLength = int(guessLength)
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


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

Forum Jump:

User Panel Messages

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