我想输入变量名并将它们转换为实际变量,类似于print语句。有可能吗?你知道吗
print(f'I love {fruit} and the colour {colour}')
代码:
fruit=apples
colour=red
message=input('Enter your message: ')
print(message)
所需输入:
Enter your message: I love {fruit} and the colour {colour}
期望输出:
I love apples and the colour red
Solution: How can i use f-string with a variable, not with a string literal?
variables = {
'fruit': 'apples',
'colour': 'red'
}
message=input('Enter your message: ').format(**variables)
print(message)
输入:I love {fruit} and the colour {colour}
输出:I love apples and the colour red
相关问题 更多 >
编程相关推荐