Python字符串内建函数
Python字符串内建函数
1、find函数:检测字符串是否包括子字符串
str.find(sub, [start,end])
示例代码:
str_example = "hello world I am jinjiaodawang"
print(str_example.find("hello")) #打印hello第一次出现的下标
print(str_example.find("lll")) #如果没有出现过 则打印-1
打印结果:
0
1
2、index函数:检测字符串是否包括子字符串
str.index (sub, [start,end])
示例代码:
str_example = "hello world I am jinjiaodawang"
print(str_example.index("hello")) #打印hello第一次出现的下标
print(str_example.index("no")) #如果字符串中没有 报异常
打印结果:
0
报异常
3、count函数∶统计字符串中某个字符的个数
str.count(sub, [start,end])
示例代码:
str_example = "hello world I am jinjiaodawang"
print(str_example.count("o"))
打印结果:
3
4、replace函数:将旧字符串替换为新字符串
str.replace(old, new,[count])
示例代码:
str_example = "hello world I am jinjiaodawang"
print(str_example.replace("hello","nohello"))
打印结果:
nohello world I am jinjiaodawang
5、splite函数︰通过指定分隔符对字符串进行切片
str.split(str="", num=string.count(str))
示例代码:
str_example = "hello world I am jinjiaodawang"
print(str_example.split(" ",2)) #以空格为分隔符 实行两次
打印结果:
['hello', 'world', 'I am jinjiaodawang']
6、capitalize :第一个字符大写,其他字符小写
str.capitalize()
示例代码:
str_example = "hello world I am jinjiaodawang"
print(str_example.capitalize())
打印结果:
Hello world i am jinjiaodawang
7、title :所有单词首字母大写,其余字母小写
str.title()
示例代码:
str_example = "hello world I am jinjiaodawang"
print(str_example.title())
打印结果:
Hello World I Am Jinjiaodawang
8、startswith :检查字符串是否以制定子串开头
str.startswith(str, beg=0,end=len(string))
示例代码:
str_example = "hello world I am jinjiaodawang"
print(str_example.startswith("hello"))
print(str_example.startswith("lloheoo"))
打印结果:
True
False
9、endswith :检查字符串是否以制定子串结尾
str.endswith(str, beg=0,end=len(string))
示例代码:
str_example = "hello world I am jinjiaodawang"
print(str_example.endswith("wang"))
print(str_example.endswith("llo"))
打印结果:
True
False
10、upper:将小写字母转为大写字母
str.upper()
示例代码:
str_example = "hello world I am jinjiaodawang"
print(str_example.upper())
打印结果:
HELLO WORLD I AM JINJIAODAWANG
11、lower :将小写字母转为大写字母
str.lower()
示例代码:
str_example = "HELLO world I am jinjiaodawang"
print(str_example.lower())
打印结果:
hello world i am jinjiaodawang
12、ljust:左对齐,使用空格填充至指定长度的新字符串
str.ljust(width,[fillchar])
示例代码:
str_example = "HELLO world I am jinjiaodawang"
print(str_example.ljust(50,"&"))
打印结果:
HELLO world I am jinjiaodawang&&&&&&&&&&&&&&&&&&&&
13、rjust :右对齐,使用空格填充至指定长度的新字符串
str.rjust(width,[fillchar])
示例代码:
str_example = "HELLO world I am jinjiaodawang"
print(str_example.rjust(50,"&"))
打印结果:
&&&&&&&&&&&&&&&&&&&&HELLO world I am jinjiaodawang
14、center :返回一个指定的宽度width居中的字符串
str.center(width,[fillchar])
示例代码:
str_example = "hello world I am jinjiaodawang"
print(str_example.center(50,"-"))
打印结果:
----------HELLO world I am jinjiaodawang----------
15、lstrip:截掉字符串左边的空格或指定字符;
rstrip:截掉字符串右边的空格或指定字符;
strip:截掉字符串左右两边的空格或指定字符
str.lstrip([chars])
str.rstrip([chars])
str.strip([chars])
示例代码:
str_example = "++hello world I am jinjiaodawang++"
print(str_example.lstrip("+"))
print(str_example.rstrip("+"))
print(str_example.strip("+"))
打印结果:
hello world I am jinjiaodawang++
++hello world I am jinjiaodawang
hello world I am jinjiaodawang
Python字符串内建函数相关推荐
- python字符串内建函数_Python的字符串内建函数(字符串处理)
Python的字符串内建函数 这些方法实现了string模块的大部分方法 , 如下表硕士列出了目前字符串内建支持的方法 string = 'XXX' string.capitalize() # 把字符 ...
- python字符串内建函数详解
概述 字符串方法是从python1.6到2.0慢慢加进来的--它们也被加到了Jython中. 这些方法实现了string模块的大部分方法,如下表所示列出了目前字符串内建支持的方法,所有的方法都包含了对 ...
- python字符串内建函数_python字符串内建函数
操作符描述实例 + 字符串连接 a + b 输出结果: HelloPython * 重复输出字符串 a*2 输出结果:HelloHello [] 通过索引获取字符串中字符 a[1] 输出结果 e [ ...
- python字符串title函数_python字符串内建函数-capitalize、title、upper
python字符串内建函数-capitalize.title.upper 6.capitalize 函数功能:该函数用于将字符串的第一个字母变成大写,其他字母变成小写. 返回值:该函数的返回值是一个首 ...
- python 字符串操作和内建函数
文章目录 访问字符串值 字符串连接 Python 转义字符 python 字符串运算符 python 字符串格式化 Unicode 字符串 字符串内建函数 访问字符串值 var1 = 'Hello W ...
- [转载] python 字符串包含某个字符_python字符串
参考链接: Python字符串capitalize() str字符串 本节内容概览 1.何为str?2.转义字符3.字符串格式化4.Python字符串内建函数和操作5.python字符串练习 一.字符 ...
- python字符串处理方法与函数有什么区别_傻傻分不清系列 | Python中各种字符串处理方法...
Python易混淆知识系列:Pandas字符串方法和字符串内建函数,使用Python的一个优势就是字符串处理起来比较容易. Python的初学者在学习字符串内建函数的时候往往会很困惑:字符串的内建函数 ...
- python 基础教程:字符串内建函数之大小写的区别
假设有一个字符串s = "hello,world",如何对字符串进行大小写的更改? 1,将字符串的首字母改为大写: msg = s.capitalize() print(msg) ...
- python的字符串内建函数
python的字符串内建函数 字符串方法是从python1.6到2.0慢慢加进来的--它们也被加到了Jython中. 这些方法实现了string模块的大部分方法,如下表所示列出了目前字符串内建支持的方 ...
- python内建函数istitle_Python 的字符串内建函数
print("----------------------------------first one---------------------------") print(&quo ...
最新文章
- 日志——Vue.js开发在线简历生成器
- gsonformat插件_IntelliJ IDEA18个常用插件,动图演示,让你效率翻倍!
- 求1-100之间的所有素数
- android 蓝牙 setscanmode,蓝牙LE扫描在后台无法在Android M上运行
- 应用DOM操作文档的一个实用例子
- [六省联考2017]组合数问题
- 发送附件时,防止文件名中的中文字符变成乱码
- python下路径问题及模型存储
- 20190913:(leetcode习题)罗马数字转整数
- java动态数组储存敌机_如何使用参数通过graphql将动态数组字符串存储为neo4j中的节点属性?...
- Atitit 解析m4a文件的元数据标签音乐名,歌手 专辑 年代等信息 java版本 目录 1.1. 自己解析mp4 m4a结构	1 1.2. 格式返回	1 1.3. /bookmarksHtmlE
- Vue项目-2首页开发(header)
- 汇编实现吃豆子小程序
- 戴尔台式计算机usb驱动,dell服务器和电脑不支持usb2.0设备安装系统的解决方案方法...
- Boosting Crowd Counting via Multifaceted Attention
- 滇池学院计算机基础,云南大学滇池学院网络服务系统 云南大学滇池学院
- Hadoop学习----Hadoop介绍
- 阿里云天池大赛赛题解析——机器学习篇
- webpack 报错Cannot find module 'opn'
- 字节跳动面试必问:大厂程序员35岁后的职业出路在哪?太香了
热门文章
- c语言自动贩卖机找钱,c语言趣题之“找钱的方法数量 ”
- 老李分享:六度分隔理论 1
- MySQL5.7 配置优化
- 牛腩——SQLhelper
- html5 自动刷新,javascript – 每5分钟自动刷新一次
- led灯光衰怎么解决_影响LED灯具光衰的原因及解决方法
- STM32F4+W25Q64实现一个U盘
- 解决.Net Framework 在计算机上已安装了更高的 4.x 版本
- 计算机硬件的主要性能指标包括,计算机硬件系统有哪些主要的性能指标
- 数据库设计中面临的主要困难和问题的总结