IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> python 字符串 b站大学 -> 正文阅读

[Python知识库]python 字符串 b站大学

  • 字符串驻留机制(地址)
  • 查询 推荐.find() .rfind()
  • 大小写转换 .upper() .lower() .swapcase() .capitalize() .title()
  • 内容对齐 .center() .ljust() .rjust() .zfill()
  • 劈分 .split() .rsplit()
  • 字符串内容判断 .isidentifier() .isspace() .isalpha() .isdecimal() .isnumeric() .isalnum()
  • 替换 .replace()
  • 合并 .join()
  • 比较 >< ord(字符) chr(ascii码值)
  • 切片 [start,end,step]
  • 格式化字符串 %占位符, {}占位符
  • 字符编码 .encode() .encode()
# ==================== 字符串的驻留机制
a = 'python'
b = "python"
c = '''python'''
print(id(a))
print(id(b))
print(id(c)) # 地址相同!
# 驻留:仅保存一份相同且不可变字符串,只把相同字符串的地址赋给新建变量
# 交互式演示驻留机制(控制台)参考:https://www.bilibili.com/video/BV1wD4y1o7AS?p=76

# ==================== 字符串的常用操作
# 查询 .index() .rindex() .find() .rfind() 推荐使用后两个
s = 'hello,hello'
print(s.index('lo'))  # 3 if能找到,return子串第一次出现的位置
print(s.find('lo'))   # 3 同上
print(s.rindex('lo')) # 9 if能找到,return子串最后一次出现的位置
print(s.rfind('lo'))  # 9 同上
# print(s.index('k'))   # ValueError if找不到,则报错
print(s.find('k'))      # -1 if找不到,return -1
# print(s.rindex('k'))  # ValueError if找不到,则报错
print(s.rfind('k'))     # -1 if找不到,return -1

# 大小写转换 .upper() .lower() .swapcase() .capitalize() .title()
s = 'Hello,World'
print(id(s))   # 9719088
a = s.upper()  # 全转大写
print(a,id(a)) # HELLO,WORLD 7265904 相当于新串
b = s.lower()  # 全转小写
print(b)       # hello,world
c = s.swapcase() # 大转小,小转大
print(c)         # hELLO,wORLD
d = s.capitalize()  # 仅首字母大写
print(d)            # Hello,world
e = s.title()    # 每个单词首字母大写
print(e)         # Hello,World

# 内容对齐 .center() .ljust() .rjust() .zfill()
s = 'hello,Python'
print(s.center(20,'*')) # ****hello,Python**** 居中对齐
print(s.ljust(20,'^'))  # hello,Python^^^^^^^^ 左对齐
print(s.rjust(20))      #         hello,Python 右对齐,默认符号为空格
print(s.zfill(20))      # 00000000hello,Python 右对齐,左侧用0填充
print(s.ljust(5))       # hello,Python 长度小于原字符,则返回原字符
print('-233'.zfill(8))  # -0000233

# 劈分(分段) .split() .rsplit()
s = 'hello world Python'
s1 = 'hello|world|Python'
# .split()默认分隔符为空格,参数:sep指定分隔符,maxsplit指定劈分次数 .rsplit()从右开始识别劈分
print(s.split())              # ['hello', 'world', 'Python']
print(s1.split(sep='|'))      # ['hello', 'world', 'Python']
print(s1.split(sep='|',maxsplit=1))   # ['hello', 'world|Python']
print(s.rsplit(maxsplit=1))   # ['hello world', 'Python']

# 判断字符串内容 .isidentifier() .isspace() .isalpha() .isdecimal() .isnumeric() .isalnum()
# 1  .isidentifier() 合法标识符:字母数字下划线
# 2  .isspace() 空白字符:回车 换行 水平制表符
# 3  .isalpha() 全部是字母(英文 中文)组成
# 4  .isdecimal() 十进制数字(罗马数字不是)
# 5  .isnumeric() 数字(各国语言的数字)
# 6  .isalnum() 全部由字母或数字组成
print('1,','hello,world'.isidentifier()) # False
print('1,','张三2_'.isidentifier()) # True
print('2,','\t'.isspace()) # T
print('3,','hhh'.isalpha()) # T
print('3,','张三'.isalpha()) # T
print('3,','张三2'.isalpha()) # F
print('4,','101'.isdecimal()) # T
print('4,','ⅠⅡⅣ'.isdecimal()) # F
print('4,','八八六'.isdecimal()) # F
print('5,','ⅠⅡⅣ'.isnumeric()) # T
print('5,','八八六'.isnumeric()) # T
print('6,','ab78张'.isalnum()) # T
print('6,','!。'.isalnum()) # F

# 字符串替换合并 .replace() .join()
s = 'hello Python Python Python'
print(s.replace('Python','Java'))   # hello Java Java Java
print(s.replace('Python','Java',2)) # hello Java Java Python
print('*'.join('Python'))  # P*y*t*h*o*n
lst = ['hello','Python','world']
print('|'.join(lst))       # hello|Python|world
t = ('hello','Python','world')
print(''.join(t))          # helloPythonworld

# ==================== 字符串的比较
# 一个一个字符比较,比的是原始值ordinal value
print('apple' > 'app')    # T
print('apple' < 'banana') # T
print(ord('a'), ord('b')) # 97 98
print(chr(97), chr(98))   # a b
# 再次强调:== 与 is 区别 :==比较value is比较id地址

# ==================== 字符串的切片[start,end,step]
s = 'hello,Python'
s1 = s[:5] # hello
s2 = s[6:] # Python
s3 = '!'
newstr = s1+s3+s2
print(newstr) # hello!Python
print(id(s))
print(id(s1))
print(id(s2))
print(id(s3))
print(id(newstr)) # 以上地址不同
s = '0123456789'
print(s[1:9:2]) # 1357
print(s[::2])   # 02468
print(s[::-2])  # 97531
s = 'hello,Python'
print(s[-6::1]) # Python

# ==================== 格式化字符串
# 1,%占位符 %s:字符串 %i/%d:整数 %f:浮点数
# 2,{}占位符 .format()
# 输出:我叫张三,今年21周岁
name = '张三'
age = 21
print('我叫%s,今年%i周岁' % (name,age))
print('我叫{0},今年{1}周岁'.format(name,age))
print(f'我叫{name},今年{age}周岁')
print('%10f' % 3.1415926)     #  3.141593
print('%10.3f' % 3.1415926)   #     3.142
print('%-10.3f' % 3.1415926)  # 3.142
print('{0}'.format(3.1415926))      # 0表示占位符,可以不写
print('{:.3}'.format(3.1415926))    # 3.14   .3表示是三位数
print('{:.3f}'.format(3.1415926))   # 3.142 .3f表示3位小数
print('{:10.3}'.format(3.1415926))  #       3.14 10表示宽度

# ==================== 字符串的编码转换 .encode() .encode()
# 编码.encode() 解码.encode()
s = '天涯共此时'
print(s.encode(encoding='GBK'))   # 在GBK这种编码格式中,一个中文占2字节
print(s.encode(encoding='UTF-8')) # 在UTF-8编码格式中,一个中文占3字节
# so,不同的编码格式决定了它占的字节数
byte = s.encode(encoding='GBK') # 编码
print(byte.decode(encoding='GBK')) # 解码 天涯共此时
  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2021-07-29 11:35:59  更:2021-07-29 11:36:28 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/8 14:28:37-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码