当前位置:首页 > Software > Python > 正文内容

搜索字符串

Doraemon6年前 (2020-02-16)Python6631

常用搜索字符串中子串的方法

str.count(substring)      返回str中substring子串出现的无覆盖的次数

str.find(s1)                    返回s1在这个字符串的最低下标,如果字符串中不存在s1,则返回-1

str.rfind(s1)                   返回s1在这个字符串的最下标,如果字符串中不存在s1,则返回-1

str.startswith(s1)            如果 字符串是以字符串s1开始,返回True

str.endswith(s1)            如果字符串是以字符串s1结尾,返回True

                                          >>>str="hello world"

                                           >>>str.endswith("world")

循环实例: 统计并输出用户输入的字符串中数字、大写字母、小写字母、以及其他字符的个数。

a=b=c=d=0
str=input("Please input a string:")
for ch in str:            #遍历输入的字符串
   if '0'<=ch<='9':
       a=a+1
   elif'A'<=ch<='Z':
       b=b+1
   elif'a'<=ch<='Z':
       c=c+1
   else:
       d=d+1
print("数字、大写字母、小写字母以及其他字符的个数是%d、%d、%d、%d\n"%(a,b,c,d))

 

扫描二维码推送至手机访问。

版权声明:本文由我的FPGA发布,如需转载请注明出处。

本文链接:https://world.myfpga.cn/index.php/post/131.html

分享给朋友:

“搜索字符串” 的相关文章

for循环

for循环

range()函数range(start,end,step)range()函数返回一个可迭代对象(可理解为一个序列,序列中的数包括start,不包括end)例如range(1,101),返回1-100的序列。range(101),范围0-100的序列。range(1,100,2),返回1,3,5.....

列表实例

列表实例

随机生成100个小写字母存入一个列表中,统计26个字母的出现次数。import random def getRandomLetter():     code_a=ord('a')     code_z=ord('z')     x=random.randint(code_a,code_z)...

anaconda打不开的解决方法

anaconda打不开的解决方法

报错内容Navigator Error An unexpected error occurred on Navigator start-up Report Please report this ...

(原创)使用Python对任意网站图片进行爬取,仅用于学习

(原创)使用Python对任意网站图片进行爬取,仅用于学习

import os import time import argparse import requests import re import io from urllib.parse import ...

(原创)使用Python递归获取网页内的所有URL,并进行清洗

(原创)使用Python递归获取网页内的所有URL,并进行清洗

import argparse import time from urllib.parse import urljoin, urlparse from selenium import webdriver...