string是很基础的数据结构,来看看string有哪些常用的操作。

#!/bin/python
#!---encoding: UTF- s = 'abcdefg' #concat
s1 = s + "hi"
print(s1) #find string pos
#如果成功则返回对应index 如果查找失败则报异常
print(s.index("abc"))
#print(s.index("ccc")) #ValueError: substring not found #slice
print(s[:])
print(type(s), dir(str))

通过dir来查看当前类的所有函数(包括私有函数,也包括公开函数)

<class 'str'> ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

常用的有:join,count,encode,find,format,index,lower,upper,lstrip,rstrip,strip,replace,split,title

操作1:lower,upper

将字符串全部变成大/小写

a = 'abcdef'
b = a.upper() #ABCDEF
c = b.lower() #abcdef

操作2:lstrip,rstrip,strip

strip:将string中左右出现的空白字符删除掉,并且如果是连续的空白字符,也一并删除,知道删除到不是空白字符为止,空白字符包含:\n, \r, 空格

lstrip:将string中左边出现的连续空白字符删除掉,右侧的不管

rstrip:将string中右侧出现的连续空白字符删除掉,左侧的不管

a = "\nabc\n"
print(a.strip()) #abc
print(a.rstrip()) #\nabc
print(a.lstrip()) #abc\n

操作3:replace

replace:将匹配的字符串替换为第二个参数中提供的字符串,注意replace不会改变原先的值,改变后的值需要用新的变量来接收

#replace
a = 'a b c'
b = a.replace(' ', '-') #a-b-c
print(a, b)

操作4:title

title:将字符串变成首字符大写,其它字符小写的格式,像文章段落的第一个单词那样。

#title
a = 'aBc'
print(a.title()) #Abc

操作5:count,find,index

#count, find, index
a = 'abcabc'
print(a.count('abc')) #2
print(a.count('abcd')) #0
print(a.index('ab')) #0
#print(a.index('abcd')) #ValueError: substring not found
print(a.find('abc') #0
print(a.find('abcd')) #-1

count:统计字符串出现的次数

find:查找该字符串是否在母字符串中出现过,出现过则返回对应的下标,否则返回-1;

index:查找该字符串是否在木字符串中出现过,出现过则返回第一次出现的下标,否则抛出异常;

操作6:join

a = 'abc'
print(a.join('---')) #-abc-abc-

操作7:format,encode

format:对象规定格式,参数来负责填充格式中的缺少的

encode:字符串转码函数,参数是转码后的格式

#format, encode
print('{2},{1},{0}'.format('a', 'b', 'c')) #c,b,a
print('abcd'.encode('UTF-8')) #b'abcd'

操作8:split

split:将按照参数中的字符将对象分割开来,返回list数组

#split
a = 'a,b,c'
print(a.split(',')) #['a', 'b', 'c']

还有两个常用的操作:连接,slice

s1 = 'abcd'
s2 = 'efgh'
print(s1 + s2) #abcdefgh
print(s1[1:]) #bcd

21 pythone【入门指南】:string的更多相关文章

  1. CI Weekly #21 | iOS 持续集成快速入门指南

    搭建 iOS 持续集成环境要多久?每个 iOSer 都有不同的答案.这次我们整理了 flow.ci 的 iOS 持续集成的相关文档和最佳实践,希望帮你更快地完成构建.更新文档见: flow.ci iO ...

  2. 关于 HSSF 和 XSSF 功能的开发者入门指南 (Apache POI 操作 Excel)

    关于 HSSF 和 XSSF 功能的开发者入门指南 笔者深夜无眠,特此对本文翻译一部分,未完成部分待后续更新 本文源文地址 意欲使用 HSSF 和 XSSF 功能快熟读写电子表格?那本文就是为你而写的 ...

  3. Ext JS 6学习文档–第1章–ExtJS入门指南

    Ext JS 入门指南 前言 本来我是打算自己写一个系列的 ExtJS 6 学习笔记的,因为 ExtJS 6 目前的中文学习资料还很少.google 搜索资料时找到了一本国外牛人写的关于 ExtJS ...

  4. [转] Spark快速入门指南 – Spark安装与基础使用

    [From] https://blog.csdn.net/w405722907/article/details/77943331 Spark快速入门指南 – Spark安装与基础使用 2017年09月 ...

  5. redis入门指南(二)—— 数据操作相关命令

    写在前面 以下绝大部分内容取材于<redis入门指南>,部分结合个人知识,实践后得出. 只记录重要,明确,属于新知的相关内容,杜绝冗余和重复. 字符串 1.字符串类型是redis中最常见的 ...

  6. Web API 入门指南 - 闲话安全

    Web API入门指南有些朋友回复问了些安全方面的问题,安全方面可以写的东西实在太多了,这里尽量围绕着Web API的安全性来展开,介绍一些安全的基本概念,常见安全隐患.相关的防御技巧以及Web AP ...

  7. 【翻译】Fluent NHibernate介绍和入门指南

    英文原文地址:https://github.com/jagregory/fluent-nhibernate/wiki/Getting-started 翻译原文地址:http://www.cnblogs ...

  8. ASP.NET MVC 5 入门指南汇总

    经过前一段时间的翻译和编辑,我们陆续发出12篇ASP.NET MVC 5的入门文章.其中大部分翻译自ASP.NET MVC 5 官方教程,由于本系列文章言简意赅,篇幅适中,从一个web网站示例开始讲解 ...

  9. 【HBase】HBase Getting Started(HBase 入门指南)

    入门指南 1. 简介 Quickstart 会让你启动和运行一个单节点单机HBase. 2. 快速启动 – 单点HBase 这部分描述单节点单机HBase的配置.一个单例拥有所有的HBase守护线程- ...

  10. 001.Getting Started -- 【入门指南】

    Getting Started 入门指南 662 of 756 people found this helpful Meng.Net 自译 1. Install .NET Core 到官网安装 .NE ...

随机推荐

  1. day38-常见第三方模块

    1.requests模块 2.psutil模块 3.xlrd模块 4.xlwt模块 5.Paramiko模块

  2. js event

    event jquery获取: 在jquery中调用函数中最多只能有event这一个参数,所有的值在event.data中可以获取. $('select').click(function(event) ...

  3. git 和 github 学习总结

    https://mp.weixin.qq.com/s?src=11&timestamp=1543302553&ver=1269&signature=NAX65qusuVVDEl ...

  4. Django--URL(路由层)

    一.django 静态文件配置 在配置文件中settings.py STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR ...

  5. leetcode 错误题解,反面教材 Gas Station

    class Solution { public: int canCompleteCircuit(vector<int>& gas, vector<int>& c ...

  6. js 提示框的实现---组件开发之(一)

    自己做了一个简单的提示框,供自己使用,也可以供他人参考,看懂此文,是理解组件开发的入门 思路比较简单: 1.常规写法: 1.1. 创建一个构造函数 1.2. 给构造函数的原型对象添加show 和hid ...

  7. vue项目动态控制数据变动时箭头样式

    html代码 <div class="top_precent"> <span :class="{arrow:numPrecent<0}" ...

  8. 运行vue项目--安装vue脚手架vue cli

    第一步. 安装node: 官网下载node的.pkg,下载地址,选择相应版本进行下载 mac终端下输入npm -v 和 node -v, 出现相应版本号即安装成功. 若均提示 command not ...

  9. 抢红包js程序

    https://www.cnblogs.com/miid/p/5192235.html <!DOCTYPE html> <html> <head> <meta ...

  10. TensorFlow saved_model 模块

    最近在学tensorflow serving 模块,一直对接口不了解,后面看到这个文章就豁然开朗了, 主要的困难在于   tf.saved_model.builder.SavedModelBuilde ...