[Python] 输入输出

1.输出

在Python中使用print()函数进行输出

输出字符串时可用单引号或双引号括起来,可直接打印引号中的内容

# Hello World!!!
print('Hello World!!!')
# My name is Andy.
print("My name is Andy.")

输出变量可不加引号

txt = 'I love Python'
# I love Python
print(txt)

变量与字符串同时输出或多个变量同时输出时,需要用逗号","隔开各项

url = 'https://blog.csdn.net/Hudas'
# 我的blog网址: https://blog.csdn.net/Hudas
print('我的blog网址:', url)
a = 'Andy'
b = 437
# Andy 437
print(a, b)

print()函数可以使用sep参数来间隔多个对象 

# 未设置间隔符(默认为空格)
# https://blog csdn net
print("https://blog","csdn","net")

# 设置间隔符
# https://blog.csdn.net
print("https://blog","csdn","net",sep=".")

print()函数默认输出是换行的,如果要实现不换行的效果,需要在变量末尾加上分隔符参数end,用来设定以什么结尾,默认值是换行符 \n,我们可以换成其他字符串

'''
0
1
2
3
'''
for i in range(0,4):
    # 未设置end参数,默认值是换行符 \n
    print(i)

'''
0 1 2 3
'''
for i in range(0,4):
    # 设置end参数
    print(i, end=" ")

扩展补充知识

转义字符:反斜杠\ + 想要实现的转义功能首字母

常用的转义字符
功能转义符
换行\n
退格\b
回车\r
代表一个反斜杠\\
代表一个单引号\'
代表一个问号\?
# https:\blog.csdn.net
print('https:\\blog.csdn.net')
# https:\\blog.csdn.net
print('https:\\\\blog.csdn.net')

# 老师说:'大家好'
print('老师说:\'大家好\'')

'''
hello
world
'''
print('hello\nworld')

# 不希望字符串中的转义字符起作用,就在字符串之前加上r或R
# hello\nworld
print(r'hello\nworld')

2.输入

Python提供了input()函数用于获取用户键盘输入的字符(获取用户输入)

input()函数收集的输入值将被强制转换为字符串类型str

# 输入数据赋给变量name
name = input("请输入姓名:")
# 输出数据
print('Welcome to my blog', name, sep = ',')

# str
type(name)

input()返回一个字符串,对字符串使用split(" ")可对其按空格切分

content = input("请输入带有空格的内容:")
list = content.split(" ")
print(list)

可以使用int()将输入的字符串转换成整数 

例子: 计算三角形的半周长

a,b,c = (input("请输入三角形三边的长:").split())
a = int(a)
b = int(b)
c = int(c)

# 计算三角形的半周长
res = (a+b+c)/2
print('该三角形的半周长为', res)

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值