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

搜索字符串

Doraemon6年前 (2020-02-16)Python6196

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

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://www.myfpga.cn/index.php/post/131.html

分享给朋友:

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

列表实例

列表实例

随机生成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自动清理错误图片,深度学习训练数据集准备

使用python运行from PIL import Image from pathlib import Path import os   path = r'.'  ...

(原创)使用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...

(原创)使用Python自动对子文件夹中的图片文件进行重命名

(原创)使用Python自动对子文件夹中的图片文件进行重命名

为了解决Python深度学习的时候,经常遇到的文件名问题import os # 获取指定目录下的所有子文件夹 def get_subfolders(path):     subfolders = []...