1, str () function: the parameter value into a value type of a string, and returns.
scene:
When using the printing function is used if the link character string of numbers, we need to use str () function converts the value into a string and then using the + operator connection.
Example:
>>> number=12 >>> print('我有'+number+'个苹果') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: can only concatenate str (not "int") to str
Description:
The above is an example of an error
The reason being given the rearmost row said: Only connect str and str
So: we must have a means to convert int to str can be output.
Therefore: python and the connection string values, we can first value into a string, then the connection
>>> Print ( ' I ' + str (Number The) + ' apple ' ) I have 12 apples
2, int () function: converting a digital value into an integer form character string, and returns.
scene:
When we use the Input () function, we know that all string values returned after input information received from the console INPUT (), if we want the user inputted digital arithmetic operations have to use int () conversion .
Example:
Age = the INPUT >>> ( ' Enter your age: ' ) Enter your Age: 12 >>> type(age) <class 'str'> >>> new_age=age+10 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: can only concatenate str (not "int") to str >>> new_age=int(age)+10 >>> new_age 22
Description:
- The first line of variable age using the received input () return value;
- Then we use the python built-in type () function type detection. The type of information returned is: str type
- We use str directly adding, incorrect report! Error message is: strings and numbers can not be connected to the operation? Viewed as a plus even the string concatenation operator
- Finally, use int () function to convert a string, success.