name = input("Please input your name: ")
age = input("Please input your age: ")
sex = input("Please input your sex: ")
print("Information of " + name + ":" + "\n\tName:" + name + "\n\tAge:" + age + "\n\tSex:" + sex)
输出结果如下:
Information of Alex:
Name:Alex
Age:38
Sex:girl
字符串拼接直接进行相加就可以,比较容易理解,但是一定要记得,变量直接相加,不是变量就要用引号引起来,不然会出错,另外数字是要转换为字符串才能够进行相加的,这点一定要记住,不能把数字直接相加。
2. %进行拼接
"%s%s%s" % (i,j,k)
3. 使用单引号(”””)或者双引号(“”“”“”)的方式
name = input("Please input your name: ")
age = input("Please input your age: ")
sex = input("Please input your sex: ")
message = '''
Information of %s:
Name:%s
Age:%s
Sex:%s
'''%(name,name,age,sex)
print(message)
输出结果如下:
Information of Alex:
Name:Alex
Age:38
Sex:girl
使用单引号('''''')或者双引号("""""")的方式,这种方式也很方便,我们首先进行定义,把我们需要的格式进行定义,要经常尝试这几种格式的方法,这三种方式我都觉得挺好的。
python代码过长的换行
python代码换行就是每行后面加个 \
举个栗子:
time = "2017"
print "one" + "," \
+ "two" \
+ ",three" + \
"," + time
打印出来就是:
one,two,three,2017
再举一个栗子:
print "this line is toooooooooooo \
long"
打印出来:
this line is toooooooooooo long