Python字符串操作大全
时间:2022-07-28
本文章向大家介绍Python字符串操作大全,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
Python中字符串的定义:由Unicode码点组成的不可变序列(Strings are immutable sequences of Unicode code points)。
Python内置函数提供了强大的字符串的使用方法,熟练掌握常见的方法,对于数据处理、面试、笔试都非常有用。
dir(str)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__',
'__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'cent
er', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isuppe
r', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfi
ll']
首先,字符串是一种序列,所以支持切片和索引操作,具体可以看这篇文章Python基础之数据类型详解。
下面是常见的内建函数用法总结:
s="hello testers !"
print(s.replace('hello','hi')) #输出hi testers !
s="hello testers !"
print(s.split()) #输出['hello', 'testers', '!']
print(s.partition('testers')) #输出('hello ', 'testers', ' !')
s=" hellotesters! "
print(s.strip()) #输出hellotesters!
print(s.rstrip()) #输出 hellotesters!
print(s.lstrip()) #输出hellotesters!
s1="hellotesters!"
s2="HELLO TESTERS!"
s3="Hello Testers!"
s4=s1="hello testers!"
print(s1.upper()) #输出HELLO TESTERS!
print(s2.lower()) #输出hello testers!
print(s3.swapcase()) #输出hELLO tESTERS!
print(s1.capitalize()) #输出Hello testers!
print(s4.title()) #输出Hello Testers!
s1="hello testers!"
print(s1.count('l')) #输出2
print(s1.find('hello')) #输出0
print(s1.rfind('testers!')) #输出6
s1="hellotesters!1234567890"
s2="hellotesters"
s3="1234567890"
s4="HELLOTESTERS"
print(s1.isalnum()) #输出False
print(s2.isalpha()) #输出True
print(s3.isdigit()) #输出True
print(s4.isupper()) #输出True
print(s2.islower()) #输出True
- 用R解析Mahout用户推荐协同过滤算法(UserCF)
- 操作符与数据类型转换
- 超级强大的vim配置(vimplus)
- 美团App插件化实践
- 函数式编程在Redux/React中的应用
- 关于刘海打理这种事儿,美团点评的iOS工程师早就有经验了,不信你看!
- 这个Spring高危漏洞,你修补了吗?
- 详析JSONP跨域
- Android Binder漏洞挖掘技术与案例分享
- Stanford机器学习笔记-5.神经网络Neural Networks (part two)
- 监控平台前端SDK开发实践
- 一步步实现静态页面布局
- Stanford机器学习笔记-3.Bayesian statistics and Regularization
- 在R中使用支持向量机(SVM)进行数据挖掘
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 《Algorithms Unlocked》读书笔记2——二分查找和排序算法
- 《Algorithms Unlocked》读书笔记3——计数排序
- vue-element-admin
- 二叉树的递归算法
- mongoDB基本操作
- 一个 Vue + Node + MongoDB 博客系统
- promise 和 async 的用法
- 解决 iPhone 微信 H5 无法自动播放音乐问题
- Sequelize 基本操作
- Java面试高频问题汇总 线程池专题
- Node.js 使用 RSA 做加密
- 面试官:说说Ribbon是如何实现负载均衡的?
- 用node.js写一个jenkins发版脚本
- Java面试高频面试题汇总 锁专题
- Java面试高频面试题汇总 I/O专题