首页 > 编程语言 > 详细

python 变量 字符串拼接 字符串格式化输出

时间:2021-06-03 10:28:34      阅读:19      评论:0      收藏:0      [点我收藏+]
# 声明变量 区分大小写
name = "andy"
age = 18
print(name,age)
temp = type(2 ** 64)  # type检测变量类型
print(temp)

 

# 查看关键字
import keyword
print(keyword.kwlist)

 

# 字符串拼接
name = "andy"
age = 18
six = ""
print(name + six)  # 字符串拼接用+
print(name * 10)  # 字符串运算 10个 andy

 

# input函数的使用 获取用户输入的内容 字符串类型的
password = input("请输入密码:")
print("输入的内容是:" + password)

 

# 类型转换
temp = int("520")  # 字符串转换成整型
print(type(temp))

temp2 = float("12.5")
print(type(temp2))

 

# 字符串格式化输出
name = "张三"
age = 18
price = 12.052
student_no = 1
scale = 0.25
print("我的名字是%s" % name)  # 字符使用%s
print("今年%d岁" % age)  # 整数使用%d
print("价格是%.2f" % price)  # 小数使用%f 保留小数点2位中间.2
print("学生编号是%06d" % student_no)  # 整数最低6位 不足6位补0
print("数据比例是%.2f%%" % (scale* 100)) # 输出百分号使用%%

 

# 字符串格式化输出2
name = "张三"
age = 18
price = 100.25
print("我的名字是%s,年龄%d岁,能卖%.2f元" % (name,age,price))

 

python 变量 字符串拼接 字符串格式化输出

原文:https://www.cnblogs.com/dazahui/p/14843318.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!