博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python基础一:之字符串
阅读量:7034 次
发布时间:2019-06-28

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

组成字符串的两种方式

a = 'lao'b = 'wang'd = a + be = "===%s===" % (a+b)  #a与b必须都是字符串print(d)print(e)

字符串下标

name = 'abcdef'print(name[0])print(name[5])print(name[len(name)-1])print(name[-1])

字符串切片

name = 'abcdefABCDEF'#切片语法:string[起始位置:终止位置:步长]print(name[2:5])        #cdeprint(name[2:])         #cdefABCDEFprint(name[2:-2])       #cdefABCDprint(name[2:-1:2])     #ceACEprint(name[0:])#以下写法将字符串逆序print(name[-1::-1])         #FEDCBAfedcbaprint(name[::-1])           #前面两个都省略,关键看后面的步长

字符串函数

python基础一:之字符串

#coding=utf-8myStr = "hello world and we are the world"print(myStr.count("world"))     #2#统计字符串出现的次数,如果没有则返回0print(myStr.replace("world","WORLD"))       #hello WORLD and we are the WORLD#注意:replace不会对原字符串产生任何影响print(myStr.replace("world","xxxxx",1))     #hello xxxxx and we are the world#replace第一个参数为:要替换的字符串,第二个参数为:替换成什么字符串,第三个参数为:如果要替换的字符串出现多次那么要替换的个数print(myStr.split(" "))         #['hello', 'world', 'and', 'we', 'are', 'the', 'world']print(myStr.capitalize())       #Hello world and we are the worldfile_name = "xxxxx.txt"print(file_name.endswith(".txt"))           #True#ljust , rjust , centerlyric = "Tonight , I feel close to you"print(lyric.center(50))             #          Tonight , I feel close to you           print(lyric.ljust(50))print(lyric.rjust(50))#lstrip , rstrip , stripname = "  who are you "print(name.lstrip())print(name.rstrip())print(name.strip())#partition , rpatitionprint(myStr.partition("world"))         #('hello ', 'world', ' and we are the world')print(myStr.rpartition("world"))        #('hello world and we are the ', 'world', '')#spliteline     按照换行符进行分隔content = "hello\nworld\nxxx\nyyy"print(content.splitlines())                     #['hello', 'world', 'xxx', 'yyy']#isalpha            判断是否是纯字母#isdigit            判断是否是纯数字#isalnum         判断是否是字母与数字#joina = ['aaa','bbb','ccc']b = '='print(b.join(a))                #aaa=bbb=cccb = ' 'print(b.join(a))                #aaa bbb ccc

面试题:给定一个字符串str,返回使用空格或者'\t'分割后的倒数第二个子串

str = "haha nihao a \t heihei \t woshi nide \t hao \npenyou"print(str)sstr = str.split()print(sstr)                 #['haha', 'nihao', 'a', 'heihei', 'woshi', 'nide', 'hao', 'penyou']print(' '.join(sstr))

转载于:https://blog.51cto.com/12332406/2047832

你可能感兴趣的文章
《数据结构与算法:Python语言描述》一1.2 问题求解:交叉路口的红绿灯安排...
查看>>
《软技能:代码之外的生存指南》一一2.15 我可不能保证你能够一鸣惊人
查看>>
《Unity着色器和屏幕特效》——1.2 导入项目文件
查看>>
《自顶向下网络设计(第3版)》——2.1 可扩展性
查看>>
《Excel数据可视化:一样的数据不一样的图表》——3.3 用数据条的长度代表数值大小...
查看>>
《Android应用开发入门经典(第3版)》——第1.2节立即创建一个简单的应用
查看>>
[重磅]2015年中国银行电子商务产业链趋势报告(35PPT)
查看>>
《Python Cookbook(第3版)中文版》——6.3 解析简单的XML文档
查看>>
儿童节将至,你应该送IT宝宝们这些礼物!
查看>>
致命漏洞将允许攻击者绕过苹果的OTR签名验证并窃取iCloud钥匙串信息
查看>>
高可用系统常用解决手段浅述
查看>>
《Python和Pygame游戏开发指南》——1.9 特色的程序
查看>>
《深入理解Scala》——第2章,第2.3节优先选择不变性
查看>>
《应用程序性能测试的艺术(第2版)》—第1章 1.3节小结
查看>>
vmstat:一个标准的报告虚拟内存统计工具
查看>>
在 Ubuntu 中如何安装或升级 Linux 内核到4.2
查看>>
手机网站建设的过程是怎么样的?
查看>>
《MySQL DBA修炼之道》——1.3 查询执行过程概述
查看>>
Java核心技术卷I基础知识3.3.3 char类型
查看>>
《JavaScript设计模式》——9.5 Observer(观察者)模式
查看>>