• 字符串截取 
>>>s = 'hello'
>>>s[0:3]
'he'
>>>s[:] #截取全部字符
'hello'
  • 消除空格及特殊符号
s.strip() #消除字符串s左右两边的空白字符(包括'\t','\n','\r','')

s.strip('') #消除字符串s左右两边的特殊字符(如'0'),字符串中间的'0'不会删除
例如:
>>>s = '000hello00world000'
>>>s.strip('')
'hello00world' s.strip('')等价于s.strip('')
例如:
>>>s = '12hello21'
>>>s.strip('')
'hello' lstrip,rstrip 用法与strip类似,分别用于消除左、右的字符
  • 字符串复制
s1 = 'hello'
s2 = s1 # s2 = 'hello' 若指定长度
s1 = 'hello'
s2 = s1[0:2] #s2 = 'he'
  • 字符串连接 
s1 = 'hello'
s2 = 'world'
s3 = s1 + s2 #s3 = 'helloworld' 或者
import operator
s3 = operator.concat(s1,s2) #concat为字符串拼接函数
  • 字符串比较 
(1)利用operator模块方法比较(python3.X取消了cmd函数)
包含的方法有:
lt(a, b) ———— 小于
le(a, b) ———— 小于等于
eq(a, b) ———— 等于
ne(a, b) ———— 不等于
ge(a, b) ———— 大于等于
gt(a, b) ———— 大于 例子:
>>>import operator
>>>operator.eq('abc','edf') #根据ASCII码比较
Flase
>>>operator.gt('abc','ab')
True (2)关系运算符比较(>,<,>=,<=,==,!=)
>>>s1 = 'abc'
>>>s2 = 'ab'
>>>s1 > s2
True
>>>s1 == s2
False
  • 求字符串长度 
>>>s1 = 'hello'
>>>len(s1)
5
  • 求字符串中最大字符,最小字符 
>>>s1 = 'hello'
>>>max(s1) #求字符串s1中最大字符
'o'
>>>min(s1) #求字符串s2中最小字符
'e'
  • 字符串大小写转换 
主要有如下方法:
upper ———— 转换为大写
lower ———— 转换为小写
title ———— 转换为标题(每个单词首字母大写)
capitalize ———— 首字母大写
swapcase ———— 大写变小写,小写变大写 例子:
>>>s1 = 'hello'
>>>s2 = 'WORLD'
>>>s3 = 'hello world'
>>>s1.upper()
'HELLO'
>>>s2.lower()
'world'
>>>s3.title()
'Hello World'
>>>s3.capitalize()
'Hello world'
>>>s3.title().swapcase()
'hELLO wORLD'
  • 字符串翻转
>>>s1 = 'hello'
>>>s1[::-1]
'olleh'
  • 字符串分割
split方法,根据参数进行分割,返回一个列表

例子:
>>>s1 = 'hello,world'
>>>s1.split(',')
['hello','world']
  • 字符串序列连接 
join方法:
语法为str.join(seq) #seq为元素序列 例子: >>>l = ['hello','world']
>>>str = '-'
>>>str.join(l)
'hello-world'
  • 字符串内查找 
find方法:
检测字符串内是否包含子串str
语法为:
str.find(str[,start,end]) #str为要查找的字符串;strat为查找起始位置,默认为0;end为查找终止位置,默认为字符串长度。若找到返回起始位置索引,否则返回-1 例子: >>>s1 = 'today is a fine day'
>>>s1.find('is')
6
>>>s1.find('is',3)
6
>>>s1.find('is',7,10)
-1
  • 字符串内替换 
replace方法:
把字符串中的旧串替换成新串
语法为:
str.replace(old,new[,max]) #old为旧串,new为新串,max可选,为替换次数 例子:
>>>s1 = 'today is a find day'
>>>s1.replace('find','rainy')
'today is a rainy day'
  • 判断字符串组成 
主要有如下方法:
isdigit ———— 检测字符串时候只由数字组成
isalnum ———— 检测字符串是否只由数字和字母组成
isalpha ———— 检测字符串是否只由字母组成
islower ———— 检测字符串是否只含有小写字母
isupper ———— 检测字符串是否只含有大写字母
isspace ———— 检测字符串是否只含有空格
istitle ———— 检测字符串是否是标题(每个单词首字母大写) 例子:
>>>s1 = 'hello'
>>>s1.islower()
True
>>>s1.isdigit()
False

python3字符串操作总结的更多相关文章

  1. python3字符串操作

    python3字符串操作 x = 'abc' y = 'defgh' print(x + y) #x+y print(x * ) #x*n print(x[]) #x[i] print(y[:-]) ...

  2. [No000078]Python3 字符串操作

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- '''Python 字符串操作 string替换.删除.截取.复制.连接.比较.查找.包含.大小写转换.分 ...

  3. python3 字符串操作相关函数

    整理自python基础|菜鸟教程 感谢菜鸟教程提供的优质资源! 1.capitalize() 将字符串的第一个字符转换为大写 实例 以下实例展示了capitalize()方法的实例: #!/usr/b ...

  4. Python3 字符串操作

    截掉指定字符串 # 截掉指定字符串 string.strip("what you want to delete") #截掉字符串左边的空格 string.lstrip() #截掉字 ...

  5. python3.4学习笔记(十五) 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)

    python3.4学习笔记(十五) 字符串操作(string替换.删除.截取.复制.连接.比较.查找.包含.大小写转换.分割等) python print 不换行(在后面加上,end=''),prin ...

  6. Python中的字符串操作总结(Python3.6.1版本)

    Python中的字符串操作(Python3.6.1版本) (1)切片操作: str1="hello world!" str1[1:3] <=> 'el'(左闭右开:即是 ...

  7. Python3基础(2)模块、数据类型及运算、进制、列表、元组、字符串操作、字典

    ---------------个人学习笔记--------------- ----------------本文作者吴疆-------------- ------点击此处链接至博客园原文------ 1 ...

  8. Python3学习之路~2.3 字符串操作

    字符串操作 特性:不可修改 name="my \tname is alex" print(name.capitalize()) #首字母变大写 print('Alex LI'.ca ...

  9. python3.0 day02 列表、元组 、字典、字符串操作

    1.列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作,类似于其他语言中的数组. 定义列表 names = ['Lioa',"Tenglan ...

随机推荐

  1. 将数组转化为json字符串(不使用json_encode函数)

    将数组转化为json字符串(不使用json_encode函数) public function arrayToJson($arr,$jsonStr=''){ $jsonStr.='{'; foreac ...

  2. 用Python学分析 - 单因素方差分析

    单因素方差分析(One-Way Analysis of Variance) 判断控制变量是否对观测变量产生了显著影响 分析步骤 1. 建立检验假设 - H0:不同因子水平间的均值无差异 - H1:不同 ...

  3. STM32位带操作

    STM32的位带操作是基于cortex内核自带的,而不是st公司独创.基本的思路就是用一个32位的地址空间访问一个bit,因为stm32只支持32位数据的读取,不像51单片机一样,是可以单独对一位操作 ...

  4. 谋哥:《App自推广》连载2直立人行走迁徙

    [谋哥每天一干货,第六十九篇] 前篇说到声音在远古时代,是一个神奇的东西,它能够很快地把信息传播到其他地方,突破了短距离.然而能人的后代直立人学会了直立行走,他们开始走出非洲,到达遥远的中东.中国,还 ...

  5. 【Best Time to Buy and Sell Stock II】cpp

    题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

  6. 使用 SpiritManager 类管理在 XNA 游戏中的精灵(十四)

    平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...

  7. python中 in, any 和 all用法

    in if x == 1 or y == 1 or z == 1: print('passed') if 1 in (x, y, z): print('passed') any if x or y o ...

  8. jquery判断元素是否存在在数组中

    var myArray = new Array(); function checkRepeat(sel) { console.log("索引是:" + $.inArray(sel, ...

  9. [cocos2dx utils] cocos2dx读取,解析csv文件

    在我们的游戏中,经常需要将策划的数值配置成csv文件,所以解析csv文件就是一个很common的logic, 例如如下csv文件: 下面是一个基于cocos2dx 2.2.4的实现类: #ifndef ...

  10. NetScaler 12.1 Deploy Package

    NetScaler 12.1 Deploy Package NS_VPX_Deploy_Package 百度网盘共享地址https://pan.baidu.com/s/1OT0Hxuz6ZBLwwM5 ...