脚本小子_python数据类型代码

#!/usr/local/bin/python3
# coding:utf-8
# format格式
# x = 4
# y = 4
# z = x + y
# 格式化,输出整形
# print("z is {0:d}".format(z))
# 格式化,输出浮点型,并保留两位小数点
# print("z is {0:.2f}".format(z))

# 一、数值
# 整数、浮点数、长整数、复数
# 1、整数
# x = 2
# print("x is {0}".format(x))
# print("x**4 is {0}".format(x**4))
# print("{0}".format(int(8.3)/int(2.7)))

# 2、浮点数
# print("{0:.3f}".format(8.3/2.7))

# 3、内置数学库 math
# import math
# 查看math有哪些方法
# print dir(math)
# 开根号
# x = 4
# x2 = math.sqrt(4)
# print("4 开根号: {0}".format(x2))

# 二、字符串
# 字符串可以包含在单引号、双引号、3 个单引号或 3 个双引号之间
# print("单引号:{},双引号:{},3单引号:{}".format('hello world',"hello world",'''hello world'''))
# 打印字符串可以使用的方法
# print dir("hello")
# 1、分割与连接
# split 分割 split(分割符[,分割数])
# 默认按照分割符全部分割
# print "hello=world=haha".split('=')
# 只分割1个
# print "hello=world=haha".split('=',1)
# join 连接 一般操作列表、元组
# print("{}".format(" ".join(['hello','world'])))
# print("{}".format(",".join(('hello','world'))))

# 2、字符去掉与替换
# 去掉不必要字符,strip两边,lstrip左边,rstrip右边
# 默认是去掉空格
# print(" hello world ".strip())
# 去掉指定字符
# print("!hello world!".strip('!'))
# replace替换字符 replace(string1,string2,num),按照次数num,把字符string1替换成string2
# print("hello#world#haha".replace('#',' '))
# print("hello#world#haha".replace('#',' ',1))

# 3、字母大小写问题
# lower小写 upper大写 capitalize首字母大写
print("HellO WoRld".lower())
print("HellO WoRld".upper())
print("HellO WoRld".capitalize())

猜你喜欢

转载自blog.csdn.net/u014795720/article/details/80784588
今日推荐