4

I'm getting strange error "'int' object has no attribute 'startswith'"

I haven't used the word "startswith" in my python program. ? Does any one how to fix this -- or what it refers to ?

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
webminal.org
  • 44,948
  • 37
  • 94
  • 125

2 Answers2

14

Something in your program is trying to call the startswith method of an object, probably because it expects it to be a string. You'll have to pay attention to the traceback to see what it is being called on, and why that is an integer instead of a string. Did you pass along an integer where a string was expected?

Thomas Wouters
  • 130,178
  • 23
  • 148
  • 122
5

startswith only works with strings.

If you need to check if an int starts with a set of numbers, you can convert it to a string, i.e.:

someint = 1234
if str(someint).startswith("123"):
    # do somenting
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268