#示例1 连接
s = 'lilei'
print("my name is ", s)
#执行结果:
my name is lilei
#示例2 格式控制
n = 3
s = "土豆"
print("我买了", n, "斤", s)
print("我买了%s斤%s" %(n, s))
print("我买了{}斤{}".format(n, s))
#执行结果:
我买了 3 斤 土豆
我买了3斤土豆
我买了3斤土豆
input
#示例1
s = input("属于一个字符串:")
print("输入的字符串为:",s)
#执行结果:
属于一个字符串:hahaha
输入的字符串为: hahaha
#排错1
a = input("请输入一个数字:")
print(a+2)
#报错 TypeError: can only concatenate str (not "int") to str
#注意:input 输入的内容,类型为字符串类型!!!!
#如果想要实现上述代码,修改如下:
a = input("请输入一个数字:")
print(int(a)+2)
#执行结果:
请输入一个数字:2
4
#排错2
a = input("请输入一个数字:")
print(a*2)
#执行结果:
请输入一个数字:2
22
解析:字符串*2 代表 字符串重复输出两次