find()# 方法
find()# 范围查找子串,返回索引值,找不到返回-1 # 语法
s.find(substring, start=0, end=len(string))
# 参数
# substring -- 指定检索的字符串
# start -- 开始索引,默认为0。
# end -- 结束索引,默认为字符串的长度。 # 示例
s = 'python'
s.find('th')
# 2
s.find('th',1,2)
# -1
s.find('th',1,3)
# -1
s.find('th',1,4)
# 2
s.find('tb',1,4)
# -1
count()方法
# 统计字符串里某个字符出现的次数。 # 语法
s.count(substring, start= 0,end=len(string))
# 参数
# sub -- 搜索的子字符串
# start -- 字符串开始搜索的位置。默认为第一个字符,第一个字符索引值为0。
# end -- 字符串中结束搜索的位置。字符中第一个字符的索引为 0。默认为字符串的最后一个位置。 # 示例
s = 'password'
s.count('s')
# 2
s.count('s',0,2)
# 0
s.count('s',0,3)
# 1
join()# 方法
join() # 方法用于将序列中的元素以指定的字符连接生成一个新的字符串。 # 语法
s.join(sequence)
# 参数
# sequence--要生成字符串的元素序列。 # 示例
ls = ['p', 'y', 't', 'h', 'o', 'n']
s = '' # 拼接的字符
s.join(ls)
# 'python'
s = '-'
s.join(ls)
# 'p-y-t-h-o-n'
# replace()方法
# replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串) # 语法
s.replace(old, new[, max]) # 参数
old -- 将被替换的子字符串。
new -- 新字符串,用于替换old子字符串。
max -- 可选字符串, 替换不超过 max 次
# 示例 s = 'hello word'
s.replace('word', 'python')
'hello python'
s = 'hello-word'
s.replace('-', '')
'helloword'
s.replace('-', ' ')
'hello word'
strip()方法
strip() 方法用于移除字符串头尾指定的字符(默认为空格)或字符序列。 # 语法
s.strip([chars])
# 参数
chars -- 移除字符串头尾指定的字符序列。 # 示例
s = ' abcdefg '
s.strip()
'abcdefg'
s = ' 123abcdefg123 '
s.strip('')
' 123abcdefg123 '
s = '123abcdefg123'
s.strip('')
'abcdefg'
# 说明
# 如果字符中前后有空格需新去掉空格才能移除指定的字符 split()方法
split()通过指定分隔符对字符串进行切片 # 语法
s.split(str="", [num])
# 参数
str -- 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。
num -- 分割次数。 # 示例
s = 'this is string'
s.split()
['this', 'is', 'string']
s.split('s', 1) # 切割一次
['thi', ' is string']
# 去除字符串空格的方法
# 1、使用字符串函数replace() >>> s = ' hello world ' >>> s.replace(' ', '')
'helloworld'
# 2、使用字符串函数split() >>> s = ' hello world '
>>> s = ' '.join(s.split())
>>> print(s)
helloworld
# eval()方法
# eval() 函数用来执行一个字符串表达式,并返回表达式的值。 # 示例
def eval_test():
l='[1,2,3,4,[5,6,7,8,9]]'
d="{'a':123,'b':456,'c':789}"
t='('a', 'b', 'c', 'd')'
a = '2 * 3'
print '--------------------------转化开始--------------------------------'
print type(l), type(eval(l))
print type(d), type(eval(d))
print type(t), type(eval(t))
print(eval(a))
if __name__=="__main__":
eval_test()
# 输出结果 --------------------------转化开始--------------------------------
<type 'str'> <type 'list'>
<type 'str'> <type 'dict'>
<type 'str'> <type 'tuple'>
6
# 其他方法
s.capitalize() # 首字母大写 s.lower() # 转小写 s.upper() # 转大写 s.swapcase() # 大小写互换 len(str) # 字符串长度 cmp(“my friend”,str) # 字符串比较 第一个大,返回1 按ascii码表比较 max('abcxyz') # 寻找字符串中最大的字符 min('abcxyz') # 寻找字符串中最小的字符 s.startswith()('ab') # 判断字符串以是否已ab开头 s.endwith('xz') # 判断字符串是否已xz结尾

python3的字符串常用方法的更多相关文章

  1. OC中的字符串常用方法

    OC中的字符串常用方法 OC中对字符串进行操作使用了Foundation框架中的NSString类(不可变).NSMutableString类(可变). NSString 1.创建字符串 [objc] ...

  2. python 字符串常用方法

    字符串常用方法 capitalize() String.capitalize() 将字符串首字母变为大写 name = 'xiaoming' new_name = name.capitalize() ...

  3. python基础3 字符串常用方法

    一. 基础数据类型 总览 int:用于计算,计数,运算等. 1,2,3,100...... str:'这些内容[]'    用户少量数据的存储,便于操作. bool: True, False,两种状态 ...

  4. Python基础二_操作字符串常用方法、字典、文件读取

    一.字符串常用方法: name.captitalize()                       #字符串首字母大写 name.center(50,'*')                   ...

  5. Python3 格式化字符串

    Python3 格式化字符串 在Python 3.6之前,有两种将Python表达式嵌入到字符串文本中进行格式化的主要方法:%-formatting和str.format() 一.%-formatti ...

  6. python_字符串常用方法

    1.切片就是通过索引(索引:索引:步长)截取字符串的一段,形成新的字符串(原则就是顾头不顾腚). a = 'ABCDEFGHIJK' print(a[0:3]) # print(a[:3]) 从开头开 ...

  7. 一篇文章助你理解Python3中字符串编码问题

    前几天给大家介绍了unicode编码和utf-8编码的理论知识,以及Python2中字符串编码问题,没来得及上车的小伙伴们可以戳这篇文章:浅谈unicode编码和utf-8编码的关系和一篇文章助你理解 ...

  8. Day2:字符串常用方法

    字符串常用方法 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan name = "my \tname is ...

  9. 2018.03.26 Python-Pandas 字符串常用方法

    import numpy as np import pandas as pd 1 #字符串常用方法 - strip s = pd.Series([' jack ','jill',' jease ',' ...

随机推荐

  1. xutils工具上传日志文件--后台服务器的搭建

    在上一篇文章中使用xutils将手机上保存的日志上传到后台服务器中,现在我们来讲后台服务器是如何搭建的 后台服务器采用jsp+sevlet+mysql的框架 首先讲mysql数据库的表的建立 在fil ...

  2. 3、struct2的常见配置

    1.在eclipse中如何复制一个工程作为一个新的工程 在struct.xml中: <result name="success">/login_sucess.jsp&l ...

  3. Python3-shutil模块-高级文件操作

    Python3中的shutil模块提供了对文件和容器文件的一些高级操作 shutil.copy(src, dst) 拷贝文件,src和dst为路径的字符串表示,copy()会复制文件数据和文件权限,但 ...

  4. 【思考】URI和URL的区别?以及URL的结构

    URI = Universal Resource Identifier URL = Universal Resource Locator 在学习中,我们难免会遇到 URI 和 URL,有时候都傻傻分不 ...

  5. 深度学习“四大名著”发布!Python、TensorFlow、机器学习、深度学习四件套!

    Python 程序员深度学习的"四大名著": 这四本书着实很不错!我们都知道现在机器学习.深度学习的资料太多了,面对海量资源,往往陷入到"无从下手"的困惑出境. ...

  6. 入门大数据---Map/Reduce,Yarn是什么?

    简单概括:Map/Reduce是分布式离线处理的一个框架. Yarn是Map/Reduce中的一个资源管理器. 一.图形说明下Map/Reduce结构: 官方示意图: 另外还可以参考这个: 流程介绍: ...

  7. python高阶-Linux基础命令集

    声明: 1)仅作为个人学习,如有冒犯,告知速删! 2)不想误导,如有错误,不吝指教! 1: 查看文件信息:ls ls常用参数: 参数 含义 -a 显示指定目录下所有子目录与文件,包括隐藏文件 -l 以 ...

  8. spring配置文件的部分配置文字说明

    http://blog.csdn.net/zzjjiandan/article/details/22922847/ spring的组成: <beans> <contenxt> ...

  9. UVA1464 Traffic Real Time Query System

    传送门:https://www.luogu.com.cn/problem/UVA1464 看到这道题,求必经的点数,还是无向图.那么妥妥的圆方树.圆方树上的任意两圆点间的路径必定是圆点方点相交错的,对 ...

  10. CF1051F The Shortest Statement 题解

    题目 You are given a weighed undirected connected graph, consisting of n vertices and m edges. You sho ...