博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python学习笔记-5字符串
阅读量:3907 次
发布时间:2019-05-23

本文共 6405 字,大约阅读时间需要 21 分钟。

a = 'Hello'name = 'lzy'print(type(a))  #打印类型,输出:
print('---------------------字符串:单引号 或 双引号---------------------')name1='Tom'b="""i am Tom"""c="""i\'m Tom"""print(name1)print(b)print(c)print('---------------------字符串输出---------------------')print('Hello world!')print('My name is %s'%name1)print(f'My name is {name1}')print('---------------------字符串输入---------------------')name=input('请输入名字:') # 输入的内容赋值给namepsw=input('请输入密码:')print('您输入的名字是%s'%name)print(f'您输入的的密码是{psw}')print('---------------------字符串下标---------------------')name = "abcdef"print(name[1])print(name[0])print(name[2])print('---------------------切片[::]---------------------')# 序列[开始位置下标:结束位置下标:步⻓]# 1. 不包含结束位置下标对应的数据, 正负整数均可; [)# 2. 步⻓是选取间隔,正负整数均可,默认步⻓为1。name = "abcdefg"print(name[2:5:1]) # cdeprint(name[2:5]) # cdeprint(name[:5]) # abcde 0:5:1print(name[1:]) # bcdefg 1:最后一个:1print(name[:]) # abcdefgprint(name[::2]) # aceg 0:最后一个:隔一个取一个print(name[:-1]) # abcdef, 负1表示倒数第⼀个数据 0到倒数第一个数print(name[-4:-1]) # def 倒数第四个开始, 至倒数第一个数结束 ,使用默认步长1print(name[::-1]) # gfedcba 全部倒着print('------------------------查找------------------------')print('------------------------find():检测某个⼦串是否包含在这个字符串中,如果在返回这个⼦串开始的位置下标,否则则返回-1。------------------------')print("语法: 字符串序列.find(⼦串, 开始位置下标, 结束位置下标)")mystr = "hello world and itcast and itheima and Python"print(mystr.find('and')) # 12 从下标0开始查找,在下标12找到print(mystr.find('and', 15, 30)) # 23 查找下标15至30 找到下标23print(mystr.find('ands')) # -1 找不到返回-1print('rfind(): 和find()功能相同,但查找⽅向为右侧开始')print('------------------------index():检测某个⼦串是否包含在这个字符串中,如果在返回这个⼦串开始的位置下标,否则则报异常------------------------')print('语法:字符串序列.index(⼦串, 开始位置下标, 结束位置下标')mystr = "hello world and itcast and itheima and Python"print(mystr.index('and')) # 12 从下标0开始查找,在下标12找到print(mystr.index('and', 15, 30)) # 23 查找下标15至30 找到下标23#print(mystr.index('ands')) # 报错 找不到print('rindex():和index()功能相同,但查找⽅向为右侧开始。')print('------------------------count():返回某个⼦串在字符串中出现的次数------------------------')print(' 字符串序列.count(⼦串, 开始位置下标, 结束位置下标)')mystr = "hello world and itcast and itheima and Python"print(mystr.count('and')) # 3print(mystr.count('ands')) # 0print(mystr.count('and', 0, 20)) # 1print('------------------------ 字符串序列.replace(旧⼦串, 新⼦串, 替换次数)------------------------')mystr = "hello world and itcast and itheima and Python"# 结果:hello world he itcast he itheima he Pythonprint(mystr.replace('and', 'he'))# 结果:hello world he itcast he itheima he Pythonprint(mystr.replace('and', 'he', 10))# 结果:hello world and itcast and itheima and Pythonprint(mystr) #替换了但是没有改变mystr的值print('注意:替换次数如果查出⼦串出现次数,则替换次数为该⼦串出现次数')print('注意:数据按照是否能直接修改分为可变类型和不可变类型两种。字符串类型的数据修改的时候不能改变原有字符串,属于不能直接修改数据的类型即是不可变类型')print('------------------------split():按照指定字符分割字符串。------------------------')print('字符串序列.split(分割字符, num)')print('注意:num表示的是分割字符出现的次数,即将来返回数据个数为num+1个')mystr = "hello world and itcast and itheima and Python"# 结果:['hello world ', ' itcast ', ' itheima ', ' Python']print(mystr.split('and'))# 结果:['hello world ', ' itcast ', ' itheima and Python']print(mystr.split('and', 2))# 结果:['hello', 'world', 'and', 'itcast', 'and', 'itheima', 'and', 'Python']print(mystr.split(' '))# 结果:['hello', 'world', 'and itcast and itheima and Python']print(mystr.split(' ', 2))print(mystr) # hello world and itcast and itheima and Pythonprint('注意:如果分割字符是原有字符串中的⼦串,分割后则丢失该⼦串')print('------------------------join():⽤⼀个字符或⼦串合并字符串,即是将多个字符串合并为⼀个新的字符串。------------------------')print(' 字符或⼦串.join(多字符串组成的序列)')list1 = ['a', 'b', 'c', 'd']t1 = ('aa', 'bb', 'cc', 'ddd')# 结果:a_b_c_dprint('_'.join(list1))# 结果:aa...bb...cc...dddprint('...'.join(t1))str='-'.join(list1) # 结果保存给strprint(str)print(list1) # 不变 还是原来的数print('------------------------capitalize():将字符串第⼀个字符转换成⼤写------------------------')mystr = "hello world and itcast and itheima and Python"# 结果:Hello world and itcast and itheima and pythonprint(mystr.capitalize())print('------------------------lower():将字符串中⼤写转⼩写------------------------')mystr = "HFGJGHKhfgjjghjhglkj"print(mystr)# 结果:hfgjghkhfgjjghjhglkjprint(mystr.lower())print('------------------------upper():将字符串中⼩写转⼤写------------------------')# 结果:HFGJGHKHFGJJGHJHGLKJprint(mystr.upper())print(mystr)print('------------------------title():将字符串每个单词⾸字⺟转换成⼤写。------------------------')mystr = " hello world and itcast and itheima and Python "# 结果:Hello World And Itcast And Itheima And Pythonprint(mystr.title())print('------------------------lstrip():删除字符串左侧空⽩字符。------------------------')print(mystr.lstrip())print('------------------------rstrip():删除字符串右侧空⽩字符。------------------------')print(mystr.rstrip())print('------------------------strip():删除字符串两侧空⽩字符------------------------')print(mystr.strip())print('------------------------ljust():返回⼀个原字符串左对⻬,并使⽤指定字符(默认空格)填充⾄对应⻓度 的新字符串------------------------')print(' 字符串序列.ljust(⻓度, 填充字符)')name = 'lzy'print(name.ljust(10,'.'))print(name.ljust(10))print('rjust():返回⼀个原字符串右对⻬,并使⽤指定字符(默认空格)填充⾄对应⻓度 的新字符串,语法和ljust()相同。')print('center():返回⼀个原字符串居中对⻬,并使⽤指定字符(默认空格)填充⾄对应⻓度 的新字符串,语法和ljust()相同')print('--------------------判断------------------------')print('++++++++++++++++++++++++++startswith():检查字符串是否是以指定⼦串开头,是则返回 True,否则返回 False。如果设置开始和结束位置下标,则在指定范围内检查')print('字符串序列.startswith(⼦串, 开始位置下标, 结束位置下标)')mystr = "hello world and itcast and itheima and Python "# 结果:Trueprint(mystr.startswith('hello'))# 结果Falseprint(mystr.startswith('hello', 5, 20))print('++++++++++++++++++++++++endswith()::检查字符串是否是以指定⼦串结尾,是则返回 True,否则返回 False。如果设置开始和结束位置下标,则在指定范围内检查。')print('字符串序列.endswith(⼦串, 开始位置下标, 结束位置下标)')mystr = "hello world and itcast and itheima and Python"# 结果:Trueprint(mystr.endswith('Python'))# 结果:Falseprint(mystr.endswith('python'))# 结果:Falseprint(mystr.endswith('Python', 2, 20))print('++++++++++++++++++++++++isalpha():如果字符串⾄少有⼀个字符并且所有字符都是字⺟则返回 True, 否则返回 False')mystr1 = 'hello'mystr2 = 'hello12345'# 结果:Trueprint(mystr1.isalpha())# 结果:Falseprint(mystr2.isalpha())print('++++++++++++++++++++++++isdigit():如果字符串只包含数字则返回 True 否则返回 False。')mystr1 = 'aaa12345'mystr2 = '12345'# 结果: Falseprint(mystr1.isdigit())#Trueprint(mystr2.isdigit())print('++++++++++++++++++++++++isalnum():如果字符串⾄少有⼀个字符并且所有字符都是字⺟或数字则返 回 True,否则返回false')mystr1 = 'aaa12345'mystr2 = '12345-'# 结果:Trueprint(mystr1.isalnum())# 结果:Falseprint(mystr2.isalnum())print('++++++++++++++++++++++++sspace():如果字符串中只包含空⽩,则返回 True,否则返回 False')mystr1 = '1 2 3 4 5'mystr2 = ' '# 结果:Falseprint(mystr1.isspace())# 结果:Trueprint(mystr2.isspace())

转载地址:http://rhqen.baihongyu.com/

你可能感兴趣的文章
adb介绍
查看>>
Android lint相关
查看>>
WebDriver介绍
查看>>
testNG介绍
查看>>
Python integer ranges
查看>>
Python List Pop
查看>>
IM Architecture
查看>>
Apache Kafka
查看>>
Message Queue - MQ Intro
查看>>
Python chr ord
查看>>
理解Docker容器
查看>>
Java类数组
查看>>
i++
查看>>
Binary Tree Maximum Path Sum
查看>>
Divide and Conquer
查看>>
BFS Vs DFS (Level Order Traversal)
查看>>
Lowest Common Ancestor
查看>>
EM Intro
查看>>
Online Learning
查看>>
Delete Last Element
查看>>