Python学习日记 Day2 输入输出数据类型转换

输出

作用:程序输出内容给用户

print'hello  world'print('hello')
print('h看看')
print("叫叫叫")

学习使用的是python3.11.0,在print()中单引号和多引号效果一样

格式化输出

在这里插入图片描述

# 今年我的年龄是x岁--%d
age = 18
print('今年我的年龄是%d岁' % age)
# 我的名字是x
Name = 'Tom' 
print('我的名字是%s' % Name)
# 我的体重是x公斤--保留两位小数
weight = 75.5
print('我的体重是%.2f公斤 ' % weight)
#-------------------------
# 今年我的学号是x
stu_id=17
print('我的学号是%d' % stu_id)
#今年我的学号是00x(三位数,不足三位在前面补0,超出的原样输出)
print('我的学号是%03d' % stu_id)
# 今年我的名字是x,年龄是x岁
print( '今年我的名字是%s,年龄是%d岁' %(Name,age))
# 今年我的年龄是x岁,明年是x岁
print('今年我的年龄是%d岁,明年是%d岁' %(age,age+1))

%.3f:保留三位小数点
%03d:三位数在之前补零,超出的(如1000),则按原样输出
%(x,y):在一起输出两个

运行结果

今年我的年龄是18岁
我的名字是Tom
我的体重是75.50公斤
我的体重是75.50公斤
我的学号是17
我的学号是017
今年我的名字是Tom,年龄是18岁
今年我的年龄是18岁,明年是19岁是引用

%s的高级用法

name = 'Tom'
age = 18
weight = 75.5
print('我的名字是%s,今年我的年龄是%s,我的体重是%s公斤' %(name,age,weight))

可以全用%s输出

语法f'{}'

print(f'我的名字是{name},今年我的年龄是{age},我的体重是{weight}公斤')

比%s更加简单高效,//3.6版本后新添加

运行结果:

我的名字是Tom,今年我的年龄是18,我的体重是75.5公斤

转义字符

  • \n换行
  • \t:制表符,一个tab键(4个空格的距离
print('hello')
print('world')#已经换行输出
print('hello world')#一行输出
print('hello\n world')#换行,在\n后面加多少空格缩进多少
print('\taaaa')#空了四个空格

运行结果

hello
world
hello world
hello
(这里有空格因为在\n后有空格) world
(四个空格没有显示出来) aaaa

结束符end

end=“”
为什么两个print会换行?
因为print(‘输出的内容’,end=“\n”)。,这是默认隐藏的
这里的 end=“\n” 就是结束符
所以也可以自定义结束符

print('哈哈哈哈哈哈',end="66666")
print('hhhhhh')

运行结果

哈哈哈哈哈哈66666hhhhhh

输入

在python中,程序接受用户输入的数据的功能就是输入
语法:input(“提示信息”)
输入的特点

  • 当执行到input,用户输入后才回到下一步
  • 接受用户输入后,存储到变量,方便使用
  • input()任意用户输入的数据都当作字符串处理

转换数据类型

问:input()接受用户输入的数据都是字符串类型,用户输入1,想得到整形怎么办
答:转换数据类型即可
在这里插入图片描述
在这里插入图片描述

将输入的password由字符串形式转化为整型
这是第一种办法

password = input('请输入你的密码:')
print('密码是'f'{password}')
print(type(password))
b=int(password)
print(type(b))

第二种办法

password = input('请输入你的密码:')
print('密码是'f'{password}')
print(type(password))
print(type(int(password)))

运行结果

请输入你的密码:1
密码是1
<class ‘str’>
<class ‘int’>

要注意的是,这种情况不会进行转换

password = input('请输入你的密码:')
print('密码是'f'{password}')
print(type(password))
int(password)
print(type(password))

此时的password还保持着原来的形式
运行结果:

请输入你的密码:1
密码是1
<class ‘str’>
<class ‘str’>

数据类型转换函数

  1. float()转化为浮点型
    num1=1 str1 = '10' print(type(float(num1))) print(float(num1)) print(float(str1))
    运行结果

<class ‘float’>
1.0
10.0

  1. str()转化为字符串型print(type(str(num1)))
    运行结果

<class ‘str’>

  1. tuple()序列转化为元组
    list1 = [10,20,30] print(list(list1))
    运行结果

[10, 20, 30]

  1. list()元组转化为列表(序列)
    t1 = (100,200,300) print(list(t1))
    运行结果

[100, 200, 300]

  1. eval()计算字符串中有效的python表达式,并返回一个对象
tr3 = '1'
str4 = '1.1'
sr5 = '(100,200,300)'
str6 = '[100,20,300]'
print(eval(str3))
print(type(eval(str3)))
print(eval(str4))
print(eval(sr5))
print(eval(str6))

运行结果

1
<class ‘int’>
1.1
(100, 200, 300)
[100, 20, 300]


eval()的解释:
str1=‘1’- - -此时为字符串型
eval(str1)- - - eval()检测1为整形,则返回整型

------------------------------------------------------------------------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值