python练习题__基础输入输出
目录
一、基本变量输出
1、输出字符串Hello World!
2、多行输出字符串
3、读入字符串
4、读入整数数字
二、格式化输出
5、按格式输出相应的英文句子
6、格式化数字输出
7、小数化整数
8、为整数增加小数点
一、基本变量输出
1、输出字符串Hello World!
题目:
将字符串 'Hello World!' 存储到变量str中,再使用print语句将其打印出来。
输入:无
输出:一行输出字符串Hello World!
str = 'Hello World!'print(str)
2、多行输出字符串
题目:
多行输出字符串'Hello World!','我还是从前那个少年!'
描述:将字符串 'Hello World!' 存储到变量str1中,再将字符串 '我还是从前那个少年!' 存储到变量str2中,再多行输出,使用print语句将其打印出来。
输入:无
# 方法一(一行一个变量)str1 = 'Hello World!'str2 = '我还是从前那个少年!'print(str1)print(str2)# 方法二str1 = 'Hello World!'str2 = '我还是从前那个少年!'print(str1, str2, sep=("\n"))# 方法三str1 = 'Hello World!'str2 = '我还是从前那个少年!'print(str1 + "\n" + str2)print(str1 + "\n" + str2 + "\n")
3、读入字符串
题目:
使用input函数读入一个字符串,然后将其输出。
输入描述:输入一行字符串:逆境清醒。
输出描述:将读入的变量输出。
输入:逆境清醒
输出:逆境清醒
string = input()print(string)
4、读入整数数字
题目:
使用input函数读入数字并输出数字与变量类型。
输入描述:输入只有整数。
输出描述:将输入的数字输出,同时换行输出变量类型。
num = int(input())print(num)print(type(num))
二、格式化输出
5、按格式输出相应的英文句子
题目:
用input函数读入name,按格式输出相应的英文句子。
name:Cross the stars over the moon to meet your better self.
# 方法1name=input()print('{} :Cross the stars over the moon to meet your better self'.format(name))# 方法2:位置一一对应name=input()print('{0} :Cross the stars over the moon to meet your better self'.format(name))# 方法3:参数name=input()print('{A} :Cross the stars over the moon to meet your better self'.format(A=name))# 方法4:文本用 %s 占位符name=input()print('%s:Cross the stars over the moon to meet your better self'%name)# 方法5:print('%s :Cross the stars over the moon to meet your better self'%(input()))# 方法6:name=input()print(f'{name}:Cross the stars over the moon to meet your better self')
6、格式化数字输出
题目:
读入一个浮点类型小数,保留两位小数输出。
例:
输入:1.000000
输出:1.00
a = float(input())# 方法1:print(f'{a:.2f}')# 方法2:print('%.2f' %a)# 方法3:print('{:.2f}'.format(a))
7、小数化整数
题目:
输入一个浮点小数。输出将其强制类型转换为int后的结果。
a = float(input("请输入一个浮点小数:"))print(int(a))
8、为整数增加小数点
题目:
输入一个整数。
输出强制类型转换后的结果,换行再输出变量类型。
方法一:a=float(input())print('{:.1f}'.format(a), type(a), sep = '\n')方法二:a=float(input())print('%.1f'%a, type(a), sep = '\n')方法三:a=float(input())print(f'{a :.1f}', type(a), sep = '\n')
推荐阅读:
如果觉得《python练习题__基础输入输出》对你有帮助,请点赞、收藏,并留下你的观点哦!