python-基础语法

输出相关:
  • 基本输出案例
print("HelloWorld")

# 执行结果:
'''
HelloWorld
'''
  • 转义字符
# 转义字符"/"
# 输出前加"r",输出原始字符串,即去除转义字符
# 输出前加"f",输出填补字符串,和format相似
name = 'c:\\user'
x = '路径c:\\user'
y = r"路径c:\user"
z = f"路径{name}"
print(x,"\n",y,"\n",z)

# 执行结果:
'''
路径c:\user 
路径c:\user 
路径c:\user
'''
  • 占位符
age = 17
print("我的年龄是:%d岁"%age)

# 执行结果:
'''
我的年龄是:17岁
'''
  • 间隔符
print("www", "baidu", "com", sep = ".")

# 执行结果:
'''
www.baidu.com
'''
  • 控制符
print("hello", end = "")
print("world")

# 执行结果:
'''
helloworld
'''
  • 连接符
name = "World"
print("Hello" + name)

# 执行结果:
'''
HelloWorld
'''
  • 复制符
count = "num "
print(count*3)

# 执行结果:
'''
num num num 
'''
  • 进制转换
x = 100
print(bin(x)) #转换为二进制
print(oct(x)) #转换为八进制
print(hex(x)) #转换为16进制

# 执行结果:
'''
0b1100100
0o144
0x64
'''
  • 去除/替换指定字符
a = "abcd"
print(a.strip("dc"))
print(a.replace('ab','-'))

# 执行结果:
'''
ab
-cd
'''
  • format
x = "我是{name},今年{age}".format(name = 'cjl', age = 18)
print(x)

# 执行结果:
'''
我是cjl,今年18
'''
输入相关:
  • 基础输入案例
password = input("请输入密码:")
print("你输入的密码是:", password)
print(type(password))

# 执行结果:
'''
请输入密码:666
你输入的密码是: 666
<class 'str'>
'''
  • input默认输入后的类型为字符串
  • 输入后类型强转
password = int(input("请输入密码:"))
print("你输入的密码是:", password)
print(type(password))

# 执行结果:
'''
请输入密码:666
你输入的密码是: 666
<class 'int'>
'''
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值