周大胖子 发表于 2023-9-7 23:36:23

python 字符串基础




# # This is a sample Python script.
#
# # Press Shift+F10 to execute it or replace it with your code.
# # Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
#
#
# def print_hi(name):
#   # Use a breakpoint in the code line below to debug your script.
#   print(f'Hi, {name}')# Press Ctrl+F8 to toggle the breakpoint.
#
#
# # Press the green button in the gutter to run the script.
# if __name__ == '__main__':
#   print_hi('PyCharm_main')
#
# # See PyCharm help at https://www.jetbrains.com/help/pycharm/
#

# 参考文献
# https://blog.csdn.net/m0_51769031/article/details/127322960
# 字符串与 切片
# 一个概念: python 具有一个变量池,所有变量值相同的变量 所对应的值其实 是同一个值;
# 当产生这个变量时,如果 变量池中已经有这个值,那么 默认这个ID(值的地址)是一样的
# 1.切片的用法
p = 'ABCDEFGHIGKLMN'

n = '0123456789'

# 至前 内置函数
# len(x)   #返回字符串x的长度
# str(x)   #将任意类型的x转化为字符串类型
# chr(x)   #返回Unicode编码为x的字符
# ord(x)   #返回字符x的Unicode编码
# hex(x)   #将整数x转化为十六进制数
# oct(x)   #将整数x转化为八进制数

# 1.find 成功则返回下标(最左侧首次出现) 不成功返回 -1
# 参数说明 str.find(sub[,start[,end]])
# str表示被查找的字符串。sub表示查找的子串。start表示开始索引,缺省时为0。end表示结束索引,缺省时为字符串的长度。
print(n.find('3'))# 3
print(n.find('b'))# -1
print(p.find('CD'))# 2

# 2.rfind同find但是查的的是最后一次出现的位置
print(p.rfind('C'))

# 3.index 存在则返回下标(最左侧首次出现) 不存在则抛出异常
print(p.index('C'))
# print(p.index('c'))

# 4. rindex(最右侧 最后出现) 同index


# 5.count()#用来返回一个字符串在另一个字符串中出现的次数,若不存在则返回
m = 'pcyuasdoiuuuuusdasoaso'
print('u出现了%d次' % (m.count('u')))

# 字符串和列表的相互转化
# split 将字符串格式化为列表
# str.split(]) 参数说明:
# sep是分割符,maxsplit是最大分割次数。如果传入的参数sep为空或为None,则默认使用空格作为分隔符
# 注意这里的最大分割数,并不是代表分割为几段 二十代表分割出来几段 如果是1 那就是2个段

s = 'bird,fish,monkey,rabbi,666'
print(s.split(','))# ['bird', 'fish', 'monkey', 'rabbi', '666']
print(s.rsplit(','))# ['bird', 'fish', 'monkey', 'rabbi', '666']
print(s.split(',', 1))# ['bird', 'fish,monkey,rabbi,666']
print(s.rsplit(',', 2))# ['bird,fish,monkey', 'rabbi', '666']

# partition()    #以指定字符串为分隔符将原字符串分割为3个部分,分隔符之前的字符串,分隔符字符串和分隔符之后的字符串
# rpartition()   #以指定字符串为分隔符将原字符串分割为3个部分,分隔符之前的字符串,分隔符字符串和分隔符之后的字符串
# 就是生成一个长度为3的列表左侧是 指定字符之前的, 右侧是指定字符之后的中间是指定的字符 各位列表中的一项
print(p.partition('B'))# ('A', 'B', 'CDEFGHIGKLMN')
print(p.rpartition('B'))# ('A', 'B', 'CDEFGHIGKLMN')

# join 列表转为字符串 插入指定字符
s_list = ['name', 'yesa', 'vvv']
print('--'.join(s_list))# name--yesa--vvv

# lower()   #将字符串转换为小写字符串
# uppper()    #将字符串转换为大写字符串
# capitalize()    #将字符串首字母变为大写
# title()         #将字符串中每个单词的首字母都变为大写
# swapcase()      #将字符串中的字符大小写互换

# replace()      #替换字符串中指定字符或子字符串全局替换
sm = 'rorororoftt'
cm = sm.replace('t', '溜溜溜')
print(cm)
# rorororof溜溜溜溜溜溜


# strip()         #删除字符串两端空白字符 或者指定字符串
# rstrip()      #删除字符串右端空白字符 或者指定字符串
# lstrip()      #删除字符串左端空白字符 或者指定字符串
bm = "=====das==da==="
bb = bm.strip('==')
print(bb)
# das==da



# startswith()   #判断字符串是否以指定字符开始
# endswith()      #判断字符串是否以指定字符结束
a = bm.startswith('==')
print(a)
# True



# isupper()       #是否全为大写
# islower()       #是否全为小写
# isdigit()       #是否全为数字
# isalnum()       #是否全为字母或汉字或数字
# isalpha()       #是否全为字母或汉字


# center()         #字符串居中对齐
# ljust()          #字符串居左对齐
# rjust()          #字符串居右对齐
# zfill()          #输出指定宽度,不足的左边填0



# format 格式方法
# 1 format()的默认顺序和指定顺序

cstr = '这是{}年级,{}班的学生{}'.format('三','8','周杰伦')
print(cstr)
# 这是三年级,8班的学生周杰伦

cstr = '这是{0}年级,{2}班的学生{1}'.format('三','周杰伦','八')
print(cstr)
# 这是三年级,八班的学生周杰伦 注意后方元组中下标从0 开始

# 当使用format()方法格式化字符串的时候,首先需要在"{}”中输入“:”,然后在":"之后分别设置<填充字符><对齐方式><宽度>。

cstr = "{:y^12}".format( '内容')
print(cstr)
# yyyyy内容yyyyy

cstr2 = "{:f<12}".format('内容')
print(cstr2)
# 内容ffffffffff


cstr2 = "{:f>12}".format('内容')
print(cstr2)
# ffffffffff内容

# 利用format()方法设置数字的保留位数。
cstr = "{:0>6.4}".format('23.1234567')
print(cstr)
# 0023.1


cstr ="{:.4}".format('23.1234567')
print(cstr)
# 23.1

cstr2 ="{:.2f}".format('23.1234567')
print(cstr2)
#

cstr3 ="{:.5d}".format('23.1234567')
print(cstr3)




页: [1]
查看完整版本: python 字符串基础