python 字符串 查找 基本操作

个人博客首页(点击查看详情) -- https://blog.51cto.com/11495268
个人微信公众号(点击查看扫描关注) -- https://blog.51cto.com/11495268/2401194

1、简介

????字符串 相关操作 较多,本文 只简单描述下 python 字符串 查找 相关的基础操作
????

2、字符串 查找 内置函数

3、实例

????获取 指定 公司 的相关信息(公司信息 格式 都一致)

????

3.1 字符串 格式

## 公司名:排名:薪资-所占比例
HUAWEI:0:20K-30.8% ZTE:1:15K-50.6% SUNING:3:13K:39.9%

????

3.2 伪代码(思路)

查找 公司名 所在位置
从 公司名位置开始 查找 第一个 冒号 所在位置
从第一个 冒号 所在位置开始 查找 第二个 冒号 所在位置
从第二个 冒号 所在位置开始 查找 第一个 -号 所在位置
从第一个 -号 所在位置开始 查找 最近一个 空格 所在位置
    若 没有 找到 最近一个 空格位置,那么 字符串长度 代表 索要获取的位置(字符串结尾)

????

3.3 代码

#! /usr/bin/env python2.7
#-*- coding: utf-8-*.

str = ‘SUNING‘
string = ‘HUAWEI:0:20K-30.8% ZTE:1:15K-50.6% SUNING:3:13K-39.9%‘
index_company = string.find(str, 0, len(string))
index_first = string.find(‘:‘, index_company, len(string))
index_sec = string.find(‘:‘, index_first + 1, len(string))
index_line = string.find(‘-‘, index_sec + 1, len(string))
index_null = string.find(‘ ‘, index_line + 1, len(string))
if index_null == -1 :
    index_null = len(string)
# print ‘index_company:%d,index_first:%d,index_sec:%d,index_line:%d,index_null:%d\n‘ % (index_company,index_first,index_sec,index_line,index_null)

print ‘公司名:%s\t\n‘ % (string[(index_company):(index_first)])
print ‘公司排名:%s\t\n‘ % (string[(index_first + 1):(index_sec)])
print ‘公司平均薪资:%s\t\n‘ % (string[(index_sec + 1):(index_line)])
print ‘公司平均薪资所占百分比:%s\t\n‘ % (string[(index_line + 1):index_null])

????

3.4 执行结果

# python str_find_wl.py
index_company:35,index_first:41,index_sec:43,index_line:47,index_null:53

公司名:SUNING  

公司排名:3  

公司平均薪资:13K  

公司平均薪资所占百分比:39.9%

原文地址:https://blog.51cto.com/11495268/2413492

时间: 2024-08-27 22:30:15

python 字符串 查找 基本操作的相关文章

python字符串搜索

python字符串字串查找 find和index方法 更多0 python 字符串 python 字符串查找有4个方法,1 find,2 index方法,3 rfind方法,4 rindex方法. 1 find()方法:查找子字符串,若找到返回从0开始的下标值,若找不到返回-1 info = 'abca'print info.find('a')##从下标0开始,查找在字符串里第一个出现的子串,返回结果:0 info = 'abca'print info.find('a',1)##从下标1开始,查

python字符串常用操作

今天把之前遗漏的内容补上,关于python字符串的基本操作 name="my \tname is {name},and I am {year} old"print(name)print(name.capitalize())print(name.count("n"))#查看字符数量print(name.center(50,"-"))#填充print(name.endswith("n"))#判断末尾字符print(name.exp

Python中字符串查找效率比较

Python中字符串查找方式有多种,常见的有re.match/search or str.find 用一个例子来说明各种方式的效率如下: from timeit import timeit import re def find(string, text): if string.find(text) > -1: pass def re_find(string, text): if re.match(text, string): pass def best_find(string, text): i

python字符串(大小写、判断、查找、分割、拼接、裁剪、替换、格式化)

一.通用操作 1.Python len() 方法返回对象(字符.列表.元组等)长度或项目个数. 语法 len()方法语法: len( q ) 参数 q -- 对象. 返回值 返回对象长度. 实例 以下实例展示了 len() 的使用方法: >>>str = "runoob" >>> len(str) # 字符串长度 6 >>> l = [1,2,3,4,5] >>> len(l) # 列表元素个数 5 2.pytho

lintcode 容易题:strStr 字符串查找

题目: 字符串查找 字符串查找(又称查找子字符串),是字符串操作中一个很有用的函数.你的任务是实现这个函数. 对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出现的第一个位置(从0开始). 如果不存在,则返回 -1. 样例 如果 source = "source" 和 target = "target",返回 -1. 如果 source = "abcdabcdefg" 和

Python 字符串操作方法大全

python字符串操作实方法大合集,包括了几乎所有常用的python字符串操作,如字符串的替换.删除.截取.复制.连接.比较.查找.分割等,需要的朋友可以参考下. 1.去空格及特殊符号 s.strip().lstrip().rstrip(',') 2.复制字符串 #strcpy(sStr1,sStr2) sStr1 = 'strcpy' sStr2 = sStr1 sStr1 = 'strcpy2' print sStr2 3.连接字符串 #strcat(sStr1,sStr2) sStr1 =

【代码学习】PYTHON字符串的常见操作

一.字符串运算符 下表实例变量 a 值为字符串 "Hello",b 变量值为 "Python": 操作符 描述 实例 + 字符串连接 >>>a + b 'HelloPython' * 重复输出字符串 >>>a * 2 'HelloHello' [] 通过索引获取字符串中字符 >>>a[1] 'e' [ : ] 截取字符串中的一部分 >>>a[1:4] 'ell' in 成员运算符 - 如果字符串

【转】Python 字符串操作方法大全

python字符串操作实方法大合集,包括了几乎所有常用的python字符串操作,如字符串的替换.删除.截取.复制.连接.比较.查找.分割等,需要的朋友可以参考下 1.去空格及特殊符号 复制代码代码如下: s.strip().lstrip().rstrip(',') 2.复制字符串 复制代码代码如下: #strcpy(sStr1,sStr2)sStr1 = 'strcpy'sStr2 = sStr1sStr1 = 'strcpy2'print sStr2 3.连接字符串 复制代码代码如下: #st

python字符串的操作方法有哪些

python 字符串str是在Python开发http://www.maiziedu.com/course/python/过程中,最常见的一种基本数据类型.字符串是许多单个子串组成的序列,其主要是用来表示文本.字符串是不可变数据类型,也就是说你要改变原字符串内的元素,只能是新建另一个字符串.虽然这样,但python中的字符串还是有许多很实用的操作方法. 文章中我们会为大家介绍一些简单的字符串操作方法,如字符串创建.提取字符串子串的值.字符串的修改和删除操作等. Python字符串基本操作方法 创