格式化输出
思路一:通过字符串的连接输出
a=input() print("I am " + a + " and I am studying Python in Nowcoder!")
思路二:利用format格式化函数填充
name=input() print('I am {} and I am studying Python in Nowcoder!'.format(name))
同时还有format的几个变形使用:
#利用序号对应
# print('I am {0} and I am studying Python in Nowcoder!'.format(name))
#利用参数指代
# print('I am {A} and I am studying Python in Nowcoder!'.format(A=name))
思路三:字符串格式化 %s 占位符
name = input() print("I am %s and I am studying Python in Nowcoder!"%name)
%s 的用法扩展
print "My name is %s and weight is %d kg!" % ('Zara', 21)
思路四: f字符串
str = input() print(f'I am {str} and I am studying Python in Nowcoder!')
Python中的f字符串的用法:要在字符串中插入变量的值,可在前引号前加上字母f,再将要插入的变量放在花括号内
first_name="ada" last_name="lovelace" full_name=f"{first_name}{last_name}" print(f"Hello,{full_name.title()}!")
打印结果:Hello,Ada Lovelace!
# title()函数是返回"标题化"的字符串,就是说所有单词都是以大写开始,其余字母均为小写
# f字符串在python3.6这么用,之前版本用法还有差别。