python在字符串中查找字符

两类函数:

  1. find(),rfind()
  2. index(),rindex()

找到了都返回下标.

find找不到返回-1,index找不到抛出ValueError.

带r的表示从右向左找.

都可以使用第二个参数表示从哪个下标开始找.

a=‘abcdab‘
a.find(‘a‘)
Out[3]: 0
a.rfind(‘a‘)
Out[4]: 4
a.rfind(‘a‘,1)
Out[5]: 4
a.rfind(‘x‘)
Out[6]: -1
a.index(‘a‘)
Out[7]: 0
a.index(‘a‘,1)
Out[8]: 4
a.rindex(‘a‘)
Out[9]: 4
a.index(‘x‘)
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/IPython/core/interactiveshell.py", line 2882, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-10-51f0d5bb66b2>", line 1, in <module>
    a.index(‘x‘)
ValueError: substring not found

原文地址:https://www.cnblogs.com/zywscq/p/10569080.html

时间: 2024-07-30 05:39:50

python在字符串中查找字符的相关文章

字符串中查找字符

0:字符串中不包含字符c; >0 包含字符c. int str_search(char *cstr,char c) { int irs = 0; char *cp = cstr; zlog_debug(fzs,"cp=%s",cp); zlog_debug(fzs,"c=%c",c); while(*cp != 0) { if(*cp==c) { irs++; } cp++; } cp = NULL; return irs; }

c# 字符串 中查找字符,判断是否包含字符串

Eval("spr").ToString().IndexOf(HttpUtility.UrlDecode(Request.Cookies["UserName"].Value))>-1   //spr字段是否有当前用户名

*字符串-01. 在字符串中查找指定字符

1 /* 2 * Main.c 3 * D1-字符串-01. 在字符串中查找指定字符 4 * Created on: 2014年8月18日 5 * Author: Boomkeeper 6 *****部分通过****** 7 */ 8 9 #include <stdio.h> 10 11 int mysearch(char ch, const char str[], int length) { 12 13 int j, ret = -1; 14 15 for (j = 0; j < le

字符串-01. 在字符串中查找指定字符(15)

输入一个字符串S,再输入一个字符c,要求在字符串S中查找字符c.如果找不到则输出“Not found”:若找到则输出字符串S中从c开始的所有字符. 输入格式: 输入在第1行中给出一个不超过80个字符长度的.以回车结束的非空字符串:在第2行中给出一个字符. 输出格式: 在一行中按照题目要求输出结果. 输入样例1: It is a black box b 输出样例1: black box 输入样例2: It is a black box B 输出样例2: Not found 1 #include <

在字符串中查找指定字符

输入一个字符串S,再输入一个字符c,要求在字符串S中查找字符c.如果找不到则输出“Not found”:若找到则输出字符串S中从c开始的所有字符. 输入格式: 输入在第1行中给出一个不超过80个字符长度的.以回车结束的非空字符串:在第2行中给出一个字符. 输出格式: 在一行中按照题目要求输出结果. 输入样例1: It is a black box b 输出样例1: black box 输入样例2: It is a black box B 输出样例2: Not found #include<std

【C语言】模拟实现strchr函数.即在一个字符串中查找一个字符第一次出现的位置并返回

//模拟实现strchr函数.即在一个字符串中查找一个字符第一次出现的位置并返回 #include <stdio.h> //#include <string.h> #include <assert.h> char* my_strchr(char *dst, char src) { assert(dst); while (*dst != '\0') { if (*dst == src) return dst; dst++; } return 0; } int main()

10-1. 在字符串中查找指定字符(15)

输入一个字符串S,再输入一个字符c,要求在字符串S中查找字符c.如果找不到则输出“Not found”:若找到则输出字符串S中从c开始的所有字符. 输入格式: 输入在第1行中给出一个不超过80个字符长度的.以回车结束的非空字符串:在第2行中给出一个字符. 输出格式: 在一行中按照题目要求输出结果. 输入样例1: It is a black box b 输出样例1: black box 输入样例2: It is a black box B 输出样例2: Not found 1 #include <

【c语言】模拟实现strchr函数,功能:在一个字符串中查找一个字符第一次出现的位置,如果没有出现返回NULL

// 模拟实现strchr函数,功能:在一个字符串中查找一个字符第一次出现的位置,如果没有出现返回NULL #include <stdio.h> #include <assert.h> char const* my_strchr(char const *p,char c) { assert(p != NULL); while (*p) { if (*p == c) return p; else p++; } return NULL; } int main() { char *p =

js实现从字符串中查找出现次数最多的字符的两种解决办法

方法一:正则表达式匹配 1 var str = "adadfdfseffserfefsefseeffffftsdg"; 2 var maxLength = 0; var result = ""; 3 while (str != '') { 4 oldStr = str; 5 getStr = str.charAt(0); 6 str = str.replace(new RegExp(getStr, "g"), ""); 7 i