关于python的字符串操作
str = "fahaf asdkfja\t \r \n fjdhal 3453"
print(str.isspace()) # 如果str中只包含空格,则返回True
print(str.isalnum()) # 如果str至少有一个字符并且所有字符都是数字或者字母则返回True
print(str.isalpha()) # 如果str至少有一个字符并且所有字符都是字母则返回True
print(str.isdecimal()) # 如果string只包含数字则返回True, 全角数字
print(str.isdigit()) # 如果string只包含数字则返回True,全角数字、(1)、\u00b2--->(unicode)
print(str.isnumeric()) # 如果string只包含数字则返回True,全角数字,汉字数字
print(str.istitle()) # 如果string是标题化的(每个单词的首字母大写)则返回True
print(str.islower()) # 如果string中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小写,则返回True
print(str.isupper()) # 如果string中包含至少一个区分大小写的字符,并且所有这些(區分大小写的)字符都是大写,则返回True

字符串的查找的替换操作:
str = "hello hello"
print(str.startswith("hello")) # 检查字符串是否是以hello开头,是则返回True
print(str.endswith("hello")) # 检查字符串是否是以hello结束,是则返回True
print(str.find("lo")) # 检测lo是否包含在str中,如果start和end指定范围,则检查是否包含在指定范围内,如果是返回开始的索引值,否则返回 -1
print(str.rfind("lo")) #类似于find()方法,不过是从右边开始查找
print(str.index("lo")) #跟find()方法类似,只不过如果lo不在str会报错
print(str.rindex("lo")) # 类似于index()方法,不过是从右边开始查找
print(str.replace("hello", "HELLO", 1)) # 将hello替换为HELLO,替换次数为1次

大小写转换:
# 大小写转换
str = "hello pyTHon"
print(str.capitalize()) # 把字符串的第一个字母大写
print(str.title()) # 首字母大写
print(str.lower()) # 将所有大写转换为小写
print(str.upper()) # 将所有小写转换为大写
print(str.swapcase()) # 将大小写翻转

# 文本对齐
poem = ["静夜思",
"李白",
"床前明月光",
"疑似地上霜",
"举头望明月",
"低头思故乡"]
for i in poem: # 左对齐,返回一个填充10个单位的全角空格字符串
print("|%s|" % i.ljust(10), " ")
print() for i in poem: # 右对齐,返回一个填充10个单位的全角空格字符串
print("|%s|" % i.rjust(10), " ")
print() for i in poem: # 居中,返回一个填充10个单位的全角空格字符串
print("|%s|" % i.center(10, " "))

去除空白:
# 去除空白
poem = ["静夜思",
"李白",
"\t\n床前明月光",
"疑似地上霜\t",
"\t举头望明月\t\n",
"低头思故乡"] for i in poem: # 去除左侧空白字符
print("|%s|" % i.lstrip())
print("*" * 10) for i in poem: # 去除右侧空白字符
print("|%s|" % i.rstrip())
print("*" * 10) for i in poem: # 去除两侧空白字符
print("|%s|" % i.strip())

# 拆分和连接
str = "how are you"
print(str.partition(" ")) # 把字符串分成一个3元素的元组
print(str.rpartition(" ")) # 类似partition()方法,不过是从右边开始
print(str.split()) # 以空白字符(\r \t \n 空格)为分隔符拆分字符串
print(str.splitlines()) # 以 行(\r \n \r\n)为分隔符拆分字符串
print(" ".join(str)) # 以" "(空格)作为分隔符,将strongoing所有元素合并为一个新的字符串

关于python的字符串操作的更多相关文章
- python中字符串操作--截取,查找,替换
python中,对字符串的操作是最常见的,python对字符串操作有自己特殊的处理方式. 字符串的截取 python中对于字符串的索引是比较特别的,来感受一下: s = '123456789' #截取 ...
- Python中字符串操作
#Python字符串操作 '''1.复制字符串''' #strcpy(sStr1,sStr2) sStr1 = 'strcpy' sStr2 = sStr1 sStr1 = 'strcpy2' pri ...
- Python之字符串操作
一.字符串特点 内容不可修改 password=' #内容不可修改 二.字符串常用方法 1..strip()方法 去字符串两边的空格和换行符 print(password.strip()) #去掉字符 ...
- python基础--字符串操作、列表、元组、文件操作
一.变量及条件判断 1.字符串.布尔类型.float.int类型,None都是不可变变量 2.字符串是不可变变量,不可变变量就是指定义之后不能修改它的值 3.count +=1和count=count ...
- Python中字符串操作函数string.split('str1')和string.join(ls)
Python中的字符串操作函数split 和 join能够实现字符串和列表之间的简单转换, 使用 .split()可以将字符串中特定部分以多个字符的形式,存储成列表 def split(self, * ...
- 「Python」字符串操作内置函数
目录: capitalize casefold center count encode decode endswith expandtabs find format format_map index ...
- Python:字符串操作总结
所有标准的序列操作(索引.分片.乘法.判断成员资格.求长度.取最小值最大值)对字符串同样适用,且字符串是不可变的. 一.字符串格式化 转换说明符 [注]: 这些项的顺序至关重要 (1)%字符:标记转换 ...
- python常用字符串操作
#!/usr/bin/env python name='cunzhang' print(name.capitalize())#首字母大写 print(name.count('n'))#统计字符有几个 ...
- Python的字符串操作和Unicode
字符串类型 str:Unicode字符串.采用''或者r''构造的字符串均为str,单引号可以用双引号或者三引号来代替.无论用哪种方式进行制定,在Python内部存储时没有区别. bytes:二进制字 ...
随机推荐
- Apache Flink 进阶入门(二):Time 深度解析
前言 Flink 的 API 大体上可以划分为三个层次:处于最底层的 ProcessFunction.中间一层的 DataStream API 和最上层的 SQL/Table API,这三层中的每一层 ...
- 基于 RocketMQ 的同城双活架构在美菜网的挑战与实践
本文整理自李样兵在北京站 RocketMQ meetup分享美菜网使用 RocketMQ 过程中的一些心得和经验,偏重于实践. 嘉宾李样兵,现就职于美菜网基础服务平台组,负责 MQ ,配置中心和任务调 ...
- vue爬坑之input组件
本篇写给第一次用VUE写输入框组件的朋友们 正常情况我们vue2.0是怎么样取到input框的值的呢? 很简单只需要给input框设置v-model="val" 我们就能从data ...
- SpringBoot学习笔记(四):SpringBoot集成Mybatis-Plus+代码生成
简介 官网:http://baomidou.oschina.io/mybatis-plus-doc/ 平时业务代码不复杂的时候我们写什么代码写的最多,就是我们的SQL语句啊,配置那么多的Mapper. ...
- HDU 1069 Monkey and Banana (动态规划)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1069 简单记录一下 思路:把长方体的各种摆法都存到数组里面,然后按照长宽排序,再dp即可 转移方程 d ...
- 在当前目录打开DOS命令行窗口
[step1]选中文件夹 [step2]shift + 鼠标右键
- js中控制流管理的四种方法
引自http://es6.ruanyifeng.com/#docs/generator#yield--表达式 1.常用的回调方法 step1(function (value1) { step2(val ...
- C++字符串前面加LR
const std::experimental::filesystem::path symbolsFilename = LR"(d:\fulongtech_git\draing_recogn ...
- EnumProcess 实现枚举进程
BOOL WINAPI EnumProcesses ( _Out_writes_bytes_(cb) DWORD * lpidProcess, _In_ DWORD cb, _Out_ LPDWORD ...
- deepfake安装python常用命令
pip install -r requirements.txt -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/ python -m p ...