编写一个 Python示例,将整数转换为字符串,或将 int 转换为字符串。例如,在 Python 中,我们可以使用内置的 str 函数将 int 转换为 string。
num = 120
print(num)
print(type(num))
intToString = str(num)
print(intToString)
print(type(intToString))
在这个 Python 示例中,我们使用%s 格式化程序将 int 转换为 string。
num = int(input("Enter Any Number = "))
print(num)
print(type(num))
intToString = "%s" %num
print(intToString)
print(type(intToString))
Enter Any Number = 12313
12313
<class 'int'>
12313
<class 'str'>
Python示例使用格式函数将整数转换为字符串。
num = int(input("Enter Any Number = "))
print(num)
print(type(num))
intToString = "{}".format(num)
print(intToString)
print(type(intToString))
Enter Any Number = 2345
2345
<class 'int'>
2345
<class 'str'>
这个 python 示例使用 f 字符串将 int 转换为 string。
num = int(input("Enter Any Number = "))
print(num)
print(type(num))
intToString = f'{num}'
print(intToString)
print(type(intToString))
Enter Any Number = 4567
4567
<class 'int'>
4567
<class 'str'>
在这个 Python 示例中,我们组合或展示了将 int 转换为 string 的所有可能选项。
num = int(input("Enter Any Number = "))
print(num)
print(type(num))
print("\nResult of str function")
intToString = str(num)
print(intToString)
print(type(intToString))
print("\nResult of %s Formatter")
intToString1 = "%s" %num
print(intToString1)
print(type(intToString1))
print("\nResult of format function")
intToString2 = "{}".format(num)
print(intToString2)
print(type(intToString2))
print("\nResult of f string")
intToString3 = f'{num}'
print(intToString3)
print(type(intToString3))
Enter Any Number = 12345
12345
<class 'int'>
Result of str function
12345
<class 'str'>
Result of %s Formatter
12345
<class 'str'>
Result of format function
12345
<class 'str'>
Result of f string
12345
<class 'str'>
标签: 兼职