python中的字符串
一、在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中的字符串的更多相关文章
- Python中Unicode字符串
Python中Unicode字符串 字符串还有一个编码问题. 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特(bit)作为一个字节(byte ...
- Python中的字符串处理
Python转义字符 在需要在字符中使用特殊字符时,python用反斜杠(\)转义字符.如下表: 转义字符 描述 \(在行尾时) 续行符 \\ 反斜杠符号 \' 单引号 \" 双引号 \a ...
- python中修改字符串的几种方法
在Python中,字符串是不可变类型,即无法直接修改字符串的某一位字符.因此改变一个字符串的元素需要新建一个新的字符串.常见的修改方法有以下4种. 方法1:将字符串转换成列表后修改值,然后用join组 ...
- python中根据字符串导入模块module
python中根据字符串导入模块module 需要导入importlib,使用其中的import_module方法 import importlib modname = 'datetime' date ...
- 【转】Python中的字符串与字符编码
[转]Python中的字符串与字符编码 本节内容: 前言 相关概念 Python中的默认编码 Python2与Python3中对字符串的支持 字符编码转换 一.前言 Python中的字符编码是个老生常 ...
- Python中常见字符串去除空格的方法总结
Python中常见字符串去除空格的方法总结 1:strip()方法,去除字符串开头或者结尾的空格>>> a = " a b c ">>> a.s ...
- Python中的字符串方法
Python中的字符串方法 字符串类即str提供了许多有用的方法来操纵字符串.具体来说,我们将讨论如下的方法. 搜索字符串内的子字符串. 测试字符串. 格式字符串. 转换字符串. 回顾前面的章节,方法 ...
- python中的字符串切片
python中的字符串切片,似乎有点乱,例如: >>>pystr='Python' >>>pystr[2:5] 就会输出 'tho' 这该怎样理解呢?中括号[2:5 ...
- python 中的字符串格式化
python 中的字符串格式化 %方式的调用 1.格式化代码 代码 意义 s 字符串,使用str r 字符串,使用repr不使用str c 字符 d 十进制的数字 i 整数 u 无符号整数 o 八进制 ...
随机推荐
- 《Linux内核原理与分析》第二周作业
反汇编一个简单的C程序 1.实验要求 使用: gcc –S –o test.s test.c -m32 命令编译成汇编代码,对汇编代码进行分析总结.其中test.c的具体内容如下: int g(int ...
- WebSocket是什么原理?为什么可以实现持久连接?
作者:Ovear 链接:https://www.zhihu.com/question/20215561/answer/40316953来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载 ...
- javascript运行机制之执行顺序详解
1.代码块 指的的是有标签分割的代码段. 例如: <script type="text/javascript"> alert("这是代码块一"); ...
- 装系统时 System clock uses UTC 问题
装系统也装了至少不下50次了,每次都是傻瓜一样的按照第一印象在弄,从未想过为啥,装到这里的时候,System clock uses UTC 勾不勾呢,每次都是百度,然后装完这一次下一次又忘了,这是没有 ...
- Linux内核分析第二次作业
这周学习了<庖丁解牛Linux内核分析>并且学习了实验楼的相关知识. 在实验楼的虚拟环境下编写代码: 通过gcc编译后,使用查看文件命令:cat -n 20189223.c 在vim中, ...
- ubuntu python3和python2切换脚本
最近在ubuntu上开发较多,有些工具只能在python2运行,而开发又是在python3上做的开发,所以写个脚本方便在python2和python3之间切换. 切换成python2的文件usepy2 ...
- mysql 远程备份
#远程备份./innobackupex --defaults-file=/etc/my.cnf --no-timestamp --user xxx --host 192.168.1.123 \--pa ...
- pyhdfs安装
参考: http://blog.csdn.net/sinat_33741547/article/details/54428726 1.先更新pip,防止版本过低pip install --upgrad ...
- read()、write()返回 Input/output error, Device or resource busy解决
遇到的问题,通过I2C总线读.写(read.write)fs8816加密芯片,报错如下: read str failed,error= Input/output error! write str fa ...
- cookie and sesssion
会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端 ...