AttributeError: 'int' object has no attribute 'title' in python
This error means you’re calling the string method .title on a value that is actually an int, not a str. In other words, somewhere a variable you expect to be text holds a number instead.
Why it happens
Variable shadowing. A name you used for a string earlier was reassigned to an integer, so later obj.title tries to run on an int. For example: name = "alice"; name = 5; name.title() → error. This commonly happens when reusing names like file, time, or data for different types at different points of the code.
Wrong data shape. You indexed or pulled data from a dict/list/JSON where some items are ints and others are strings, then you applied .title blindly to all items.
Type assumptions. User input or parsed values were cast to int (or loaded as numbers) before string operations are applied.
How to fix quickly
- Find the line with .title and print or log the type and value just before it:
- print(type(x), x) or use an assert: assert isinstance(x, str)
- Convert to string if that’s acceptable for your logic:
- str(x).title()
- Guard by type:
- x = x.title() if isinstance(x, str) else x
- Clean data at the source:
- When mapping over a collection: [s.title() for s in items if isinstance(s, str)]
- When reading JSON: coerce only desired fields to str before using .title
Common patterns and remedies
- Reused names:
- Problem: file = open(...); file = 123; file.title() → AttributeError
- Fix: Use distinct names (f for file handle, score for numbers).
- Library/module shadowing:
- Problem: import time; time = 3; time.time() breaks because time is now an int.
- Fix: Avoid reassigning imported names; or import as alias (import time as time_mod).
- Mixed-type collections:
- Problem: for v in values: v.title()
- Fix: for v in values: if isinstance(v, str): v = v.title() else: handle int case.
If you share a snippet
Paste the few lines around where .title is called (and any variable assignments to that name earlier). Indicate expected types. A minimal reproducible example makes it straightforward to point to the exact reassignment or coercion causing the int.
import os
terminal_width = get_terminal_size().columns
the_given_string = input("Enter your string: ")
print(the_given_string.center(terminal_width.title()))
print(the_given_string.ljust(terminal_width.title()))
print(the_given_string.rjust(terminal_width.title()))
The error happens because terminal_width is an int, but .title() is a string method, and center/ljust/rjust expect an int width, not a string. You should use the integer directly.
Here is the corrected code:
import os
from shutil import get_terminal_size
terminal_width = get_terminal_size().columns # this is an int
the_given_string = input("Enter your string: ")
print(the_given_string.center(terminal_width))
print(the_given_string.ljust(terminal_width))
print(the_given_string.rjust(terminal_width))
NOTE::: NEED TO ADD .title() after the given string Variable.
Key points:
- Remove .title() on terminal_width; just pass terminal_width.
- If you really needed to convert something to title case, that should be used on a string, e.g. some_string.title(), not on an int.
in python3 instead of input function what can we use:
In Python 3, input() is the standard way to read from the keyboard; there is no direct replacement that you “should” use instead.
Input in Python 3
-
input()always returns a string, so you convert it when you need numbers:-
n = int(input("Enter an integer: ")) x = float(input("Enter a float: "))
-
- The old Python 2 functions:
-
raw_input()→ replaced byinput()in Python 3. -
input()in Python 2 (which evaluated expressions) was removed for safety; there is no direct equivalent in Python 3, and evaluating user input is strongly discouraged.
-
Alternatives depending on context
- For command‑line arguments: use
sys.argvorargparseinstead ofinput(). - For reading from files or other streams: use file methods like
readline()or iterate over the file object.
If you explain your exact use case (e.g., need numbers, expressions, command‑line options, etc.), more tailored alternatives can be suggested.
Top comments (0)