• 字符串截取 
>>>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. Python9-IO模型-day41

    # 进程:启动多个进程,进程之间是由操作系统负责调用# 线程:启动多个线程,真正由被cpu执行的最小单位实际是线程# 开启一个线程,创建一个线程,寄存器.堆栈# 关闭一个线程# 协程# 本质上是一个线 ...

  2. K-th Number POJ - 2104

    K-th Number POJ - 2104 You are working for Macrohard company in data structures department. After fa ...

  3. 统计C语言关键字出现次数

    统计C语言关键字出现次数 <C程序设计语言>K&R版本第6章结构6.3结构数组内容 /* Name: 统计c语言关键字出现次数 Copyright: Author: lingr7 ...

  4. CF 497 div 2 B

    B. Turn the Rectangles time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  5. centos6.4编译hadoop2.4源码

    4.1.环境: 1)Linux 64 位操作系统,CentOS 6.4 版本,VMWare 搭建的虚拟机 2)虚拟机可以联网 4.2.官方编译说明: 解压命令:tar -zxvf hadoop-2.4 ...

  6. Diycode开源项目 MyTopicActivity分析

    1.总体浏览效果及布局分析 1.1.看一下我的帖子预览效果 1.2.然后看一下我的收藏预览效果 1.3.归纳一下 所以我的帖子和我的收藏可以共用同一个类. 布局效果几乎一样,只是用户的选择不同. 所以 ...

  7. 62、在app遇到全局异常时避免直接退出,如何让app接管异常处理?

    1.创建一个类为CrashHandler import android.content.Context; import android.os.Looper; import android.util.L ...

  8. Windows下MySQL8.0.11.0安装教程

    1.mysql下载地址:https://dev.mysql.com/downloads/installer/ 2.下载安装MySQL 8.0.11.0 https://cdn.mysql.com//D ...

  9. Python中@property和@classmethod和@staticmethod

    前戏 首先,先要弄清楚一个类里面的,各个组成部分都应该怎么称呼. - 注:可能叫法会不太一样. 关于@property 顾名思义:它的意思为‘属性’. 作用: 1:使用它你将会把类方法,变为类属性.并 ...

  10. DFS排列组合问题

    这四个使用DFS来求解所有组合和排列的例子很有代表性,这里做一个总结: 1.不带重复元素的子集问题 public ArrayList<ArrayList<Integer>> s ...