Home >Backend Development >Python Tutorial >Problems encountered when Python's str is forcibly converted to int
The following is an article to share with you to solve the problems encountered when Python's str is forced to int. It has certain reference value and I hope it will be helpful to everyone.
It’s okay if there are spaces before and after the numeric string:
>>> print(int(" 3 ")) 3
But the following situation with decimal points It is not advisable:
>>> print(int("3.0")) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: invalid literal for int() with base 10: '3.0'
There is no problem in forcing this kind of string to float
>>> print(float("3.0")) 3.0
Related recommendations:
How to achieve mutual conversion from str to list in python
The above is the detailed content of Problems encountered when Python's str is forcibly converted to int. For more information, please follow other related articles on the PHP Chinese website!