Python之路day03-进制转换_字符串切片_strip_replace_split

1,244次阅读
一条评论

前言时刻

又是很水的一上午,不过也学习到了一些新知识,比如st.isnum、st.isalpha等等

来来总结一波:

今天讲的有:进制转换、str字符串函数的用法:切片、replace、split、isalpha、strip、count、join。

差不多就这些了,学就完事了,总结那是必须的。

1、进制转换

常用的就是10进制、2进制(binary)、8进制(octonary)、16进制(hexadecimal),他们之前的转换是需要掌握的。

1.1、10进制向2、8、16进制转换

主要就是这三个内置函数:bin、oct、hex,其分别是2进制、8进制、16进制的英文缩写,具体可看上面。

# 1、进制转换

tmp = 5

# 10进制转2进制
print(bin(tmp))    # 0b101

# 10进制转8进制
print(oct(tmp))    # 0o5

# 10进制转16进制
print(hex(tmp))   # 0x5

# 返回数的二进制有效位数
print(tmp.bit_length())    # 3  对应二进制:0b101正好是3位

1.2、2/8/16进制向10进制转换

主要用到 int(str, num) 函数进行互转,具体用法见下方。

#  2/8/16进制向10进制转换

# 1、2进制转10进制
print(int("0b101", 2))   # 5

# 2、8进制转10进制
print(int("0o17", 8))    # 15

# 3、16进制转10进制
print(int("0x14", 16))      # 20

# 4、8进制转2进制
print(bin(int("0o17", 8)))   # 0b1111

# 其他的也同理,使用int作为中间量,进行互转。

下面介绍的都是字符串的种种操作方法,记住即可。

2、字符串切片

直接看例子吧,三种用法都有。

st = "thepathofpython"

# 1、一般切片
print(st[0:3])   # the  区间包前不包后

# 2、带步长的切片  **
print(st[0:8:3])   # tph  最后面的一个参数是步长

# 3、从后往前扫描 切片
print(st[-6:])   # python      右区间若无值,则默认包括末尾 到底

# 3.1、从后往前扫描 + 步长
print(st[-6::-2])   # potph   步长为负数 表示从右到左扫
print(st[-6::2])   # pto      步长为正数 表示从左到右扫

# 4、重点 面试题常考 翻转字符串 ***
print(st[::-1])   # nohtypfohtapeht    左区间若无值,则默认从首位开始

3、字符串字母大小写

将字符串中的所有字母转成大写或者小写,upper和lower函数

tmp = "xiYuanGongZi"

# 字符串字母全部变成大写
print(tmp.upper())   # XIYUANGONGZI

# 字符串中字母全部变成小写
print(tmp.lower())   # xiyuangongzi

4、startswith和endswith

判断字符串开头和尾端是否等于某一字符串。

tmp = "thepathofpython"

print(tmp.startswith('the'))   # True
print(tmp.startswith('zhe'))   # False

print(tmp.endswith("python"))   # True

5、Replace函数

tmp.replace(old, new, count)

替换一些特定的字符串,少量推荐。若有大量的字符要替换,推荐使用re正则,后面会有介绍。

replace函数用法例子:

tmp = "thepathofthepython"

# tmp.replace(old, new, count)

# 1、简单替换
print(tmp.replace("the", "study"))  # studypathofstudypython

# 2、限制替换次数
print(tmp.replace("the", "study", 1))   # studypathofthepython  第三个count 限制替换的old的次数

6、strip函数

# 左右两端
st.strip()
st.strip("name")

# 单独去除一端空白/自定义字符
st.lstrip()
st.rstrip()

默认去除字符串两端的空白字符如:空格、制表符\t、换行符\n。当然你也可以设置自定义的字符。

strip例子:

tmp = "\n \tthepathof\tpython\n\t"

# 1、默认两端去除空白字符
# 两端从头扫描 去除空白字符,一旦碰到非空表字符,就停止扫描。所以of和python的\t没有被移除
print(tmp.strip())   # thepathof    python 

tmp2 = "tthepathof\tpython"
# 2、设置特定字符
# 重点  strip中的字符串参数 ,每一个字符都作为要消除的,并不是整体。两端扫描,遇到参数字符串中的任意字符就消除,若没遇到就停止扫描。
print(tmp2.strip("thpen"))   # athof  pytho

# 3、单端扫描
print(tmp2.lstrip("thpen"))   # athof python  从首端扫描
print(tmp2.rstrip("thpen"))   # tthepathof    pytho  从未端扫描

# 总结:这点有点绕,与replace不同是,strip是从两端扫描,只要遇到非消除字符就停止扫描,结束。

7、split函数

st.split(char, count) ,split函数用于分割字符串 ,变成列表.

split用法例子:

tmp = "*the*path*of*python"

# 1、普通分割
print(tmp.split("*"))   # ['', 'the', 'path', 'of', 'python']

# 2、限制分割数量
print(tmp.split("*", 3))   # ['', 'the', 'path', 'of*python']   第二个参数count,限制分割的数量å

# 没啥难点,注意分割会产生n+1的字符串(n是要分割的字符串在原字符串中的数量)

8、join函数

join函数语法:

str.join(sequence)

# sequence -- 要连接的元素序列。

这个函数简直就是split函数的cp,一个分割,一个合并。

join函数例子:

tmp = ['', 'the', 'path', 'of', 'python']

print("*".join(tmp))   # *the*path*of*python

9、is系列

和C++中的用法差不多,连函数名称都一样,果然同宗生😂。

str.isalpha()   # 字符串的所有字符是否都是字母
str.isalnum()   # 字符串的所有字符是否都是字母或数字
str.isdecimal()  # 字符串的所有字符是否都是十进制数字

例子:

tmp = "zan66"
tmp2 = "zan"
tmp3 = "666"

print(tmp.isalnum())  # True 字符串的所有字符是否都是字母或数字
print(tmp2.isalpha())  # True 字符串的所有字符是否都是字母
print(tmp3.isdecimal())  # True 字符串的所有字符是否都是十进制数字

10、count函数

主要与统计某个字符在字符串中出现的次数。

count函数例子:

tmp = "thepathofpython"

print(tmp.count("t"))   # 3
print(tmp.count("z"))   # 0

11、in/not in用法

一般用于列表中,判断某成员是否在列表中。

例子:

tmp = ['the', 'path', 'of', 'python']
print('python' in tmp)   # True
print('love' not in tmp)  # True

---2021.7.25新增----

12、字符串格式化对齐

zfill、ljust、rjust、center

str.zfill(width),给字符串左边补 0 凑成 width 位。

  • width:int型

str.ljust(width,fillchar),str左对齐,不足 width 位用 fillchar(默认是空格) 填充凑齐。

  • fillchar:要填充的字符串

str.rjust(width,fillchar),str右对齐,不足 width 位用 fillchar(默认是空格) 填充凑齐。

  • fillchar:要填充的字符串

str.center(width,fillchar),str两边对齐,不足 width 位用 fillchar(默认是空格) 填充凑齐。

  • fillchar:要填充的字符串

小例子:

s = '7'.zfill(3)   # 007
s2 = '6'.ljust(3, '8')   # 688
s3 = '7'.rjust(3, '0')   # 007
s4 = '8'.center(5, '0')   # 00800

总结:

肝的我背疼,歇歇,总算是写完了。本来不想写的,但是必须的坚持,每天必定要完成计划。跟着计划走才会有有进步。

全程手敲,对知识的理解又加深了,明天继续加油。

参考链接:

https://www.runoob.com/python/att-string-join.html

https://www.zwjjiaozhu.top

3
西园公子
版权声明:本站原创文章,由西园公子2021-04-03发表,共计3981字。
转载提示:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(一条评论)
载入中...
西园公子 博主
2021-04-04 21:04:51 回复

Python之路第3天的总结