python练习题__基础输入输出
目录
一、基本变量输出
1、输出字符串Hello World!
题 目:
将字符串 'Hello World!' 存储到变量str中,再使用print语句将其打印出来。
输入:无
输出:一行输出字符串Hello World!
分 析: |
首先将字符串“Hello World!”存储到变量str中 然后使用print语句将变量str打印出来 |
str = 'Hello World!'
print(str)
运行结果: |
Hello World! |
2、多行输出字符串
题 目:
多行输出字符串'Hello World!','我还是从前那个少年!'
描述:将字符串 'Hello World!' 存储到变量str1中,再将字符串 '我还是从前那个少年!' 存储到变量str2中,再多行输出,使用print语句将其打印出来。
输入:无
分 析: |
1.\n换行,\t制表,\r回车 2.转义字符需要用双引号引起来 |
# 方法一(一行一个变量)
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")
运行结果: |
Hello World! 我还是从前那个少年! |
3、读入字符串
题 目:
使用input函数读入一个字符串,然后将其输出。
输入描述:输入一行字符串:逆境清醒。
输出描述:将读入的变量输出。
输入:逆境清醒
输出:逆境清醒
分 析: |
Python2 中 input() 函数: input() = eval(raw_input()) ,是将 raw_input() 函数返回的结果 eval() 了一下。因为引入了 eval(),所以会存在代码注入的风险。 在 Python3 中,将 Python2 的 raw_input() 函数保留、重命名为 input(),同时移除了 Python2 的 input() 函数。因此,在 Python3 中,不存在 input() 函数代码注入风险。 |
string = input()
print(string)
运行结果: |
逆境清醒 逆境清醒 |
4、读入整数数字
题 目:
使用input函数读入数字并输出数字与变量类型。
输入描述:输入只有整数。
输出描述:将输入的数字输出,同时换行输出变量类型。
分 析: |
# 题解: # 1.强制转化int(要转化的类型) # 2.判断类型type(要判断的类型) |
num = int(input())
print(num)
print(type(num))
运行结果: |
56546 56546 <class 'int'> |
二、格式化输出
5、按格式输出相应的英文句子
题 目:
用input函数读入name,按格式输出相应的英文句子。
name:Cross the stars over the moon to meet your better self.
# 方法1
name=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')
运行结果: |
Adversity Awake Adversity Awake :Cross the stars over the moon to meet your better self |
6、格式化数字输出
题 目:
读入一个浮点类型小数,保留两位小数输出。
例:
输入:1.000000
输出:1.00
分 析: |
利用"%nf"输出n位小数,使用format函数格式化数字输出,format函数中使用一个大括号表示需要填充的参数,:后面可以规定数字的精度与类型 |
a = float(input())
# 方法1:
print(f'{a:.2f}')
# 方法2:
print('%.2f' %a)
# 方法3:
print('{:.2f}'.format(a))
运行结果: |
#输入: #输出: 546547.68 |
7、小数化整数
题 目:
输入一个浮点小数。输出将其强制类型转换为int后的结果。
分 析: |
float() 强制转换为浮点型 |
a = float(input("请输入一个浮点小数:"))
print(int(a))
运行结果: |
请输入一个浮点小数:756396.54328 756396 |
8、为整数增加小数点
题 目:
输入一个整数。
输出强制类型转换后的结果,换行再输出变量类型。
分 析: |
float() 强制转换为浮点型 |
方法一:
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')
运行结果: |
45353.5634 45353.6 <class 'float'> |
题 目: | |
分 析: | |
答 案: | |
运行结果: | |
推荐阅读:
36 | python练习题目录 | |
35 | ||
34 | ||
33 | python Format()函数的用法___实例详解(一)(全,例多)___各种格式化替换,format对齐打印 | |
32 | Background 全属性实例图解(10种全)(一) | |
31 | Element Plus 实例详解(一) | |
30 | |
Vue3安装配置、开发环境搭建(组件安装卸载)(图文详细) |
29 | | |
28 | | |
27 | | |
26 | | |
25 | | |
24 | | |
23 | | |
22 | | |
21 | | |
20 | | |
19 | | |
18 | | |
17 | | |
16 | | |
15 | | |
14 | | |
13 | | |
12 | | |
11 | | 用代码写出浪漫__合集(python、matplotlib、Matlab、java绘制爱心、玫瑰花、前端特效玫瑰、爱心) |
10 | | |
9 | | |
8 | | |
7 | | 2023年3月TIOBE 指数头条:编程语言 Go 进入 TIOBE 指数前 10 名,多家权威机构____编程语言排行榜__薪酬状 |
6 | | |
5 | | |
4 | | |
3 | | |
2 | | |
1 | |