int与str
int和str是Python中的数据类型之一。
int表示整数,例如1、2、3、-10等。int类型可以进行算术运算,如加减乘除。
str表示字符串,例如"hello"、"world"、"123"等。字符串可以包含任何字符,包括字母、数字、符号等。字符串可以使用加号进行拼接,也可以使用索引和切片操作访问其中的字符。
在Python中,int和str可以通过函数相互转换:
- str()函数将int类型转换为str类型。
- int()函数将str类型转换为int类型。 例如:
num = 123
str_num = str(num)
print(type(str_num)) # 输出<class 'str'>
str_num = "456"
num = int(str_num)
print(type(num)) # 输出<class 'int'>
原文地址: https://www.cveoy.top/t/topic/c1NP 著作权归作者所有。请勿转载和采集!