关于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:二进制字 ...
随机推荐
- order方法属于模型的连贯操作方法之一
order方法属于模型的连贯操作方法之一,用于对操作的结果排序. 用法如下: $Model->where('status=1')->order('id desc')->limit(5 ...
- 洛谷 P1941 飞扬的小鸟 (NOIP 2014)
题面 题解 背包细节题,wa了一片,上升的过程我们可以看做一个完全背包(多重背包好像跑不过去),下降 过程是一个0/1背包,为了避免冲突应该先跑多重,先跑0/1就有可能产生这个点又上升又下降的 非法情 ...
- htaccess apache重定向学习
1.推荐博客:http://www.cnblogs.com/adforce/archive/2012/11/23/2784664.html 2.测试工具:https://htaccess.madewi ...
- Tree and Permutation (HDU 6446) 题解
// 昨天打了一场网络赛,表现特别不好,当然题目难度确实影响了发挥,但还是说明自己太菜了,以后还要多多刷题. 2018 CCPC 网络赛 I - Tree and Permutation 简单说明一下 ...
- PAT甲级——A1094 The Largest Generation
A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level bel ...
- 用python获取ip信息
1.138网站 http://user.ip138.com/ip/首次注册后赠送1000次请求,API接口请求格式如下,必须要有token值 import httplib2 from urllib.p ...
- Python爬虫笔记【一】模拟用户访问之设置请求头 (1)
学习的课本为<python网络数据采集>,大部分代码来此此书. 网络爬虫爬取数据首先就是要有爬取的权限,没有爬取的权限再好的代码也不能运行.所以首先要伪装自己的爬虫,让爬虫不像爬虫而是像人 ...
- 启动zuul时候报错:The bean 'proxyRequestHelper', defined in class path resource [org/springframework/cloud/netflix/zuul
启动zuul时候报错:The bean 'proxyRequestHelper', defined in class path resource [org/springframework/cloud/ ...
- Ajax请求Session超时解决
web前端js代码: $.ajaxSetup({ contentType : "application/x-www-form-urlencoded;charset=utf-8", ...
- 如何查看PostgreSQL正在执行的SQL
SELECT procpid, start, now() - start AS lap, current_query FROM (SELECT ...