Python之路第五篇——Python基本数据类型

Python基本数据类型

数字

# 1.数字 int
# - int
# 	将字符串转换为数字
a = "123"
print(type(a), a)  # 内置的 type() 函数可以用来查询变量所指的对象类型
b = int(a)  # int() 将字符串转化为数字,默认直接转化为十进制,需要其他进制后面加 base=
print(type(b), b)  # 还可以用 isinstance 来判断 isinstance(b, int)
# 结果:<class 'str'> 123
# 结果:<class 'int'> 123

num = "0011"  # 一个二进制数
v = int(num, base=2)  # base=2,是以二进制的形式进行转换为十进制,如没有base= 则结果为:11
print(v)  # 结果:3

num_16 = "e"
k = int(num_16, base=16)  # base=后面还跟着8或者16
print(k)  # 结果:14

# bit_lenght
age = 16  # 一个字节占八个 bit
# 1(十进制) --> 1(二进制)表示一位
# 2(十进制) --> 10(二进制)表示二位
# 3(十进制) --> 11(二进制)表示二位
# 4(十进制) --> 100(二进制)表示三位
# 5(十进制) --> 101(二进制)表示三位

r = age.bit_length()  # .bit_length() 当前数字的二进制,最少用n位表示
print(r)

 

字符串

capitalize()

test = "python"
v = test.capitalize()  # .capitalize()的功能是让首字母大写
print(v)  # 输出结果:Python

lower()   .casefold() 

.casefold() 要比 .lower() 更高级或者说是更强大,具体如下:

test1 = "pYthon"
test2 = "Ω"  # 比如希腊字母 Omega(大写Ω,小写ω )
test3 = "Γ"  # Γγ
v = test1.lower()  # 将英文字母全部小写
y = test1.casefold()  # 即可将英文字母全部小写,
z = test2.casefold()  # 还可以对其他未知映射进行转换
w = test3.casefold()  # 还可以对其他未知映射进行转换
print(v)  # 输出结果:python
print(y)  # 输出结果:python
print(z)  # 输出结果:ω
print(w)  # 输出结果:γ

这里只是举个例子,其实lower()也可以对希腊字母进行转换。看下面这个例子通过英文和德语中的字母还是分明的

s1 = "pyTHon"  # 英文
s2 = "ß"  # 德语

print(s1.lower())  # 输出结果:python 都被转变过来了
print(s1.casefold())   # 输出结果:python 都被转变过来了
print(s2.lower())  # 输出结果:ß 并没有被转变过来
print(s2.casefold())  # 输出结果:ss 德语的"ß"正确的小写是"ss"

 center()

# center() 设置宽度,并将内容居中
test = "python"
test2 = "python"
v = test.center(20)
v2 = test.center(20, "|")  # fillchar 的位置支持填1个字符,可以是中文,像"99"这种会报错

# center(self, width, fillchar=None): 看到这种情况,self可以直接忽略
# center(width, fillchar=None): 有 width,后面没有等号,就必须给带值
# center(width, fillchar=None): fillchar=None 后面要是有 None 就是默认,有等号的话后面可带值也可不带
print(v)  # 输出结果:       python    意思就是前后一共20个位置,python放中间,并且用空白占位
print(v2)  # 输出结果:|||||||python|||||||意思就是前后一共20个位置,python放中间,并且用"|"占位

count() 

# .count() 去字符串中寻找,寻找子序列的出现次数
test = "hello world"
v1 = test.count("w")
v2 = test.count("o", 5, 6)  # 从字符串test的第五个元素和第六个元素中间开始寻找
v3 = test.count("l", 5)  # 从字符串test的第五个元素以后开始寻找
print(v1, v2, v3)  # 输出结果:1 0 1

 endswith()   startswith()

# endswith() 判断以什么结尾
test = "hello world"
v = test.endswith("j")
print(v)  # 输出结果:False

# startswith() 判断以什么开始
y = test.startswith("h")
print(y)  # 输出结果:True

 expandtabs()   可以用来做表格

# 方法把字符串中的 tab 符号('\t')转为空格,tab 符号('\t')默认的空格数是 8。
text = "123456a\tbcdefg\t789789"
# 要注意的是 当expandtabs()中的位数正好是/t是时候,(6)位数个空格将字符串分开
# 如果不正好是位数的话,则与前面的"字符串"的个数+空格=位数(6)
v = text.expandtabs(6)
print(v)  
# 结果:123456a     bcdefg      789789
# 上面第一个是7位,所以a+5个空白格=6,这是与第二个的距离,距离排除a,则是5个
# 第二个bcdefg(正好是六位)和第三个789789之间距离是6个 

 

# expandtabs(15) 可以只用作制作类似于表格的样式
text = "name\tage\tsubjec\nSam\t19\tMath\nJamse\t21\tHistory\nJohn\t20\tEnglish"
# 让每一列都按照一定数值(这里是15)左对齐
v = text.expandtabs(15)
print(v)
'''
输出结果:
name           age            subjec
Sam            19             Math
Jamse          21             History
John           20             English
'''

find()

# 从开始往后找,找到第一个之后,获取其位置,后面还有没有不去管
test = "world hello world"
v1 = test.find("wo")
v2 = test.find("wo", 6, 15)
v3 = test.find("wo", 6, 10)
print(v1)  # 结果:0 表示在第一位
print(v2)  # 结果:12 表示在第13位
print(v3)  # 结果:-1 表示没找到

index()

# index() 方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,
# 则检查是否包含在指定范围内,该方法与 python find()方法一样,只不过如果str不在 string中会报一个异常。
test = "hello python"
v1 = test.index("th")
v2 = test.index("th", 1, 5)
print(v1)  # 输出结果:8
print(v2)  # 报错 ValueError: substring not found

format()

# format() 格式化,将一个字符串中的占位符替换为指定的值
test = "i am {name},i am {age} years old"  # 根据占位符替换
v1 = test.format(name="WuKong Sun", age=2000)
print(v1)

test = "i am {0},i am {1} years old"  # 根据位置替换,永远是从{0}开始
v2 = test.format("Liang ZhuGe", 3000)
print(v2)

format_map()

# format() 格式化,传入的值 的形式为:{"name": 'alex', "a": 19}
test = "i am {name},i am {age} years old"  # 根据占位符替换
v1 = test.format(name="WuKong Sun", age=2000) 
v2 = test.format_map({"name": "Liang ZhuGe", "age": 3000})  # 里面写成字典
print(v1)
print(v2)

isalnum()

# isalnum() 字符串中是否只包含 字母和数字 全字母,全数字也可以
test = "opop8863"
v = test.isalnum()
print(v)  # 输出结果:True

isalpha()

# isalpha() 判断字符串中时候包含字母,汉字,则返回True,数字会返回False
test = "python蟒蛇"
v = test.isalpha()
print(v)  # True

 isdigit()  isdecimal()  isnumeric()

# isdigit() isdecimal() isnumeric() 检查当前输入是否是数字
text = "123456"
v1 = text.isdigit()  # 支持十进制的数字
v2 = text.isdecimal()
v3 = text.isnumeric()
print(v1, v2, v3)  # 结果:True True True

text = "12345⑥"
v1 = text.isdigit()  # isdigit() 更强大,可以判断出其他形式的数字,如⑥
v2 = text.isdecimal()  # isdecimal() 不支持特殊数字
v3 = text.isnumeric()  # isnumeric() 也支持特殊形式数字,甚至比isdigit还要强大
print(v1, v2, v3)  # 结果:True False True

text = "12345⑥七"
v1 = text.isdigit()  # isdigit() 不支持汉字数字
v2 = text.isdecimal()  # isdecimal() 不支持汉字数字
v3 = text.isnumeric()  # isnumeric() 支持汉字数字,比isdigit还要强大
print(v1, v2, v3)  # 结果:False False True

isprintable()

# isprintable()是否存在不可显示的字符,并不是是否可以打印,
# 字符串中如果有打印看不见的东西则返回False
test = "abcd\tefg"  #此处存在不可显示的制表符,所以结果会返回False
v = test.isprintable()
print(v)  # 输出结果:False

test = "abcdefg"
v = test.isprintable()
print(v)  # 输出结果:True

isspace()

# isspace() 判断是否全部是空格
test1 = "python"
test2 = " python"
test3 = "py thon"
test4 = "py  thon"
test5 = "    "
v1 = test1.isspace()  # 输出结果:False
v2 = test2.isspace()  # 输出结果:False
v3 = test3.isspace()  # 输出结果:False
v4 = test4.isspace()  # 输出结果:False
v5 = test5.isspace()  # 输出结果:True 只有字符串中全是空格,别有其他的时候才是True

print(v1, v2, v3, v4, v5)  # 输出结果:False False False False True

istitle()  title()

# istitle() 判断是否是标题 即每个标题的单词首写字母是否为大写,对中文无效
text = "Hello Python you are a good language"
v1 = text.istitle()
print(v1)  # False

v2 = text.title()  # 将字符串转化为每个单词字母为大写的标题样式
print(v2)  # Hello Python You Are A Good Language 转化后的标题模式
v3 = v2.istitle()  # 判断转化后的字符串
print(v3)  # True

join()  非常!非常!非常!重要!

# join() 将字符串中的每一个元素按照指定分隔符进行拼接
test = "今日艰苦努力明日成就非凡"
print(test)
t = ' '
v = t.join(test)  # v = "_".join(test) 也可以直接在join前方加入分隔符
print(v)  # 结果:今 日 艰 苦 努 力 明 日 成 就 非 凡

  

 

posted @ 2018-02-25 22:51  千行路  阅读(284)  评论(0编辑  收藏  举报