一、在python中,字符串是不可变类型

通过以下代码说明:

>>> s = 'hello, world'
>>> id(s)
2108634288304
>>> s = 'hello, chenjun'
>>> id(s)
2108634548592

可以看到,改变字符串变量s的取值,其内存地址发生了变化,因此字符串是不可变数据类型。

二、字符串的操作:

字符串拼接(通过+来实现):

>>> s = 'hello'
>>> s = s + 'world'
>>> s
'helloworld'

字符串替换:

>>> s = 'hello, world'
>>> s.replace('world', 'chenjun')
'hello, chenjun'

字符串首字母大写:

>>> s = 'hello, world'
>>> s.capitalize()
'Hello, world'

字符串全变小写:

>>> s = 'HELLO'
>>> s.casefold()
'hello'

或者

>>> s = 'HELLO'
>>> s.lower()
'hello

字符串全变大写:

>>> s = 'hello'
>>> s.upper()
'HELLO'

字符串大写变小写,小写变大写:

>>> s = 'hEllo'
>>> s.swapcase()
'HeLLO'

将字符串变成标题格式:

>>> s = 'hello, world'
>>> s.title()
'Hello, World'

判断字符串是否是标题格式,返回True or False:

>>> s = 'hello, world'
>>> s.istitle()
False

判断字符串是否以某个指定字幕开头或结尾:

>>> s = 'hello, world'
>>> s.startswith('h')
True
>>> s.endswith('h')
False

判断字符串是大写还是小写:

>>> s = 'hello, world'
>>> s.isupper()
False
>>> s.islower()
True

查字符串中某指定字符出现次数,可指定位置查询:

>>> s.count('l')
3
>>> s.count('l', 3, 11) #空格和逗号算一个字符
2

查字符串中某指定字符的index,可指定位置查询:

>>> s = 'hello, world'  #默认从左向右查询,返回第一个坐标
>>> s.find('l')
2
>>> s.rfind('l') #从右往左查询
10
>>> s.find('l', 3, 12) #指定位置查询
3

填充字符:

>>> s = 'hello, world'
>>> s.center(30, '=') #填充使字符居中
'=========hello, world========='
>>> s.ljust(30, '=') #填充使字符居左
'hello, world=================='
>>> s.rjust(30, '=') #填充使字符居右
'==================hello, world'
>>> s.zfill(30) #从左填充,以0补充空位
'000000000000000000hello, world'

去空格:

>>> s = '  hello,  world  '
>>> s.strip() #去左右空格
'hello, world'
>>> s.lstrip() #去左空格
'hello, world '
>>> s.rstrip() #去右空格
' hello, world'

字符串格式化:

>>> s = 'hello, {}'.format('chenjun')
>>> s
'hello, chenjun'
>>> s = 'my name is {dic[name]}, I am {dic[age]} years old'.format(dic = dic)
>>> s
'my name is chenjun, I am 21 years old'

以上是一些基本的字符串操作案例。

python中的字符串的更多相关文章

  1. Python中Unicode字符串

    Python中Unicode字符串 字符串还有一个编码问题. 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特(bit)作为一个字节(byte ...

  2. Python中的字符串处理

    Python转义字符 在需要在字符中使用特殊字符时,python用反斜杠(\)转义字符.如下表: 转义字符 描述 \(在行尾时) 续行符 \\ 反斜杠符号 \' 单引号 \" 双引号 \a ...

  3. python中修改字符串的几种方法

    在Python中,字符串是不可变类型,即无法直接修改字符串的某一位字符.因此改变一个字符串的元素需要新建一个新的字符串.常见的修改方法有以下4种. 方法1:将字符串转换成列表后修改值,然后用join组 ...

  4. python中根据字符串导入模块module

    python中根据字符串导入模块module 需要导入importlib,使用其中的import_module方法 import importlib modname = 'datetime' date ...

  5. 【转】Python中的字符串与字符编码

    [转]Python中的字符串与字符编码 本节内容: 前言 相关概念 Python中的默认编码 Python2与Python3中对字符串的支持 字符编码转换 一.前言 Python中的字符编码是个老生常 ...

  6. Python中常见字符串去除空格的方法总结

    Python中常见字符串去除空格的方法总结 1:strip()方法,去除字符串开头或者结尾的空格>>> a = " a b c ">>> a.s ...

  7. Python中的字符串方法

    Python中的字符串方法 字符串类即str提供了许多有用的方法来操纵字符串.具体来说,我们将讨论如下的方法. 搜索字符串内的子字符串. 测试字符串. 格式字符串. 转换字符串. 回顾前面的章节,方法 ...

  8. python中的字符串切片

    python中的字符串切片,似乎有点乱,例如: >>>pystr='Python' >>>pystr[2:5] 就会输出 'tho' 这该怎样理解呢?中括号[2:5 ...

  9. python 中的字符串格式化

    python 中的字符串格式化 %方式的调用 1.格式化代码 代码 意义 s 字符串,使用str r 字符串,使用repr不使用str c 字符 d 十进制的数字 i 整数 u 无符号整数 o 八进制 ...

随机推荐

  1. DataTable的详细用法

    在项目中经常用到DataTable,如果DataTable使用得当,不仅能使程序简洁实用,而且能够提高性能,达到事半功倍的效果,现对DataTable的使用技巧进行一下总结. 一.DataTable简 ...

  2. caffe-windows环境配置(github上官方BVLC/caffe的推荐配置方法详解)

    [转载来的文章:如有侵权,请联系我!我将马上删除!] 首先声明一下,如标题,本教程是caffe在windows系统上的配置方法,而且是github上官方BVLC/caffe目前推荐的配置方法,并不是使 ...

  3. npm i 出错

    npm i npm ERR! code ECONNRESET npm ERR! errno ECONNRESET npm ERR! network request to https://registr ...

  4. flutter环境配置

    java环境安装 做基于android的原生app,首先需要安装java环境,需要到官网https://www.oracle.com/technetwork/java/javase/downloads ...

  5. 纯CSS制作图形效果

    下面所有的例子都是在demo.html的基础上添加相关样式实现的. <!DOCTYPE html> <html> <head> <meta charset=& ...

  6. keil5 MDK 链接报错 Error: L6410W 解决

    keil5 MDK 报错 Build target 'Project' linking... .\Output\Project.axf: Warning: L6310W: Unable to find ...

  7. SQLServer、MySQL、Oracle如何查看所有表的条数

    SQLServer: create table #t(name varchar(255), rows bigint, reserved varchar(20), data varchar(20), i ...

  8. oracle体系结构理解

    体系结构相关内容每次看遍书,过段时间就忘了..无奈用自己理解的方式记录之. 1.commit与写盘与否没有关系,也就是说修改数据(insert update delete)后并提交数据,这条被修改的数 ...

  9. 字符串String的API

      字符串的理解 1. 字符串的属性 str.length 2. 字符串的方法 charAt() charCodeAt() indexOf() lastIndexOf() slice() substr ...

  10. 定位JVM内存溢出问题思路总结

    JVM的内存溢出问题,是个常见而有时候有非常难以定位的问题.定位内存溢出问题常见方法有很多,但是其实很多情况下可供你选择的有效手段非常有限.很多方法在一些实际场景下没有实用价值.这里总结下我的一些定位 ...