python之字符串

 
 
 
 
 

字符串: s='不迟到' s1="不早退" s2=''' <html> <head></head> <body> </body> </html> ''' print(s1) 小知识: java 中基本数据类型:4类8种! 在python中只要不是变量传递方式,带有字符串运算的都是新内存. python是解释执行。没有内存优化,编译优化。 字符串常量:即不变的量 新内存:开辟新空间 2.可以使用中括号和下标来获取单个字符,或截取字符串:即为 切片 s='不迟到,多岁的康师傅,恢复伤口,双方都恐怕,哈哈哈h' print(s[s.__len__()-1]) print(s[-2]) 如果给2个参数:4-8: print(s[4:8])#注意默认最后一位不显示; print(s[-9:-2])#注意:前面的值一定要小于后面的值 print(s[::2])#值加2 一次叠加显示即:各一个显示一个 print(s[-1::-1])#所有字倒过来显示 :即从尾部到头 print(s[::-1])#也是从尾部到头部依次全部显示 字符串用法: in:判断是否在些字符串中从在bool not in:判断是否不在此字符串中bool r/R:一样 sun='哈哈,客户\t\t收到,是\t\t东方航空,hd'

如图:若果想要显示\t该肿么办: sun=r'哈哈,客户\t\t收到,是\t\t东方航空,hd'# r的大小写都可以 print(sun)

如图:字符串格式化 s='今年' age=23 print('%s今晚 请%s 去%s 吃%s 喝%s 消费%.2f万元' %('美女','我','白宫','大餐','茅台',18)) #%.2 : 18.00 2个零 print('%s 天气不错 '%'明天') print('{}{}岁'.format(s,age,s)) print('{name} 今年 {age}'.format(name='张三',age=age)) print('{0} 今年 {1}'.format('张三',age)) print('{{{0}}} 今年 {1}'.format('张三',age)) 给张三加一个中括号即:{张三}

如图:字符串的一些函数: s='不迟到,多岁的康师傅,恢复伤口,双方都恐怕,哈哈哈h' len() 获取参数的长度 str list tuple print(s.__len__) print(len(s)) count:查找的意思 即 返回有几个值 print(s.count('静音',32)) print('hello,python red boks'.capitalize())#第一个单词的首字母大写 print('hello,python red boks'.title())#所有的单词首字母大写 列表: listA='hello,python red,boks'.split(' ') listB=[] for i in listA: temp= i.split(',') for j in temp: listB.append(j.capitalize()) print(listB)

如图: 变成字符串: listA='hello,python red,boks'.split(' ') listB=' ' for i in listA: temp= i.split(',') for j in temp: listB+=j.capitalize()+' ' print(listB)

如图:

知识扩展 str='hello,python red bo dj jo jdf my name is' print(str.split(' ',2)) print(str.rfind('python'))#find:查找字符串,返回找到的字符串下标; 即:如果没有为 -1 rfind:从右面开始 print(str.index('python3'))和find一样 replace():替换 注:但是原字符不会被改变 print(str.replace('python','java')) print(str) 小练习 输入正确的邮箱格式,并提取用户名: email='xiaobai@163.com' if email.find('@')!=-1 and email.find('.')!=-1\ and email.find('@')!=0\ and email.find('.')>email.find('@'): print('邮箱正确用户名:',email[0:email.find('@')]) else: print('格式不正确')

如图:
如果 email='xiaobai@163.com'格式不正确即:没有加‘@’
则会显示 格式不正确
如图:

知识大开阔 print(str.startswith('python',6,12))#从开头数‘python’是从6到12. startswith:以XX开头; endswith:以XX结尾; print(str.endswith('red',6,16))

如图: 全部转换大小写: print('Hello'.lower().__eq__('HELLO'.lower()))#即:小写 print('Hello'.upper().__eq__('HELLO'.upper()))#即:大写 print('hello'.ljust(8,'*')) 即ljust补齐 以*补齐 共八位 print('hello'.rjust(8,'*')) 即:ljust:‘*’在字符串的左边 rjust;‘*’在字符串的右边 strip:去空格 lstrip()去掉左侧空格 rstrip():去掉右侧空格 案例:在‘hello’左边1个空格右边2个空格: print(len(' hello '.strip())) print(len(' hello '.rstrip())) print(len(' hello '.lstrip()))

如图: str='hello,python red bo dj jo jdf my name is' print(str) print(str.split(' ')) print(str.partition(' '))

即:由 头 分隔符 尾 3部分组成 并且返回值叫 元组

如图

 
 

str='hello,py\nthon red b\no dj jo jdf my name is' print(str.splitlines(True))splitlines是否显示换行符 默认值为false 如图: str='hello'即:不能加逗号或者空格 print(str.isalpha())判断字符串是否是全字母/文字 即:true 或false print(str.isdigit())判断字符串是否全是数字 即:true 或false print(str.isalnum())判断字符串是否由数字和字母/文字组成 即:true 或false listB=['你好','我是','你的生命'] str='-__-' print(str.join(listB))join将str和listB通过-__-来拼接起来

如图:

完了!(嘿嘿)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值