Python common error - "TypeError: 'str' object is not callable"

Example of error reporting:

students=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
str=[1,2,3,4,5,6]


print("\n列表转元组:\n",tuple(students))
print("\n列表转字符串:\n",str(students))

Running the above code, the following error message will appear:

 Cause Analysis:

1. str(value) is a built-in system function used to convert a numeric value into a string.

2. Users can still use str to define variables, such as str="hello"

3. After defining the str variable, since the name is the same, the built-in str(value) function of the system will be interpreted as a user-defined variable str, resulting in an error: "TypeError: 'str' object is not callable"

solution:

1. Delete the str variable, del str, and set other variable names (reminder: the defined variable name should not have the same name as the underlying function of Python.)

del str
students=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
list_str=[1,2,3,4,5,6]

print("\n列表转元组:\n",tuple(students))
print("\n列表转字符串:\n",str(students))

Run the above code, the result:

2. Restart the interactive environment and set other variable names.

Guess you like

Origin blog.csdn.net/sodaloveer/article/details/129586726