python中字符串的常见操作(一)
如有字符串:
str1 = '192.168.1.1'
str2 = 'asdfghjk'
str3 = 'Asd fg hj ki'
str4 = ' '
str5 = ''
以下是常见操作:
# isalpha()判断字符串是否是字符
>>> res = str1.isalpha()
>>> print(res)
False
# isalnum()判断是否是数字或者字符组成
>>> res = str1.isalnum()
>>> print(res)
False
# isdigit()判断是否是整数
>>> res = str1.isdigit()
>>> print(res)
False
#rfind()从右往左找第一个对应的值,显示的是正向索引,如果没找到匹配的值返回-1
>>> res = str1.rfind('.',0,3)
>>> print(res)
-1
>>> res = str1.rfind('.')
>>> print(res)
9
# find()从左往右找第一个对应的值,显示的是正向索引,如果没找到匹配的值返回-1
>>> res = str1.find('.',0,3)
>>> print(res)
-1
>>> res = str1.find('.')
>>> print(res)
3
# index()从左往右找第一个对应的值,显示的是正向索引,如果没找到匹配的值报错
>>> res = str1.index('.')
>>> print(res)
3
>>> res = str1.index('.',0,4)
>>> print(res)
3
>>> res = str1.index('.',4,8)
>>> print(res)
7
>>> res = str1.index('12')
>>> print(res)
res = str1.index('12')
ValueError: substring not found
# count()显示字符个数,如果没有显示0
>>> res = str1.count('q')
>>> print(res)
0
>>> res = str1.count('1')
>>> print(res)
4
>>> res = str1.count('1',0,6)
>>> print(res)
2
>>> res = str1.count('16')
>>> print(res)
1
#把字符串变成抬头(每个单词的开头变成大写,数字不会报错)
>>> res = str1.title()
>>> print(res)
192.168.1.1
>>> res = str2.title()
>>> print(res)
Asdfghjk
>>> res = str3.title()
>>> print(res)
Asd Fg Hj Ki
#判断字符串当中开头字符是否为所选的字符
>>> res = str1.startswith('1')
>>> print(res)
True
>>> res = str2.startswith('A')
>>> print(res)
False
>>> res = str3.startswith('A')
>>> print(res)
True
#判断字符串当中结尾字符是否为所选的字符
>>> res = str3.endswith('ki')
>>> print(res)
True
>>> res = str3.endswith('j ki')
>>> print(res)
True
>>> res = str3.endswith('jki')
>>> print(res)
False
#isspace判断是否是由空格组成
>>> res = str3.isspace()
>>> print(res)
False
>>> res = str4.isspace()
>>> print(res)
True
>>> res = str5.isspace()
>>> print(res)
False
pycharm快捷键
# ctrl + d:复制一行
# ctrl + ?:快速注释一行|撤销
# tab键:缩进4个空格
# shift+tab键:回退4个空格
python中字符串的常见操作(一)的更多相关文章
- python中字符串的常见操作
demo:mystr = 'hello python' 1.find:mystr.find(str, start=0, end=len(mystr)),检测字符串中是否有要查询的字符,如果有返回开始的 ...
- 超详细!盘点Python中字符串的常用操作
在Python中字符串的表达方式有四种 一对单引号 一对双引号 一对三个单引号 一对三个双引号 a = 'abc' b= "abc" c = '''abc''' d = " ...
- python中字符串的常见操作方法
1. 字符串概念,字符串是一个容器,包含若干个字符并按照一定的顺序组织成一个整体.字符串支持索引操作. 2. 创建字符串基本语法 变量名 = "字符串信息" 变量名 = '字符串信 ...
- shell脚本中字符串的常见操作及"command not found"报错处理(附源码)
简介 昨天在通过shell脚本实现一个功能的时候,由于对shell处理字符串的方法有些不熟悉导致花了不少时间也犯了很多错误,因此将昨日的一些错误记录下来,避免以后再犯. 字符串的定义与赋值 # 定义S ...
- python中字符串(str)常用操作总结
# 字符串的常用操作方法 (都是形成新的字符串,与原字符串没有关系.) 1.字符串的基本操作之切片 s = 'python hello word' # 取首不取尾,取尾要+1 # 切片取出来的字符串与 ...
- 01-python中字符串的常见操作
(1)find 检测str是否包含在myStr中,如果存在则返回开始的索引值,否则返回-1. In [1]: myStr = "hello world tairan and tairanCi ...
- python中列表的常见操作
list1 = ['a','b','ca','d','e','a'] list2 = [1,5,7,9,5,4,3] info = {'name':'wang','age':32,'num':1258 ...
- 【Python从入门到精通】(九)Python中字符串的各种骚操作你已经烂熟于心了么?
您好,我是码农飞哥,感谢您阅读本文,欢迎一键三连哦. 本文将重点介绍Python字符串的各种常用方法,字符串是实际开发中经常用到的,所有熟练的掌握它的各种用法显得尤为重要. 干货满满,建议收藏,欢迎大 ...
- Python中字符串有哪些常用操作?纯干货超详细
随机推荐
- cobalt strike笔记-常用beacon扫盲
最近还是重新补一下cs的东西 0x01 Beacon命令 Beacon Commands =============== Command Description ------- ----------- ...
- MySQL 数据库的设计规范
网址 :http://blog.csdn.net/yjjm1990/article/details/7525811 1.文档的建立日期.所属的单位.2.数据库的命名规范.视图.3.命名的规范:1)避免 ...
- PMBOK(第六版) PMP笔记-质量审计、风险审计、采购审计的区分
质量审计.风险审计.采购审计的区分 三个概念的相同之处: 都是审计的概念 都是特定知识领域的审计 三个概念的区别: (1)三个概念虽然都是审计,但分布在不同的管理过程组. ·质量审计:执行过程组, ...
- c++11::initializer_list
#include <initializer_list> template <class T> class initializer_list; initializer_list对 ...
- python类方法@classmethod与@staticmethod
目录 python类方法@classmethod与@staticmethod 一.@classmethod 介绍 语法 举例 二.@staticmethod 介绍 语法 举例 python类方法@cl ...
- [洛谷P3709]大爷的字符串题
题目传送门 不用管它随机什么的,就用贪心的思想去想, 会发现这道题的实质是:求查询区间众数出现次数. 莫队即可解决. 注意字符集1e9,要离散化处理. #include <bits/stdc++ ...
- ThreadPoolExecutor使用方法
先看构造方法 ,ThreadPoolExecutor共4个构造方法: 直接看参数最多的7个参数分别代表: public ThreadPoolExecutor(int corePoolSize, int ...
- .NET Core 3.0 本地工具
.NET Core从最早期的版本就开始支持全局工具了.如果仅仅需要在某个项目中或某个文件夹中使用特定的工具,那么.NET Core 3.0就允许您这样做. 使用.NET Core 3.0,您可以在特定 ...
- git 拉取指定的远程分支(三种方式)
直接拉取 git clone -b ants git@github.com:Ants-double/CareerJava.git git clone -b 远程分支名 仓库地址 本地已经有相关的仓库代 ...
- Spring Boot - Logback配置日志要考虑哪些因素
Spring Boot - Logback配置日志 出于性能等原因,Logback 目前是springboot应用日志的标配: 当然有时候在生产环境中也会考虑和三方中间件采用统一处理方式.@pdai ...